What is Serialization and De-Serialization? How to implement it?

SERIALIZATION:

  • When we use the normal streams to read and write the data to and from the file, there is a possibility for performing unnecessary read and write operations because of this there is a possibility of loosing the performance of java program.
  • In order to improve the performance of java program by perform single read and write operation irrespective number of fields we must use the concept of Serialization.

DEFINITION: Serialization is a mechanism of saving the state of the entire object at a time on to a file.

Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.

Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.

The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:

public final void writeObject(Object x) throws IOException

The above method serializes an Object and sends it to the output stream. Similarly, the ObjectInputStream class contains the following method for deserializing an object:

public final Object readObject() throws IOException, 
                                 ClassNotFoundException

This method retrieves the next Object out of the stream and deserializes it. The return value is Object, so you will need to cast it to its appropriate data type.

  • Steps for developing serializable subclass:
    • Choose the appropriate package name.
    • Choose the appropriate user defined serializable sub class name.
    • Whichever class we have chosen in previous step must be implementing a predefined interface call java.io.Serializable. Serializable interface does not contain any abstract method i.e. Marker/Marked/Tagged interface.
    • Choose the set of data members in serializable sub class.
    • Choose the set of setR() methods for each and every data member of the serializable sub class. The set of setR() methods are also known as mutators or modifiers whose general form is as follows:
public void setXXX (data-type variable)

{

}

Where XXX represents the data member of serializable sub class

  • Choose the set of getR() methods for each and every data member of the serializable sub class. The set of getR() methods are also known as inspectors whose general form is as follows:
public data-type getXXX()

{

}

Where XXX represents the data member of the serializable sub class

Note: If any class is containing set of getR() and setR() methods then that class is known as javaBean or POJO(Plain Old Java Object) class and such type of programming is known as Component Based Style Programming.

 

To demonstrate how serialization works in Java, I am going to use the Employee class that we discussed early on in the book. Suppose that we have the following Employee class, which implements the Serializable interface:

public class Employee implements java.io.Serializable
{
   public String name;
   public String address;
   public transient int SSN;
   public int number;
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + name
                           + " " + address);
   }
}

Notice that for a class to be serialized successfully, two conditions must be met:

  • The class must implement the java.io.Serializable interface.
  • All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

If you are curious to know if a Java Standard Class is serializable or not, check the documentation for the class. The test is simple: If the class implements java.io.Serializable, then it is serializable; otherwise, it's not.

Serializing an Object:

The ObjectOutputStream class is used to serialize an Object. The following SerializeDemo program instantiates an Employee object and serializes it to a file.

When the program is done executing, a file named employee.ser is created. The program does not generate any output, but study the code and try to determine what the program is doing.

Note: When serializing an object to a file, the standard convention in Java is to give the file a.ser extension.

 

import java.io.*;

public class SerializeDemo
{
   public static void main(String [] args)
   {
      Employee e = new Employee();
      e.name = "Mahima";
      e.address = "Indore";
      e.SSN = 11122333;
      e.number = 101;
      try
      {
         FileOutputStream fileOut =
         new FileOutputStream("employee.txt");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in employee.txt");
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }
}

DESERIALIZATION:

  • It is a mechanism of retrieving the records from the file into the main memory of the computer or it is the ability of retrieving one stream object into the main memory of the computer is known as Deserialization.

STEPS FOR DESERIALIZATION PROCESS:

  • Create an object of serializable subclass.
  • Choose the file name and open it into read mode with the help of FileInputStream class.
  • An object of FileInputStream class will not read the entire record at once hence it is recommended to create an object of ObjectInputStream class. ObjectInputStream class contains the following constructor which takes an object of FileInputstream class as a parameter.

ObjectInputStream (FileInputStream)

 

  • ObjectInputStream class contains the following method which will read the entire object at a time from the file and it returns an object of Object class.
public Object readObject();
  • Type cast the object of Object class into corresponding serializable subclass object for calling specific methods of serializable subclass.
  • Call set of getR() to get the data from serializable subclass object.
  • Close the file which is opened in read mode and close all stream.

The following DeserializeDemo program deserializes the Employee object created in the SerializeDemo program. Study the program and try to determine its output:

import java.io.*;
public class DeserializeDemo
{
   public static void main(String [] args)
   {
      Employee e = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("employee.txt");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
    }
}

This would produce the following result:

Deserialized Employee...
Name: Mahima
Address:Indore
SSN: 0
Number:101

Here are following important points to be noted:
• The try/catch block tries to catch a ClassNotFoundException, which is declared by the readObject() method. For a JVM to be able to deserialize an object, it must be able to find the bytecode for the class. If the JVM can't find a class during the deserialization of an object, it throws a ClassNotFoundException.
• Notice that the return value of readObject() is cast to an Employee reference.
• The value of the SSN field was 11122333 when the object was serialized, but because the field is transient, this value was not sent to the output stream. The SSN field of the deserialized Employee object is 0.

Leave a Reply

Your email address will not be published. Required fields are marked *