This post will discuss serialization in Java with the help of Serializable interface and Apache Commons Lang’s SerializationUtils class.

As seen in the previous post, Deep Copy using Object.clone() is very tedious to implement, error-prone, and difficult to maintain. Also, the Object.clone() method will not work if we try to 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.

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 object references to be serializable. So all involved classes should implement the 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 Java’s byte array stream implementation is also slow.
  4. We know that the Singleton pattern restricts a class’s instantiation 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. Using Apache Commons Lang

We can also use the SerializationUtils class provided by Apache Commons Lang to assist with the serialization process. It provides the following 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 the java.io.Serializable interface, and it won’t work on transient fields.

Download Code

Output:

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

That’s all about serialization in Java.

 
Also See:

Serialization of Java objects using Google’s Gson library

Serialization of Java objects using Jackson Library