Monday, August 1, 2011

Customized Object Serialization/De Serialization in Java

Customized Object Serialization/De Serialization in Java

Following describes how to serialize or de serialize the object in java
 its also known as marshalling and un marshalling java object in a customized way

When weuse Serializable interface, ourclass is serialized automatically by default.
But  we can override writeObject() and readObject() two methods to control more complex object serailization process.

When we use Externalizable interface, you have a complete control over your class's serialization process. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.

Now am going to show simple way of serializing the java object

Model object : Person.java


/**
 *
 */
package com.vels.test;
import java.io.Serializable;

/**
 * @author velmurugan pousel
 * created on Aug 1, 2011 for test
 */

public class Person implements Serializable {

 private String name;
 private String id;
 private int age;


 public Person(int age, String name, String id){
    this.name = name;
    this.age = age;
    this.id = id;
 }

 /**
  * @return the name
  */
 public String getName() {
  return name;
 }

 /**
  * @param name the name to set
  */
 public void setName(String name) {
  this.name = name;
 }

 /**
  * @return the id
  */
 public String getId() {
  return id;
 }

 /**
  * @param id the id to set
  */
 public void setId(String id) {
  this.id = id;
 }

 /**
  * @return the age
  */
 public int getAge() {
  return age;
 }

 /**
  * @param age the age to set
  */
 public void setAge(int age) {
  this.age = age;
 }

}
Main class :


/**
 *
 */
package com.vels.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.sun.corba.se.spi.ior.ObjectId;

/**
 * @author velmurugan pousel
 * created on Aug 1, 2011 for test
 */

public class SerializationTest {

 public static void main(String[] args) {

  try {

   String path = "d:/doc/person.txt";
  
   System.out.println(" ######### BEFORE SERIALIZATION ########## ");

   // serialize the Person object
   Person p = new Person(12,"raj","111");
   serializeObject(path, p);

   System.out.println(" #########  SERIALIZATION DONE ########## ");

  
   System.out.println(" ######### BEFORE DE SERIALIZATION ########## ");
   //de serialize the person

   Person p1 = (Person) deSerializeObject(path); 
   System.out.println(" name...." + p1.getName());
   System.out.println(" id...." + p1.getName());
   System.out.println(" ######### DE SERIALIZATION DONE ########## ");
  
  } catch (Throwable e) {
   e.printStackTrace();
  }
 
 }

 /**
  * to serialise the object to the given path
  * @param path- name of the file where the object going to be serialized
  * @param objToSerialize
  */
 public static void serializeObject(String path, Object objToSerialize) {

  try {
   FileOutputStream fos = new FileOutputStream(new File(path));
   ObjectOutputStream obj = new ObjectOutputStream(fos);
   obj.writeObject(objToSerialize);
  
   fos.flush();
   obj.close();
  } catch (Throwable e) {
   e.printStackTrace();
  }

 }

 /**
  * to de serialize the object from the given file where its serialized
  *
  * @param path
  * @return
  */

 public static Object deSerializeObject(String path) {

  Object obj = null;
  try {
   ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
   obj = in.readObject();
  } catch (Throwable e) {
   e.printStackTrace();
  }
  return obj;
 }

}
Output :

######### BEFORE SERIALIZATION ##########
######### SERIALIZATION DONE ##########
######### BEFORE DE SERIALIZATION ##########
name....raj
id....raj
######### DE SERIALIZATION DONE ##########

No comments:

Post a Comment