What is serialization? Why serialization?
1. What is serialization?
Serialization is the process of converting the state information of an object into a form that can be stored or transmitted.
Serialization is divided into two parts: serialization and deserialization.
Serialization is the first part of this process, which decomposes the data into byte streams for storage in files or transmission over the network.
Deserialization is to open the byte stream and reconstruct the object.
Generally speaking: serialization is to store objects in binary format.
2. Why serialize objects?
Serialization is the process of converting an object into an easily transmitted format to solve the problems caused by reading and writing to the object stream.
Two usage scenarios for object serialization:
- Object persistence: persisting the state of an object, such as storing it in a database.
- Object remote transfer: sends objects from one computer to another.
Implementation mode
Serializable interface
Serializable is a serialization interface provided by java. It is an empty interface that provides standard serialization and deserialization operations for objects.
To serialize an object, you only need this class to implement the serializable interface (and declare serializable uid. Serializable uid is not required, but its necessity will be discussed below).
public class User implements Serializable{ private static final long SerializableUID =1L; public int userId; ...... }
Serializable way to achieve object serialization and deserialization is also very simple: you only need to use ObjectOutputStream and ObjectInputStream.
//Serialization process User user = new User(0,"jack"); ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream(cache.txt)); out.writeObject(user); out.close(); //Deserialization process (the contents of the replied objects newUser and user are exactly the same, but they are not the same object) ObjectInputStream in =new ObjectInputStream(new FileInputStream(cache.txt)); User newUser=(User)in.readObject(); in.close();
Necessity of SerializableUID
Even if we do not specify SerializableUID, serialization can be realized. Should we specify it?
Answer: it is necessary to specify a SerializableUID to avoid the failure of the deserialization process.
SerializableUID is used to assist the serialization and deserialization process. In principle, only the SerializableUID in the deserialized data is the same as the SerializableUID of the current class can be deserialized normally.
Working mechanism of SerializableUID
- When serializing, the system will write the SerializableUID of the current class into the serialized file
- During deserialization, the system will detect the serializable uid in the file to see if it is consistent with the serializable uid of the current class. If it is consistent, it means that the version of the serialized class is the same as that of the current class. At this time, the deserialization can be successful; Otherwise, it means that the current class has changed compared with the serialized class (for example, the number and type of member variables may have changed). At this time, it cannot be deserialized normally.
When we manually set the SerializableUID, we can largely avoid the failure of the deserialization process (for example, after the version upgrade, we may delete a member variable or add some member variables. At this time, our deserialization process can still succeed).
If the organization of a class changes unconventionally (such as changing the class name and the type of member variables), the deserialization process will fail even though the serializable uid verification passes, because the class structure has been destructively changed.
Parcelable interface
Parcelable is a new serialization method provided by Android. Parcelable is also an interface. As long as this interface is implemented, the object of a class can be serialized and passed through Intent and Binder.
//Use of Parcelable interface public class User implements Parcelabe{ public int userId; public String userName; public User(int userId,String userName){ this.userId=userId; this.userName=userName; } public int describeCobtents(){ return 0; } public void writeToParcel(Parcel out ,int flags){ out.writeInt(userId); out.writeStrin(userName); } public static final Parcelable.Creator<User> CREATOR =new Parcelable.Creator<User>(){ public User createFrromParcel(Parcel in){ return new User(in); } public User[] newArray(int size){ return new suer[size]; ; } }; private User(Parcel in){ userId=in.readInt(); userName=in.readString(); } }
Differences and usage scenarios between the two
Both Serializable and Parcelable can realize serialization and can be used for data transmission of Intent key. How to select them?
- Serializable is a serialization interface provided by Java, which is simple to use but expensive, because the serialization and deserialization process requires a lot of I/O operations;
- Although Parcelable is a little troublesome to use, it is efficient and is the serialization method recommended by Android.
Usage scenario:
- Only serialization is performed, and Parcelable is used for data transmission in the application because of its high efficiency
- If persistence or network transmission is required, it is recommended to use Serializable because the Parcelable is complex.