Magic methods to Customize Serialization

Magic Methods to Customize Serialization

(i) private void writeObject(ObjectOutputStream out) throws IOException: This method helps in customizing the serialization process.Here we can set the properties before the object is serialized.

package com.serialize.example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationExample implements Serializable {
private static final long serialVersionUID = 1L;
private static final String fileName = “serializationExample.ser”;
private String name;
private Integer version;
public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

public String getName() {
return name;
}

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

public SerializationExample(String name, Integer version) {
this.name = name;
this.version = version;
}

public static void serializeObject(SerializationExample serializationExample)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializationExample);
oos.close();
}

public static SerializationExample deSerializeObject()
throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
SerializationExample serializationExample = (SerializationExample) ois.readObject();
ois.close();
return serializationExample;
}

private void writeObject(ObjectOutputStream out) throws IOException {
System.out.println(“writeObject”);
setName(“Rochit”);
out.defaultWriteObject();
}

public static void main(String args[]) throws IOException,
FileNotFoundException, ClassNotFoundException {

SerializationExample serializationExample = new SerializationExample(“Gyan”, 1);
System.out.println(“Before Serialization”);
System.out.println(“Name:” + serializationExample.getName());
System.out.println(“version:” + serializationExample.getVersion());
serializeObject(serializationExample);
SerializationExample serializationExample1 = deSerializeObject();
System.out.println(“After Deserialization”);
System.out.println(“Name:” + serializationExample1.getName());
System.out.println(“version:” + serializationExample1.getVersion());
}

}

Output:
Before Serialization
Name:Gyan
version:1
writeObject
After Deserialization
Name:Rochit
version:1

(ii) private void readObject(ObjectInputStream in) throws IOException: This method is executed during de-serialization process. Here we can set the properties of the object after it has been reconstructed from the bytes.

package com.serialize.example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationExample implements Serializable {
private static final long serialVersionUID = 1L;
private static final String fileName = “serializationExample.ser”;
private String name;
private Integer version;

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

public String getName() {
return name;
}

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

public SerializationExample(String name, Integer version) {
this.name = name;
this.version = version;
}

public static void serializeObject(SerializationExample serializationExample)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializationExample);
oos.close();
}

public static SerializationExample deSerializeObject()
throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
SerializationExample serializationExample = (SerializationExample) ois.readObject();
ois.close();
return serializationExample;
}

private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
System.out.println(“readObject”);
in.defaultReadObject();
setVersion(2);
}

public static void main(String args[]) throws IOException,
FileNotFoundException, ClassNotFoundException {

SerializationExample serializationExample = new SerializationExample(“Gyan”, 1);
System.out.println(“Before Serialization”);
System.out.println(“Name:” + serializationExample.getName());
System.out.println(“version:” + serializationExample.getVersion());
serializeObject(serializationExample);
SerializationExample serializationExample1 = deSerializeObject();
System.out.println(“After Deserialization”);
System.out.println(“Name:” + serializationExample1.getName());
System.out.println(“version:” + serializationExample1.getVersion());
}

}

Output:
Before Serialization
Name:Gyan
version:1
readObject
After Deserialization
Name:Gyan
version:2

(iii) private Object writeReplace() throws ObjectStreamException: 

Here we can provide a replacement object that will be serialized instead of the original one.

package com.serialize.example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
public class SerializationExample implements Serializable {
private static final long serialVersionUID = 1L;
private static final String fileName = “serializationExample.ser”;
private String name;
private Integer version;
public Integer getVersion() {
return version;
}public void setVersion(Integer version) {
this.version = version;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public SerializationExample(String name, Integer version) {
this.name = name;
this.version = version;
}

public static void serializeObject(SerializationExample serializationExample)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializationExample);
oos.close();
}

public static SerializationExample deSerializeObject()
throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
SerializationExample serializationExample = (SerializationExample) ois.readObject();
ois.close();
return serializationExample;
}

private Object writeReplace() throws ObjectStreamException {
System.out.println(“writeReplace”);
return new SerializationExample(“Prabhakar”, 4);
}

public static void main(String args[]) throws IOException,
FileNotFoundException, ClassNotFoundException {

SerializationExample serializationExample = new SerializationExample(“Gyan”, 1);
System.out.println(“Before Serialization”);
System.out.println(“Name:” + serializationExample.getName());
System.out.println(“version:” + serializationExample.getVersion());
serializeObject(serializationExample);
SerializationExample serializationExample1 = deSerializeObject();
System.out.println(“After Deserialization”);
System.out.println(“Name:” + serializationExample1.getName());
System.out.println(“version:” + serializationExample1.getVersion());
}

}

Output:
Before Serialization
Name:Gyan
version:1
writeReplace
After Deserialization
Name:Prabhakar
version:4

(iv) private Object readResolve() throws ObjectStreamException

We can replace the de-serialized object by another one.

package com.serialize.example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;public class SerializationExample implements Serializable {
private static final long serialVersionUID = 1L;
private static final String fileName = “serializationExample.ser”;
private String name;
private Integer version;
public Integer getVersion() {
return version;
}public void setVersion(Integer version) {
this.version = version;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}

public SerializationExample(String name, Integer version) {
this.name = name;
this.version = version;
}

public static void serializeObject(SerializationExample serializationExample)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializationExample);
oos.close();
}

public static SerializationExample deSerializeObject()
throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
SerializationExample serializationExample = (SerializationExample) ois.readObject();
ois.close();
return serializationExample;
}

