Friday 26 August 2016

Can I customize serialization process?

Yes, you can customize the serialization process by defining the following methods in your modal class.

private void writeObject(java.io.ObjectOutputStream stream) throws IOException
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException

For example, in Employee.java, I defined above methods like below.

private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
         stream.writeObject(id);
         stream.writeObject("Mr." + firstName);
         stream.writeObject("Mr." + lastName);
}

private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
         id = (int) stream.readObject();
         firstName = (String) stream.readObject();
         lastName = (String) stream.readObject();
}

As you see writeObject method, it prepends the string "Mr." to the firstName and lastName of Employee instance. So while serializing, instead of serializing the exact data, JVM serialize by prepending the string ‘Mr.’ to firstName and lastName.

Following is the complete working application.


Employee.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;

public class Employee implements Serializable {
 private static final long serialVersionUID = 1234L;

 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
  stream.writeObject(id);
  stream.writeObject("Mr." + firstName);
  stream.writeObject("Mr." + lastName);
 }

 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
  id = (int) stream.readObject();
  firstName = (String) stream.readObject();
  lastName = (String) stream.readObject();
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

}


Test.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Test {
 private static ObjectOutputStream out;
 private static ObjectInputStream in;

 private static Employee getEmployee() {
  Employee emp = new Employee();
  emp.setId(1);
  emp.setFirstName("Hari Krishna");
  emp.setLastName("Gurram");
  return emp;
 }

 public static void main(String args[]) throws IOException, ClassNotFoundException {
  /* Serialize object */
  FileOutputStream fos = new FileOutputStream("ser.out");
  out = new ObjectOutputStream(fos);
  Employee emp = getEmployee();
  out.writeObject(emp);

  /* Deserialize object */
  FileInputStream fis = new FileInputStream("ser.out");
  in = new ObjectInputStream(fis);
  Employee emp1 = (Employee) in.readObject();

  System.out.println(emp1);

 }
}


You may like




No comments:

Post a Comment