Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.
Why is custom serialization needed?
During serialization, there may be data loss if we use the ‘transient’ keyword. ‘Transient’ keyword is used on the variables which we don’t want to serialize. But sometimes, it is needed to serialize them in a different manner than the default serialization (such as encrypting before serializing etc.), in that case, we have to use custom serialization and deserialization.
Below program illustrates the above situation of data loss:
Username : gfg_admin Password : geeks Username : gfg_admin Password : null
In the above image example, before serialization, Account object can provide proper username and password but deserialization of Account object provides only username and not the password. This is due to declaring password variable as transient.
Hence during default serialization, there may be a chance of loss of information because of the transient keyword. To recover this loss, we will have to use Customized Serialization.
Customized serialization can be implemented using the following two methods:
- private void writeObject(ObjectOutputStream oos) throws Exception : This method will be executed automatically by the jvm(also known as Callback Methods) at the time of serialization. Hence to perform any activity during serialization, it must be defined only in this method.
- private void readObject(ObjectInputStream ois) throws Exception: This method will be executed automatically by the jvm(also known as Callback Methods) at the time of deserialization. Hence to perform any activity during deserialization, it must be defined only in this method.
Note: While performing object serialization, we have to define the above two methods in that class.
Username :gfg_admin Password :geeks Username :gfg_admin Password :geeks
- Serialization and Deserialization in Java with Example
- Enum with Customized Value in Java
- Object Serialization with Inheritance in Java
- Object Graph in Java Serialization
- Create a customized data structure which evaluates functions in O(1)
- pickle — Python object serialization
- marshal — Internal Python object serialization
- How to prevent Singleton Pattern from Reflection, Serialization and Cloning?
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.