Java serialization is the conversion of an object into a string of binary representations of byte arrays, which are persisted by saving or transferring the byte data. To persist, the object must implement the Java.io.Serializable interface. Deserialization is the opposite process of re-structuring the byte array into an object. When deserializing, it is necessary to have the original class as a template in order to restore this object, from which we can guess that the serialized data does not hold the complete structure information of the class like the class file. So what exactly does the serialized data contain?
Although the serialization of Java can guarantee the persistence of object state, it is difficult to deal with some complex object structures, the following is a summary of some complex object situations:
- When the parent class implements the Serializable interface, all subclasses can be serialized
- Subclasses implement the Serializable interface, the parent class does not, the attributes in the parent class cannot be serialized (without error, but the data is lost), but the attributes are still serialized correctly in the subclass.
- If the serialized property is an object, the object must also implement the Serialiazable interface, otherwise it will error.
- When deserializing, if the properties of an object are modified or truncated, the modified part of the property is lost but does not cause an error.
- When deserializing, if Serialversionuid is modified, the deserialization fails.
Java serialization works well in a pure Java environment, but in a multi-lingual environment, it is difficult to restore results in other languages after the storage is serialized in Java. In this case, as far as possible to store common data structures, such as JSON or XML structure data, there are better serialization tools, such as Google's protobuf.
Java Serialization Technology