Tuesday, 10 June 2014

Serialization and Deserialization in java


Serialization is a process of  converting  object into a sequences of byte which can be written to disk or database or sent over network to any other running JVM. The reverse process of creating object from sequences of byte is called Deserialization.  
A class must implement the Serializable interface present in java.io package in order to serialize its object successfully. 
Java provides Serialization API encapsulated under java.io package for serializing and deserializing object includes which
 
·      java.io.Serializable
·      java.io.Externalizable
·      ObjectInputStream and 
·      ObjectOutputStream etc.

Signature of writeObject() and readObject()

writeObject() method of ObjectOutputStream class serializes an Object and sends it to the output stream.

·        public final void writeObject(Object x) throws IOException

readObject() method of ObjectInputStream class retreives Object out of the stream and deserializes it.

·        public final Object readObject() throws IOException, ClassNotFoundException

                 While serializing if you don't want any field to be part of object's state then declare it either static or transient based on your need and it will not be included during Java serialization process.



Serializing an Object Example
package com.naresh.io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class StudentInfo implements Serializable {

      private static final long serialVersionUID = 1L;
      public String name;
      public int id;
      public static String contact;

      StudentInfo(String a, int n, String u) {
            name = a;
            id = n;
            contact = u;
      }
}

public class TestSerialization {

      public static void main(String... mnb) {

            StudentInfo smp = new StudentInfo("Naresh", 30, "9848012345");
            try {
                  FileOutputStream fos = new FileOutputStream("C:\\Studentinfo.ser");
                  ObjectOutputStream oos = new ObjectOutputStream(fos);

                  /*
                   * object of studentinfo class is serialized using writeObject()
                   * method and written to Studentinfo.ser file
                   */
                  oos.writeObject(smp);
                  System.out.println("Object Serialized Successfully!");
                  oos.close();
                  fos.close();
            } catch (Exception e) {
                  System.out.println("Exception" + e);
            }
      }
}


Deserialization of StudentInfo class Object
package com.naresh.io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class TestDeserilization {

      static public void main(String... mnb) {
            StudentInfo st = null;

            try {
                  /* Open file Studentinfo.ser in read mode */

FileInputStream fis = new FileInputStream("C:\\Studentinfo.ser");
                  ObjectInputStream ois = new ObjectInputStream(fis);

                  /* Object is deserializes using readObject() method */
                  st = (StudentInfo) ois.readObject();

            } catch (Exception e) {
                  System.out.println("Exception" + e);
            }

            System.out.println(st.name);
            System.out.println(st.id);
            System.out.println(st.contact);

      }

}

Output: Naresh
        30
        null 

contact value is null because we have declare contact field as static in StudentInfo class.

Note:  static members are never serialized because they are connected to class not object of class

transient keyword
while  serializing an object , if we don’t  want  certain data member  of  the object to be serialized we can mention it transient.transient keyword  will prevent that data member  from being serialized.

class StudentInfo implements Serializable {

      public String name;
      transient int id;
      public static String contact;
}

·         In this example id will not be serialized because it is transient and contact will not be serialized because it is static.




No comments:

Post a Comment