the principle of serialization:
Referencing Java language Programming-the No. 539 page of the basic article reads: Not all objects can be serialized (this sentence shows that serialization must have some condition to be serialized) because the serialized object is an instance of the Java.Io.Serializable interface. This means that the interface must be implemented if the serialization function is to be implemented, but what is the use of serialization? Let's take a look at what the source code describes. (Source download address)
The source of this is said to support IO operations but also throw IO exception that is, after instantiating the interface, the object can be converted to flow operations, then the question is, if the object into a Stream object, then how do we get the stream object into the corresponding stream object?
This is where the source Java Development Engineer gives a concept: deserialization, which means that for objects that have been transformed into serialized objects, they can be serialized in reverse direction by means of deserialization, and then serialized.
In the final analysis: serialization is the source object into Io, and deserialization is to say IO into the source object, but serialization and deserialization corresponds to a key to ensure that their corresponding source object is consistent, When using serialization, it must be kept in mind that if the object has changed and the key is not declared then the inverse sequence and the sequence corresponding to the object are unequal and the reverse sequence will not succeed. The advantage of this is that the persistence enhancement of the data can be saved, the kind of serialization
There are two types of serialization in Android: One is the Java serializable, the other is a parcelable encapsulated by Google engineers. Serializable:
The serializable interface, a serialized interface provided by Java, is an empty interface that provides standard serialization and deserialization operations for objects. The serialization of objects implemented using serializable is fairly straightforward, and it is simple to declare the interface in a class, and it is not even necessary to declare SERIALVERSIONUID to complete the process, but serialversionuid need not be specified.
Need.
The Java API provides this serialversionuid, which is necessarily useful. This serialversionuid is used to assist in the serialization and deserialization process, in which the Serialversionuid in the serialized data is only as normal as the serialversionuid of the current class to be deserialized.
Serialversionuid principle: When serialized, the system will write the current class's Serialversionuid (hereafter called UID) into a serialized binary file, and when deserialized, the system detects if the UID in the file is consistent with the current class's UID. Consistency is consistent with the successful deserialization of the description version; Inconsistent description deserialization failed, and the reason for the inconsistency may be that the UID in the class is automatically changed to inconsistent because the UID is not specified and the member variable or property changes.
Specifying the UID manually can minimize the need for the UID to avoid the failure of deserialization to the maximum extent possible, even if the property member variables in the class change.
If the class structure has undergone unconventional changes, for example, the class name was modified, and the type of the member variable was modified, and even though the Serialversionuid validation passed, the deserialization process still failed because the structure of the class changed substantially, and the structure object of a new class could not be restored from the old version.
There are two points to be aware of using serialization:
1. A static member variable belongs to a class that is not part of an object and therefore does not participate in the serialization process
2. member variables marked with the Transient keyword do not participate in the serialization process parcelable
ANDROIDSDK provides: High-performance serialization support intent and bundle use greatly improve the performance of data serialization, and save memory usage. Code Show:
I can see that I've declared two serialized classes from my inserted code picture. And instantiate them, serialize in the first Java class, convert a class into a file, and in deserialization turn into a class, notice that I split into two deserialization, You can see it clearly. Before the second deserialization I instantiated a new Java serialization object and then the original file reverse sequence results are only the initial serialization object, that is, in the original object does not occur when the property changes in the reverse sequence can be successfully executed, And the static variable is very good to see is not support serialization of the implementation of this function; In the serialization of Android, And the member variables have static and transient modifiers, but the serialization of these two keyword modifiers is not supported by the automatic serialization construction method built by Android and by auto-generated methods. I used Pacrel, but I need to be aware that in the construction method, if the Pacrel's corner mark is not the initial angle, the data will be unreadable and even a null pointer exception will appear. If you need to specify the data to instantiate in the construction method, specify the data in the order in which you write the data, If you are matching the initial write order one by one for all data, just specify a corner mark of 0 to begin with. P compared with S serialization, p has improved the scalability of the data for S, but the writing is more complicated and the serialization must be implemented creator this method and must also be specified as public Static final modifier otherwise there will be an exception. SOURCE Download Address