This is the 16th day of my participation in the August Text Challenge.More challenges in August
concept
-
At the bottom level of the system, data transmission is in the form of simple byte sequence transmission, that is, at the bottom level, the system does not know the object, only recognize the byte sequence, and in order to achieve the purpose of process communication, it is necessary to serialize the data, and serialization is the process of converting objects into byte sequence. Conversely, when a sequence of bytes is shipped to the corresponding process, the process, in order to recognize the data, needs to de-sequence it, that is, turn the sequence of bytes into an object
-
Serialization is indispensable for inter-process communication, local data storage and network data transmission. The selection of appropriate serialization scheme for different scenarios has a great impact on the performance of applications.
-
Broadly speaking, data serialization is the process of converting a data structure or object into a data format that we can store or transfer. During serialization, data structures or objects write their state information to temporary or persistent storage. During deserialization, It can be said that the generated data is reduced to a data structure or object process.
-
In this sense, data serialization is an extension of our original concept of object serialization. In object serialization and de-serialization, we are familiar with two methods, one is the Serializable interface provided by Java language, and the other is the Parcelable interface provided by Android. Here, because we made a extension to the concept, so also need to consider several specific to serialize data structure method, as the data returned as now the open API is the JSON format, or is our Android native SQLite database to realize the data is stored locally, broadly speaking, All of this can be thought of as data serialization
serialization
The process of converting Java objects into byte sequences, or data structures or objects into binary strings. After serialization, it is no longer an object
deserialization
The process of restoring byte sequences to Java objects, the process of converting binary strings generated during serialization into data structures or objects.
Objects and binary
Everything we operate on in Java is an object, derived from an instantiation of an object. The concepts closest to data structures in Java are entity classes, POJos, and Javabeans.
For cross-language communication, the serialized data cannot of course be a language-specific data type. Binary strings in Java refer to byte[], and byte is one of the 8 Primitive data types in Java.
Purpose of serialization/deserialization
- Serialization: Mainly used for network transmission, data persistence, generally serialization is also called Encode.
- Deserialization: Used mainly to read byte arrays from a network or disk and restore them to their original objects. Deserialization is also called Decode.
Common protocols for serialization and deserialization
XML&SOAP
XML is a common serialization and deserialization protocol. It has the advantages of cross-machine and cross-language. SOAP (Simple Object Access Protocol) is a widely used structured message delivery protocol based on XML for serialization and deserialization
JSON (Javascript Object Notation)
JSON originates from the weakly typed language Javascript. It is generated from a concept called “Associative array”, whose essence is to use “attribute-value” to describe objects. In fact, in weakly typed languages like Javascript and PHP, classes are described as Associative arrays. JSON is fast becoming one of the most widely used serialization protocols due to the following advantages.
- This Associative array format fits well with engineers’ understanding of objects.
- It retains the benefits of XML being human-readable.
- Serialized data is much more compact than XML. Research from the following links shows that XML produces sequenced files that are nearly twice the size of JSON
- It has Javascript innate support, so it is widely used in Web Browser applications, is the real Ajax standard protocol.
- Compared with XML, its protocol is simpler and faster to parse.
- Loose Associative array makes it have good scalability and compatibility
Protobuf
Protobuf has many of the typical features required for a good serialization protocol.
- Standard IDL and IDL compiler, which makes it very engineer friendly.
- Serialized data is very compact and compact, with about 1/3 to 1/10 of the serialized data compared to XML.
- Parsing is very fast, about 20-100 times faster than the corresponding XML.
- Provides a very friendly dynamic library, very simple to use, deserialization requires only one line of code.
Serialized object
Defining entity Classes
public class Users implements Serializable {
private String name;
private int age;
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge(a) {
return age;
}
public void setAge(int age) {
this.age = age; }}Copy the code
public class Client {
public static void main(String[] args) {
Users users = new Users();
users.setAge(18);
users.setName("Zhang");
try{
FileOutputStream fileOut =
new FileOutputStream("File\user.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(users);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in File\user.txt");
}catch(IOException i) { i.printStackTrace(); }}}Copy the code
Deserialize object
public class Client {
public static void main(String[] args) {
Users users = null;
try
{
FileInputStream fileIn = new FileInputStream("File\user.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
users = (Users) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c) {
c.printStackTrace();
return;
}
System.out.println("success......");
System.out.println("Name: " + users.getName());
System.out.println("age: " + users.getAge());
}
}
Copy the code