Searching...
Saturday, 13 December 2014

Java custom serialization example

18:39
Here is one example to see how to override the default Java serialization mechanism and how to do the implementation.
When we need to implement custom serialization we also have to define writeObject and/or readObject methods in our serializable class. Here we are not overriding these methods but JVM checks this methods.
Use case: I have an employee class and i do not want to serialize employees object in case employee address is null or zero length.

CustomSerializaion Example

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class CustomSerializaionExample {

    public static void main(String[] args) {
        Employee emp= new Employee("Vinod", "");

        FileOutputStream fos;
        try {
            // Serialization
            fos = new FileOutputStream(new File("employee.ser"));
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(emp);
            oos.flush();
            oos.close();

            // Deserialization
            FileInputStream fis = new FileInputStream("employee.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Employee deemp = (Employee) ois.readObject();
            ois.close();
            System.out.println("Customer after serialization: " + deemp);
        } catch (IOException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

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

    private String name;
    private String address;

    public Employee(String name, String address) {
        super();
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    private void writeObject(ObjectOutputStream o) throws IOException {

        if (address == null || address.length() == 0) {
            throw new IllegalArgumentException();

        }
        o.writeObject(name);
        o.writeObject(address);
    }

    private void readObject(ObjectInputStream o) throws IOException,
            ClassNotFoundException {

        name = (String) o.readObject();
        address = (String) o.readObject();
       
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", address=" + address + "]";
    }

}

Output

Exception in thread "main" java.lang.IllegalArgumentException
at com.pretech.test.Employee.writeObject(CustomSerializaionExample.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at com.pretech.test.CustomSerializaionExample.main(CustomSerializaionExample.java:21)

0 comments: