Posts

Showing posts with the label Serialization in Java

Customize serialization in Java

Image
How do I create my own serialization protocol? To create your own serialization protocol, it is enough to implement the Externalizable interface, which contains two methods: public void writeExternal(ObjectOutput out) throws IOException; public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException; serialVersionUID field in serialization serialVersionUID is used to indicate the version of the serialized data. When we don't explicitly declare the serialVersionUID in our class, the Java runtime does it for us, but the process is sensitive to many of the class's metadata, including the number of fields, the type of the fields, the access modifiers of the fields, the interfaces that the class implements, etc. It is recommended to explicitly declare serialVersionUID since when adding or removing class attributes, the dynamically generated value may change and an InvalidClassException will be thrown at the time of execution. private static fina...

Externalizable in Java

Image
To change the default serialization/deserialization behavior you can implement the java.io.Externalizable interface that allows custom serialization logic to be applied. The way to serialize and deserialize is described in the writeExternal() and readExternal() methods. During deserialization, the constructor without parameters is called, and then the readExternal method is called on the created object. If a serializable object has one of the following methods implemented, then the serialization mechanism will use it, rather than the default method: writeObject() - writing an object to a stream readObject() - reading an object from the stream writeReplace() - allows you to replace yourself with an instance of another class before writing readResolve() - allows you to replace another object with itself after reading Exclude fields from serialization The transient keyword can be used to control serialization when defining fields, thus excluding the fields from the general serial...

Serialization in Java

Image
Serialization is the process of converting a data structure into a linear sequence of bytes for further transmission or storage. The serialized objects can then be restored (deserialized). In Java, according to the Java Object Serialization specification, there are two standard serialization methods: standard serialization, using the java.io.Serializable interface, and "extended" serialization - java.io.Externalizable. Serialization allows you to change the class within certain limits. Here are the most important changes that the Java Object Serialization specification can handle automatically: adding new fields to the class changing fields from static to non-static changing fields from transit to non-transit Reverse changes (from non-static to static fields and from non-transit to transit) or deletion of fields require some additional processing depending on how much backward compatibility is required. Serialization/deserialization process using Serializable When u...