Serialization in Java

In this post, we will discuss about Serialization in Java with the help of Serializable Interface and Apache commons Lang’s SerializationUtils Class.


 

As seen in previous post, Deep Copy using Object.clone() is very tedious to implement, error-prone and difficult to maintain. Also, Object.clone() method will not work if we try assign a value to a final field.

One solution to address these issues is to use Java Object Serialization (JOS). Serialization is the process of converting an object into a sequence of bytes and rebuilding those bytes later into a new object. In other words, Serialization is used to persist an object.

 

1. Serializable Interface

Java provides automatic serialization which requires that the object be marked by implementing the java.io.Serializable interface. Implementing the interface marks the class as “okay to serialize”, and Java then handles serialization internally.

There are no serialization methods defined on the Serializable interface, but we can use ObjectOutputStream.writeObject() method to convert the object into a serialized form, and a corresponding ObjectInputStream.readObject() method to recreate an object from that representation. The result will be a completely distinct object, with completely distinct referenced objects.

Java

Download   Run Code

Output:

[John Snow, 25, [Science, Maths, English, History]]

Well this approach also has some limitations and issues:

  1. It requires the object being copied and all its objects references to be serializable. So all involved classes should implement java.io.Serializable interface.
     
  2. Serialization will not work on transient fields.
     
  3. There will be vast difference in the performance as compared to using Object.clone(). A deep copy requires both serializing and deserializing which are very time-consuming and the Java’s byte array stream implementation is also slow.
     
  4. We know that Singleton pattern restricts the instantiation of a class to one object i.e. a class needs to have only one instance. But Serialization can break this contract. The new object created by Serialization will not be unique. To get around this undesired behavior, we can use the readResolve() method to enforce singletons. The readResolve method is called after ObjectInputStream has read an object from the stream and is preparing to return it to the caller. It basically replaces the object with the singleton instance (if any).

 

2. Apache Commons Lang – SerializationUtils

We can also use SerializationUtils class provided by Apache Commons Lang to assist with the serialization process. It provides below methods to help with serialization and deserialization –

  1. serialize(): Serializes an Object to a byte[] array. It takes the object to be serialized as input and returns a byte[] array.
     
  2. deserialize(): Deserializes a single Object from an array of bytes. It takes the serialized object as input (which must not be null) and returns the deserialized object.

SerializationUtils requires the object being copied and all its objects references to implement java.io.Serializable interface and it won’t work on transient fields.

Java

Download

Output:

[John Snow, 25, [Science, Maths, English, History]]

 
Serialization of Java Objects using Google’s GSON Library

Serialization of Java Objects using Jackson Library

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz