Customizing Java Serialization

How to customize the Serialization?

  • Serializing is much about serializing the data members of an object.
  • You should use the transient modifier on data members you do not want to be serialized. (The transient data will not be initialized when the object is read.)
  • Serialization behavior can be altered by implementing the readObject() and writeObject() methods in the class that implements the java.io.Serializable interface.
  • Implementing readObject() and writeObject() methods allow the class to pre-proscess and post-process the serialization process, usually handling static or transient data members.
  • java.io.ObjectOutputStream provides the defaultWriteObject() method to serialize the object as normal. This method can only be called from inside the writeObject() method to handle special serialization.
  • java.io.ObjectInputStream provides the defaultReadObject() method to deserialize the object as normal. This method can only be called from inside the readObject() method to handle special deserialization.

Example in how to customize the serialization of java objects:

First a class, Rectangle, that is made serializable:
package app;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Rectangle implements java.io.Serializable {

  private int length;
  private int width;
  // This member variable will not be Serialized as it is transient.
  transient private Color color;

  public Rectangle() {
  }

  public Rectangle(int width, int length, Color color) {
    this.length = length;
    this.width = width;
    this.color = color;
  }

  private void readObject(ObjectInputStream input)
                         throws IOException, ClassNotFoundException {
    // deserialize the non-transient data members first;
    input.defaultReadObject();
    // Read the color
    setColor((Color)input.readObject());
  }
  private void writeObject(ObjectOutputStream output)
                         throws IOException, ClassNotFoundException {
    // serialize the non-transient data members first;
    output.defaultWriteObject();
    // Write the color
    output.writeObject(color);
  }

  @Override
  public String toString() {
    return " width: " + width + " length: " + length + " color: " + color;
  }

  public int getLength() {
    return length;
  }

  public void setLength(int length) {
    this.length = length;
  }

  public int getWidth() {
    return width;
  }

  public void setWidth(int width) {
    this.width = width;
  }

  public Color getColor() {
    return color;
  }

  public void setColor(Color color) {
    this.color = color;
  }
}
Here is a main class to write the serializable object of a Rectangle to a file Rectangle.ser:
package app;

import java.awt.Color;
import java.io.*;

public class WriteObj {
  public static void main(String args[]) {
    Rectangle rect = new Rectangle(25, 60, Color.blue);
    ObjectOutputStream out = null;
    try {
      out = new ObjectOutputStream(new FileOutputStream("Rectangle.ser"));
      out.writeObject(rect);
      System.out.println("Wrote Rectangle with \"" + rect + "\" to file");
    }
    catch (IOException e) {
      System.err.println("Error writing object: " + e.getMessage());
    }
    finally {
      try {
        out.close();
      }
      catch (IOException e) {
        System.err.println(e.getMessage());
      }
    }
  }
}
Here's console printout:
Wrote Rectangle with " width: 25 length: 60 color: java.awt.Color[r=0,g=0,b=255]" to file
Here is the hexadecimal representation of the Rectangle.ser file:
AC ED 00 05 73 72 00 0D 61 70 70 2E 52 65 63 74 
61 6E 67 6C 65 38 82 0C AB A6 39 12 92 03 00 02 
49 00 06 6C 65 6E 67 74 68 49 00 05 77 69 64 74 
68 78 70 00 00 00 3C 00 00 00 19 73 72 00 0E 6A 
61 76 61 2E 61 77 74 2E 43 6F 6C 6F 72 01 A5 17 
83 10 8F 33 75 02 00 05 46 00 06 66 61 6C 70 68 
61 49 00 05 76 61 6C 75 65 4C 00 02 63 73 74 00 
1B 4C 6A 61 76 61 2F 61 77 74 2F 63 6F 6C 6F 72 
2F 43 6F 6C 6F 72 53 70 61 63 65 3B 5B 00 09 66 
72 67 62 76 61 6C 75 65 74 00 02 5B 46 5B 00 06 
66 76 61 6C 75 65 71 00 7E 00 04 78 70 00 00 00 
00 FF 00 00 FF 70 70 70 78
Here is a the main class to read the serializable object of the Rectangle from the file Rectangle.ser:
package app;

import java.io.*;

public class ReadObj {
  public static void main(String args[]) {
    ObjectInputStream in = null;
    try {
      in = new ObjectInputStream(
        new FileInputStream("Rectangle.ser"));
      Rectangle rect = (Rectangle) in.readObject();
      System.out.println("Read Rectangle with \"" + rect + "\" from file");
    }
    catch (ClassCastException e) {
      System.err.println("Error casting object to a Rectangle");
    }
    catch (ClassNotFoundException e) {
      System.err.println("Class not found");
    }
    catch (IOException e) {
      System.err.println("Error reading object: " + e.getMessage());
    }
    finally {
      try {
        in.close();
      }
      catch (IOException e) {
        System.err.println(e.getMessage());
      }
    }
  }
}
Here's console printout:
Read Rectangle with " width: 25 length: 60 color: java.awt.Color[r=0,g=0,b=255]" from file
You can download this example here (needed tools can be found in the right menu on this page).
Prev. in menu JavaSerialize/Customizing.php Next in menu
Prev. in menu JavaSerialize/Customizing.php Next in menu
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.