Learnitweb

Object graph in Serialization

It is possible that an object references(uses) other objects as well. When we serialize an object, all objects which are referenced by the serialized object are also serialized. This group of objects is Object Graph.

In object graph every object should be serializable. If any of the object is not serializable then we’ll get NotSerializableException.

In the following example, we have created a Student object. Student object references an Address object and Address object references a City object. In serialization, we are serializing Student object. Since Student object references Address and City objects, Address and City objects will also be serialized as these make an object graph.

In below code, we can verify while deserialization that Address and City Objects are also serialized.

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

class Student implements Serializable{
	private String name="John";
	private Address address = new Address();
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
}

class Address implements Serializable{
	private String pinCode = "101";
	private City city = new City();
	public String getPinCode() {
		return pinCode;
	}
	public void setPinCode(String pinCode) {
		this.pinCode = pinCode;
	}
	public City getCity() {
		return city;
	}
	public void setCity(City city) {
		this.city = city;
	}
}

class City implements Serializable{
	private String city="London";

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}	
}

class SerializationExample {
	public static void main(String args[]) throws Exception {
		Student student = new Student();
		
		System.out.println("Serialization started");
		FileOutputStream fos = new FileOutputStream("test.ser");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(student);
		System.out.println("Serialization ended");
		
		System.out.println("Deserialization started");
		FileInputStream fis = new FileInputStream("test.ser");
		ObjectInputStream ois = new ObjectInputStream(fis);
		
		Student stu = (Student) ois.readObject( );
		System.out.println("Deserialization ended");
		
		System.out.println("Student name: " + stu.getName());
		System.out.println("Student pincode: " + stu.getAddress().getPinCode());
		System.out.println("Student city: " + stu.getAddress().getCity().getCity());
		
	}
}

Output

Serialization started
Serialization ended
Deserialization started
Deserialization ended
Student name: John
Student pincode: 101
Student city: London

Now suppose if Address class doesn’t implement Serializable, then we’ll get something like this:

Serialization started
Exception in thread "main" java.io.NotSerializableException: Address
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
  at SerializationExample.main(SerializationExample.java:60) 

So all objects in object graph should be serializable. If not, then we’ll get NotSerializableException.