Friday, August 11, 2017

Serializing-Deserializing transient variables manually

Problem statement - We've class Parent with 3 attributes and child class implementing it.
If we make 2 attributes transient , then these 2 attributes won't be part of serialization .
How to make these 2 attributes to be part of serialization and deserilization?

Solution :
Overriding readObject() and writeObject() and manually serializing and deserializing transient variables after default serialization

class Parent implements Serializable {
      
     private static final long serialVersionUID = 1L;
    
     String pFName ="Paras";
     transient String pLName="Chawla";
     transient int adharNo=12345;
}

/* transient fields can't serialized/deserialized ,thus during default serialization , we've to store transient fields manually in physical location.
*Same during deserilization , we've to read transient fields from physical location else they would come as null.
Order of storing & fetching transient fields must be same
*/

class Child extends Parent implements Serializable {
    
     private static final long serialVersionUID = 1L;
     Date date = new Date();

     /*Serializing transient fileds as well by overriding writeObject()*/
     private void writeObject(ObjectOutputStream out) throws IOException {
       
/* Write the non-static and non-transient fields of the current class to this stream*/
        out.defaultWriteObject();
        out.writeObject(this.pLName);
        out.writeInt(this.adharNo);
    }

/* De-Serializing transient fileds as well by overriding writeObject()*/
     private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
       
          /*Read the non-static and non-transient fields of the current class from this stream*/
        in.defaultReadObject();
        this.pLName=(String) in.readObject();
        this.adharNo = (int)in.readInt();
    }

    
}

public class SerializationInheritance {
     public static void main(String[] args) throws IOException, ClassNotFoundException {
          Child child = new Child();
         
          FileOutputStream fos = new FileOutputStream("child.ser");
          ObjectOutputStream oos = new ObjectOutputStream(fos);
          System.out.println("Before Serilization");
          System.out.println("Adhar No :" +child.adharNo + "\nFirst name :" +child.pFName +"\nLast name :"+
          child.pLName + "\nDate : "+child.date);
          oos.writeObject(child);
          oos.close();
         
          FileInputStream fis = new FileInputStream("child.ser");
          ObjectInputStream ois = new ObjectInputStream(fis);
          Child deserializaChild = (Child) ois.readObject();
          System.out.println("\nAfter DeSerilization");
          System.out.println("Adhar No :" +deserializaChild.adharNo + "\nFirst name :" 
          + deserializaChild.pFName + "\nLast name :"deserializaChild.pLName + "\nDate : "
            deserializaChild.date);
          ois.close();
     }
}

Output

Before Serilization
Adhar No :12345
First name :Paras
Last name :Chawla
Date : Fri Aug 11 17:27:40 IST 2017

After DeSerilization
Adhar No :12345
First name :Paras
Last name :Chawla
Date : Fri Aug 11 17:27:40 IST 2017

No comments:

Post a Comment