[Java study notes (117)] Introduction to the object serialization mechanism

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. Object serialization

(I. Overview

       It is efficient to store the same type of data in a fixed-length record format, but for the storage object type, the data format required by the object cannot be determined, so we use the object serialization mechanism to write out any object to the output stream , And it can be read back.

(2) Save and load serialized objects

       In order to save the object data, you must first open an ObjectOutputStream object, and save the object to the output stream through the writeObject method. The ObjectOutputStream class browses all fields of the object and stores their content, as shown below:

var out = new ObjectOutputStream(new FileOutputStream(“a.dat”));
var harry = new Employe(“harry”, 1);
out.writeObject(harry);

       At the same time, what needs to be modified is that the class that wants to store or restore in the object output stream must implement the Serializable interface. This interface has no methods, but a marker interface, which can be serialized:

class Employee implements Serializable{
    
    }

       Each object is saved with a serial number. This serial number is unique. When the object is encountered for the first time, the object is saved to the output stream. If the object has been saved before, it is directly referenced. The serial number of the object is sufficient. Another application of serial numbers is to transfer a collection of objects to another computer via a network.

(3) Serialized file format

       Object serialization is to store object data in a special file format, which is too complicated. As long as you know that it is to sort the object's class, superclass, interface, domain type and method signature in a standardized way, then apply the secure hash (SHA) algorithm The fingerprint obtained from these data, namely the serial number, is the unique ID of the serialized version.


(4) Modify the default serialization mechanism

       Certain data fields cannot be serialized, such as object information that is only meaningful in local methods. In order to prevent such fields from being serialized in Java, they can be marked as transient. If these fields belong to non-serializable classes, Also mark them as transient.

       At the same time, a single class can also modify the default serialization mechanism and customize the readObject and writeObject methods to replace the default serialization method.

(5) Serialized singleton and type-safe enumeration

       For singleton and type-safe enumerations, the default serialization mechanism is not applicable. It will create a new object that is inconsistent with the previously existing object. In order to solve this problem, you can define another special type called readResolve The serialization method returns the appropriate object.


(6) Version management

       Using serialization to save the object, the object may change, its SHA fingerprint will also change, and the object input stream will refuse to read objects with different fingerprints. But the class can indicate that it is compatible with the earlier version. To achieve this effect, you must first obtain the fingerprint of the earlier version of the class. We can use the program serialver in the JDK to get this number, as shown below:

serialver Employee

Will print out:

Employee: static final long serialVersionUID = -1814239825517340645L;

       All newer versions of this class must define the serialVersionUID constant to be the same as the fingerprint of the original version, so that it can be compatible with earlier versions and can read different versions of this class, as shown below:

class Employee implements Serializable{
    
    
	public static final long serialVersionUID = -1814239825517340645L;
}

       Just reading the version of the object does not mean anything. If the object is just a method changed, there will be no problem when reading the new object data, and if the data has changed, there will be a problem. The object input stream compares the data field of the current version of this class with the data field of the serialized version. Only the non-transient and non-static data fields are considered. If there is a gap between the two data fields If the name matches but the type does not match, the object input stream will not try to convert one type to another because the two objects are incompatible; if there are data fields that the current version of the object does not have, these additional data are ignored; If the current version of the object adds some new data, set it to the default value (the object is set to null, the number is set to 0, and the boolean is set to false).

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/113358404