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

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!

Vuze

                                Most Powerful Open Source Bit-torrent Clint Vuze Logo              Are feeling tricky in spelling the word "Vuze"?      Vuze is a new bit-torrent software came from the lap of Open Source community. It's was coded in Java and runs with Azureus Engine.   So we can also call it as Azureus Bit-torrent Clint. The features of Azureus i mean Vuze are: Nice User Interface Meta Search from Search Box You can subscribe to the content that you like Very effective download It has in-built HD player so that you can watch movies while downloading with great quality. The HD Player can support AVI, XV ID,Quick time and so on formats. You can make your file that can be played Devices such as android powered devices or iPhone. With Vuze Remote we can control our Vuze Clint from any computer or with your smart phone. You can your files to a DVD with Vuze. For More details follow this link: Features To Download this Buddy follow this l