Serialization

Serialization is the process of converting in-memory objects to bytestream for either storing the data in persistent storage (like filesystem) or passing the data across network (as in RPC calls). De-serialization is the reverse process of converting the bytestream back to Java in-memory objects. Serialization / De-serialization is frequently involved in distributed computing technologies like RMI , EJB and nowadays in Hadoop eco system. This blog discusses about Java’s default serialization and its limitations and then I have another blog which discusses the alternative options to Java’s default serialization.

Java’s default serialization

One can mark an object to be serializable just by just implementing the marker Serializable interface. That’s it and the developer does not need to do anything else! This single declaration will ensure that Java’s default serialization will kick in once you want to serialize the object.

Following is some code snippet for Java’s default serializatiopn technique:

// Serialization (Converting an in-memory object to serialized state) -  
// This assumes that Object1's class implements Serializable interface
ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream("x.ser")); 
os.writeObject(Object1);

// De-Serialization (Converting a serialized state to in-memory object) -   
ObjectInputStream os = new ObjectInputStream (new FileInputStream("x.ser")); 
os.readObject();

Following are some salient aspects of Java’s default serialization:

Following are the acceptable changes for Serialization -

Following are the un-acceptable changes for Serialization -

Although implementing Java’s default serialization is a breeze (by simply implementing one marker interface), following are several disadvantages of the default serialization:

So these are some of the reasons for which one can consider alternatives to Java’s default serialization. Will be discussed in another blog.