Skip to main content

What is Serialization?

Serialization or Marshalling: 
                      It is the process of converting data structure or object into Network or File or Database Supporting form.

Deserialization or Unmarshalling:
                      It is the opposite process to Serialization. It is the process of extracting object or data structure from Network or File or Database Supporting form.


You may feel tough to understand these definitions. I'll explain you through an example.

                Once upon a time there was a son staying in Hyderabad and his family staying in a village which is very far from Hyderabad, it is famous for good Mangos. One day Son made a phone call to Father and asked that, Father, i want 10KG mangos. So, father went to mangos shop and brought mangos to home. The challenge in front him is how to send these mangos to his son. So, he went to courier office and asked them to send mangos to Hyderabad, then

Courier Officer : Sorry, sir. It is not Possible to send these mangos.

Father: Why? This is not illegal according to Courier Rules.

Courier Officer : Yes, sir but these are not in courier transferable form.

Father: What to do now?

Courier Officer : Don't worry sir, just pay a little extra money we'll pack and send them safely.

Father paid extra money. Courier people packed the mangos and sent Son as a pack. After receiving pack son removed the package on mangos and enjoyed Mangos.

Serialization Example
Sending Magos as a pack to Son

In the same way, we can't send Objects or Data Structures directly in a Network or we can't store in database or file. We need to pack or convert them as we did with mangos. This process is called Serialization. Exact opposite process is called Deserialization. In serialization, we convert Object to byte-stream and then we'll send across the network or store in database or file. After that in deserialization, we'll fist get the byte-stream and then we'll extract Object from byte-stream.
Sending the Object through Network


Uses of Serialization:

  • Serialization plays important role in communication or two systems in a network
  • Serialization can be used to store the state of Object
  • If you want to deep copy a object and don't want to write clone method, you can use Serialization.
  • Instead of loading big object into memory, you can cache the Object using serialization to a file such that you can reduce the startup time of application.

Now we'll see the Serialization programmatically.

Java:

 import java.io.*;  
 class Student implements Serializable {  
      String name = "Chaitanya";  
      int rno = 525;  
 }  
 public class SerializationDemo {  
      public static void main(String args[]) throws Exception {  
           Student s = new Student();  
           System.out.println(s.name+"-----"+s.rno);  
           //Serialization of Student Ojbect  
           FileOutputStream fos = new FileOutputStream("abc.ser");  
           ObjectOutputStream oos = new ObjectOutputStream(fos);  
           oos.writeObject(s);  
           //Desialization of Student Object  
           FileInputStream fis = new FileInputStream("abc.ser");  
           ObjectInputStream ois = new ObjectInputStream(fis);  
           Student s1 = (Student) ois.readObject();  
           System.out.println(s1.name+"-----"+s1.rno);  
      }  
 }  

Comments

Popular posts from this blog

Flagfox Add-on for Firefox

                                                                                                                                     Flagfox Add-on       Hi, friends!                Sometimes we want to know the websites location where they are hosted and some time their IP address. Sometimes we may open sites which may have malicious code unexpectedly which may cause a virus attack, then how to know whether a site is secure or not? Sometimes we may want to find similar sites. Sometimes we may share long URLs with our friends, but seeing long URLs may irritate your friends then you have to shorten them. But how? For doing all these things there is an Add-on for Firefox namely "Flagfox" .

Customize Your FB Background

                      Facebook With Your Background Hi, friends!! Are you bored by seeing your FB default background? It is time to change your FB background!