Serialization – a Sci Fi story
Short story about serialization
After hard work of many years, Earth’s scientist developed a robot who can help them in daily work. But this robot was less featured than the robots which were developed by the scientist of Mars planet.
After a meeting between both planet’s scientist, it is decided that mars will send their robots to earth. But a problem occurred. The cost of sending 100 robots to earth was $100 millions. And it takes around 60 days for traveling.
Finally, Mar’s scientist decided to share their secret with Earth’s scientists. This secret was about the structure of class/robot. Earth’s scientists developed the same structure on earth itself. Mar’s scientists serialized the data of each robot and send it to earth. Earth’s scientists deserialized the data and fed it into each robot accordingly.
This process saved the time in communicating mass amount of data.
Some of the robots were being used in some defensive work on Mars. So their scientists marked some crucial properties of those robots as transient before sending their data to Earth. Note that transient property is set to null(in case of reference) or to default value(in case of primitive type) when the object gets deserialized.
One more point which was noticed by Earth’s scientists is that Mars’s scientists ask them to create some static variables to keep environmental detail. This detail is used by some robots. But Mars’s scientists dint share this detail since the environment on earth was different than Mars environment.
Even though knowing about the robot class structure and having serialized data Earth’s scientist were not able to deserialize the data which can make robots working.
Exception in thread "main" java.io.InvalidClassException: SerializeMe; local class incompatible: stream classdesc :
Mars’s scientists were waiting for the complete payment. Once the payment was done Mars’s scientists shared the serialVersionUID with Earth’s scientists. Earth’s scientist set it to robot class and everything became fine.
One of the Mar’s Scientist: Earth’s scientists are very slow to deserialize some of our best Robots. They must be using old version of java. They should either switch to new java version or to implement Externalizable.
Terms highlighted in above example are:
- Serialization
- Deserialization
- Transient variables
- Static variables
- Externalizable
Technically
Serialization is nothing but storing current state of an object to physical file. So the data can be used later by deserializing it.
When it is required:
- When a system gets crashed.
- When a player want to save all played game data so he can continue playing later.
- When a process need to be stopped by some reason. But it must continue working the next time with the same data.
How to
a. Implements Serializable or Externalizable
b. Serialize:
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("fileName")); out.writeObject(obj);
c. Deserialize:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("fileName")); in.readObject();
1. Object of class A can’t be deserialized to the object of class B.
2. Object of class A can’t be deserialized to the object of class A itself if any of the instance variable changes its nature. For example
- Name
- Data type
- Removed from the class
- Make it transient or static
But if serialVersionUID is added to the class of serialized and deserialized object with same value then deserialization will not generate any error (excluding point b). So if variable is not found then its value is not set.
In short, deserialization finds instance variable with the same name they were used to serialize. If variable name is changed or they are removed from the class then it throws error. But if serialVersionUID is same then it skips initialization for not found variables.
In addition, if a variable is found with the same name but its datatype is changed then it generate error of casting even if serialVersionUID is same.
Changing their access level doesn’t impact.
3. If serialVersionUID is not same for the class of serialized and deserialized object then deserialization throws error even if class structure are exact same.
4. Methods, transient variables and static variables are not transient.
5. An object can be serialized only if its class implements either Serializable or Externalizable.
6. If extra variables found than the variables which are serialized then they are set to their default value at the time deserialization.
7. Making a class part of inheritance tree is allowed. But moving a class in existing inheritance tree up/down is not allowed since it can affect saved/serialized object tree.
Why Externalizable
- It provides control of reading/writing object to/from an output/input stream.
- It was very useful till Java 1.2 since reflection was slow
- You can decide which object has to left from object tree or what default value has to be set to transient variables while deserialization.
Methods of Externalizable
public void writeExternal(ObjectOutput out) throws IOException { out.writeFloat(x); out.writeFloat(y); out.writeFloat(z); out.writeFloat(w); } public void readExternal(ObjectInput in) throws IOException { x = in.readFloat(); y = in.readFloat(); z = in.readFloat(); w = in.readFloat(); }
Sample Code
I am attaching source code, I practiced on, for serialization and externalization.