Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaObject Graph in Java Serialization

Object Graph in Java Serialization

Most of you must be aware with the Serialization involving single object but have you ever thought what if the object has reference to other objects too. Will it be serialized? Will reference objects will be serialized?
Answer to these queries is yes, you don’t have to explicitly serialize reference objects. Let’s find out how this can be achieved.

What is Object Graph?
An Object Graph is the set of objects which will be serialized automatically, if the object which contains reference to them is serialized.
In other words, we can say that when we serialize any object and if it contains any other object reference then JVM serializes the object and as well as its object references.
Lets make it more clear with the help of a simple example.




// Java program to demonstrate how serializing 
// an object serializes other reference objects.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
  
// Class Serial1 contains reference to object 
// of class Serial2. 
class Serial1 implements Serializable {
     Serial2 s2 = new Serial2(); 
}
  
// Class Serial2 contains reference to object 
// of class Serial3. 
class Serial2 implements Serializable {
     Serial3 s3 = new Serial3(); 
}
  
// A reference of this class is present in Serial2
class Serial3 implements Serializable {
     int i = 10;
     int j = 20;
}
  
class DemoSerialize {
  
     public static void main(String args[]) throws Exception {
  
     // Creating object of class Serial1
     Serial1 s1 = new Serial1();
  
     // Serializing object of class Serial1
     // Saving object in file
     FileOutputStream fos = new FileOutputStream("abc.ser");
     ObjectOutputStream oos = new ObjectOutputStream(fos);
  
     // Method for serializing object of class Serial1
     oos.writeObject(s1);
  
     // Close streams once serialization is done
     fos.close();
     oos.close();
  
     // De-Serializing object of class Serial1
  
     // Reading object from file
     FileInputStream fis = new FileInputStream("abc.ser");
     ObjectInputStream ois = new ObjectInputStream(fis);
  
     // Method for de-serializing object of class Serial1     
     Serial1 serobject = (Serial1) ois.readObject();
  
     // Close streams once de-serialization is done
     fis.close();
     ois.close();
  
     // Printing values of i and j after Serialization
     System.out.println("Value of i after Serialization"
                               " is " + serobject.s2.s3.i);
     System.out.println("Value of j after Serialization"
                                " is "+serobject.s2.s3.j);
  }
}


Output:

Value of i after Serialization is 10
Value of j after Serialization is 20

Note: In object graph every object should be serializable. If at-least one object is not serializable then we will get runtime execution saying NonSerializableExecution.

This article is contributed by Somya Garg. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

RELATED ARTICLES

Most Popular

Recent Comments