private Object readResolve() throws ObjectStreamException {
System.out.println(“readResolve”);
return new SerializationExample(“Vivek”, 3);
}

public static void main(String args[]) throws IOException,
FileNotFoundException, ClassNotFoundException {

SerializationExample serializationExample = new SerializationExample(“Gyan”, 1);
System.out.println(“Before Serialization”);
System.out.println(“Name:” + serializationExample.getName());
System.out.println(“version:” + serializationExample.getVersion());
serializeObject(serializationExample);
SerializationExample serializationExample1 = deSerializeObject();
System.out.println(“After Deserialization”);
System.out.println(“Name:” + serializationExample1.getName());
System.out.println(“version:” + serializationExample1.getVersion());
}

}
Output:
Before Serialization
Name:Gyan
version:1
readResolve
After Deserialization
Name:Vivek
version:3

(v) public void validateObject() throws InvalidObjectException

We can put a validation logic for the de-serialized object and throw InvalidObjectException in case the validation fails.Our class needs to implement ObjectInputValidation interface and override validateObject method.

package com.serialize.example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectInputValidation;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationExample implements Serializable,ObjectInputValidation {
private static final long serialVersionUID = 1L;
private static final String fileName = “serializationExample.ser”;
private String name;

private Integer version;

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

public String getName() {
return name;
}

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

public SerializationExample(String name, Integer version) {
this.name = name;
this.version = version;
}

public static void serializeObject(SerializationExample serializationExample)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializationExample);
oos.close();
}

public static SerializationExample deSerializeObject()
throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
SerializationExample serializationExample = (SerializationExample) ois.readObject();
ois.close();
return serializationExample;
}

private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
System.out.println(“readObject”);
in.registerValidation(this, 0);
in.defaultReadObject();
setVersion(null);
}

@Override
public void validateObject() throws InvalidObjectException {
System.out.println(“validateObject”);
if (getVersion() == null) {
throw new InvalidObjectException(getName());
}
}

public static void main(String args[]) throws IOException,
FileNotFoundException, ClassNotFoundException {

SerializationExample serializationExample = new SerializationExample(“Gyan”, 1);
System.out.println(“Before Serialization”);
System.out.println(“Name:” + serializationExample.getName());
System.out.println(“version:” + serializationExample.getVersion());
serializeObject(serializationExample);
SerializationExample serializationExample1 = deSerializeObject();
System.out.println(“After Deserialization”);
System.out.println(“Name:” + serializationExample1.getName());
System.out.println(“version:” + serializationExample1.getVersion());
}

}
Output:
Before Serialization
Name:Gyan
version:1
writeReplace
writeObject
readObject
validateObject
Exception in thread “main” java.io.InvalidObjectException: Rochit
at com.serialize.example.SerializationExample.validateObject(SerializationExample.java:79)
at java.io.ObjectInputStream$ValidationList$1.run(ObjectInputStream.java:2254)
at java.io.ObjectInputStream$ValidationList$1.run(ObjectInputStream.java:2252)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectInputStream$ValidationList.doCallbacks(ObjectInputStream.java:2250)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:378)
at com.serialize.example.SerializationExample.deSerializeObject(SerializationExample.java:57)
at com.serialize.example.SerializationExample.main(SerializationExample.java:101)

Please find below the java program with all the methods:

package com.serialize.example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectInputValidation;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
public class SerializationExample implements Serializable,ObjectInputValidation {
private static final long serialVersionUID = 1L;

private static final String fileName = “serializationExample.ser”;

private String name;

private Integer version;

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

public String getName() {
return name;
}

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

public SerializationExample(String name, Integer version) {
this.name = name;
this.version = version;
}

public static void serializeObject(SerializationExample serializationExample)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializationExample);
oos.close();
}

public static SerializationExample deSerializeObject()
throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
SerializationExample serializationExample = (SerializationExample) ois.readObject();
ois.close();
return serializationExample;
}

private void writeObject(ObjectOutputStream out) throws IOException {
System.out.println(“writeObject”);
setName(“Rochit”);
out.defaultWriteObject();
}

private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
System.out.println(“readObject”);
in.registerValidation(this, 0);
in.defaultReadObject();
setVersion(2);
}

@Override
public void validateObject() throws InvalidObjectException {
System.out.println(“validateObject”);
if (getVersion() == null) {
throw new InvalidObjectException(getName());
}
}

private Object writeReplace() throws ObjectStreamException {
System.out.println(“writeReplace”);
return new SerializationExample(“Prabhakar”, 4);
}

private Object readResolve() throws ObjectStreamException {
System.out.println(“readResolve”);
return new SerializationExample(“Vivek”, 3);
}

public static void main(String args[]) throws IOException,
FileNotFoundException, ClassNotFoundException {

SerializationExample serializationExample = new SerializationExample(“Gyan”, 1);
System.out.println(“Before Serialization”);
System.out.println(“Name:” + serializationExample.getName());
System.out.println(“version:” + serializationExample.getVersion());
serializeObject(serializationExample);
SerializationExample serializationExample1 = deSerializeObject();
System.out.println(“After Deserialization”);
System.out.println(“Name:” + serializationExample1.getName());
System.out.println(“version:” + serializationExample1.getVersion());
}

}
Output:
Before Serialization
Name:Gyan
version:1
writeReplace
writeObject
readObject
readResolve
validateObject
After Deserialization
Name:Vivek
version:3

Leave a Reply

Your email address will not be published. Required fields are marked *