PART - 2 : What is Serialization ?

Serialization is a general concept in Computer Science. Serialization is the process of converting objects into a format that can be stored in a file, memory or transmitted over the network and reconstructed in the same or similar environment. Serialization can be used to make a deep clone of an object. The process of Serialization is also known as marshaling and the process of De-serialization is also known as Un-marshaling.

Java provides a mechanism of serialization which requires the class to be marked by implementing the java.io.Serializable interface or java.io.Externalizable interface. Serializable is a marker interface, meaning it does not contain any method or variable. Implementing Serializable tells Java that it is “okay to serialize the object". Now, before we delve any further into this topic, it is advisable to have some idea about Java I/O as we shall be using that here.

The java.io.* classes that we will use here are:

java.io.Filejava.io.FileInputStream 
java.io.FileOutputStream
java.io.ObjectInputStream
java.io.ObjectOutputStream
The high level classes used for Serialization in Java are ObjectInputStream and ObjectOutputStream.These classes are wrapped around lower level classes like FileInputStream and FileOutputStream (an example of Decorator design pattern).

Now, lets take a look at the methods provided by these classes for Serialization and de-serialization and an example.
ObjectOutputStream.writeObject() //serialize 
ObjectInputStream.readObject() //deserialize
Lets assume we have a class Vehicle:
import java.io.Serializable;
public class Vehicle implements Serializable{
private Integer vehicleId ;
private String vehicleName ;
public Vehicle() {
super();
this.vehicleId = 1;
this.vehicleName = "Car";
}
public Integer getVehicleId() {
return vehicleId ;
}
public void setVehicleId(Integer vehicleId) {
this.vehicleId = vehicleId;
}
public String getVehicleName() {
return vehicleName ;
}
public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
@Override public String toString() {
return "Vehicle [vehicleId=" + vehicleId + ", vehicleName="
+ vehicleName + "]";
}
}
Here is a program that serializes it and then de-serializes it :
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeDemo
{
public static void serialize() throws IOException {
Vehicle vehicle = new Vehicle();
System.out.println("After creation:=" + vehicle);
vehicle.setVehicleId(2);
vehicle.setVehicleName("Bike");
System.out.println("Before serialization:=" + vehicle);
FileOutputStream fos = new FileOutputStream("serial.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(vehicle);
oos.close();
}
public static void deSerialize() throws IOException,ClassNotFoundException {
FileInputStream fis = new FileInputStream("serial.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Vehicle vehicle = (Vehicle) ois.readObject();
System.out.println("After de-serialization:=" + vehicle);
ois.close();
}
// Our Client code which is serializing and deserializing the object.
public static void main(String[] args) {
try {
serialize();
deSerialize();
} catch (ClassNotFoundException ce) {
ce.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
}
}
Now, lets take a look at what is happening here :

   1.  We declare a class Vehicle that implements the Serializable interface.
   2.  We create an object of the class by one set of values and then set another set of values.
   3.  We create a FileOutputStream object which itself is then wrapped in an ObjectOutputStream object (this is an example of use of Decorator pattern).
   4. We use the writeObject() method of ObjectOutputStream which does two actions, first serialize the object and second, write the serialized object to the file.
   5. We then deserialize using more or less similar process.The readObject() method of ObjectInputStream object is used to deserialize which returns an object.

Now, lets take a look at the output of the program.

After creation:=Vehicle [vehicleId=1, vehicleName=Car]
Before serialization:=Vehicle [vehicleId=2, vehicleName=Bike]
After de-serialization:=Vehicle [vehicleId=2, vehicleName=Bike]

We notice that serialization persists the state of the modified object and after de-serialization we get the same object instead of a newly created one.

CORE JAVA SERIALIZATION