Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Customized Serialization

During default serialization there may be a chance of loss of information because of transient keyword.

Example:
import java.io.*;
class Account implements Serializable
{
	String uname="Silan";
	transient String pass="Software";
}
class CustomizeDemo
{
	public static void main(String args[])throws Exception
	{
		Account a1=new Account();
		System.out.println(a1.uname+"...."+a1.pass);
		FileOutputStream fos=new FileOutputStream("abc.ser");
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		oos.writeObject(a1);
		FileInputStream fis=new FileInputStream("abc.ser");
		ObjectInputStream ois=new ObjectInputStream(fis);
		Account a2=(Account)ois.readObject();
		System.out.println(a2.uname+"...."+a2.pass);
	}
}
output:

Silan Software Silan null

In the above example before serialization Account object can provide proper username and password but after deserialization Account object can provide only username but not password,this is due to declaring password variable as transient.

Hence during default serialization there may be a chance of loss of information because transient keyword.

To recover this loss of information we should go for customized serialization.

We can implement customized serialization by using the following methods:-

  • 1.private void writeObject(ObjectOutputStream os)throws Exception
    This method will be executed automatically at the time of serialization.Hence at the time of serialization if we want to perform any activity we have to define that in this method only.
  • 2.private void readObject(ObjectInputStream is)throws Exception
    This method will be executed automatically at the time of serialization.Hence at the time of serialization if we want to perform any activity we have to define that in this method only.

Note:

  • • The above methods are call back methods because these are executed automatically by the JVM,
  • • While performing which object serialization we have to to do extrawhole in the corresponding class we have to define above methods.

For example while performing Account object serialization if we required to do extra work in the account class we have to define above methods.

Example:
import java.io.*;
class Account implements Serializable
{
	String username="Silan";
	transient string pwd="Software";
	private void writeObject(ObjectOutputStream os)throws Exception
	{
		os.defaultWriteObject(); //in addition to execute method do default serialization
		String epwd="abc"+pwd; //prepare encrypted password and write into a file
		os.writeObject(epwd);
	}
	private void readObject(ObjectInputStream is)throws Exception
	{
		is.defaultReadObject();
		String epwd=(String)is.readobject();
		pwd=epwd.Substring();
	}
}
class CustSerializeDemo1
{
	public static void main(String[] args)throws Exception
	Account a1=new Account();
	System.out.println(a1.username+"..."+a1.pwd);
	FileOutputStream fos=new FileOutputStream("abc.ser");
	ObjectOutputStream oos=new ObjectOutputStream(fos);
	oos.writeObject(a1);
	FileInputStream fis=new FileInputStream("abc.ser");
	ObjectInputStream ois=new ObjectInputStream(fis);
	Account a2=(Account)ois.readObject();
	System.out.println(a2.username+"..."+a2.pwd);
}
}

On the above program before serialization and after serialization Account object can provide proper username and password.

Note:
Programmer can't call private methods directly from outside of the class,but JVM can cal call private methods directly from outside of the class.

Example
import java.io.*;
class Account implements Serializable
{
	String username="Silan";
	transient String pwd="Software";
	transient int pin=1234;
	private void writeObject(ObjectOutputStream os)throws Exception
	{
		os.defaultWriteObject();
		String epwd="123"+pwd;
		int epin=4444+pin;
		os.WriteInt(epwd);
		os.WriteInt(epin);
	}
	private void readObject(ObjectOutputStream is)throws Exception
	{
		is.defaultReadObject();
		String epwd=(String)is.readObject();
		pwd=epwd.subString(3);
		int epin=is.readInt();
		pin=epin-4444;
	}
}
class CustSerializeDemo2
{
	public static void main(String args[])throws Exception
	{
		Account a1=new Account();
		system.out.println(a1.username+"..."+a.pwd+"..."+a1.pin);
		FileOutputStream fos=new FileOutputStream("abc.ser");
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		oos.WriteObject(a1);
		FileInputStream fis=new FileInputStream("abc.ser");
		ObjectInputStream ois=new ObjectInputStream(fis);
		Account a2=(Account)ois.readObject();
		system.out.println(a2.username+"..."+a2.pwd+"..."+a2.pin);
	}
}

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext