Serialization and Deserialization in Java
Omar Ismail
Senior Software Engineer @ Digitinary | Java 8 Certified? | Spring & Spring Boot?????? | AWS? | Microservices ?? | RESTFul Apis & Integrations ?? FinTech ?? | Open Banking ?? | Digital Payments and Transformation??
Thanks to the original writer and article :
https://medium.com/@yahayayusuf/serialization-and-deserialization-in-java-92031a62524b
WHAT IS SERIALIZATION/DESERIALIZATION
Serialization simply means converting an object from a class into a byte state in a Java virtual machine to be transferred to another Java virtual machine that recreates the object from the byte state and the process of recreating the object is referred to as Deserialization.
Example of serialization and deserialization
Serialization
Let’s create a class whose object will be serialized.
import java.io.*;
public class Person implements Serializable{
int id = 0;
String name = “empty”;
public Person(int identity, String nomenclature) {
name = nomenclature;
id = identity;
}
}
The class Person implements Serializable to enable its object to be serialized/deserialized. Person class has two fields id and name; that change from default value upon class instantiation. Java.io package where the Serializable interface and other classes used in the program were imported.
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = “filename here”;
Person person = new Person(1, “John”);
// serialization
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
try {
out.writeObject(person);
System.out.println(“Success”);
} catch(Exception e) {
System.out.println(“Unsuccessful”);
领英推荐
} finally {
if(out != null) {
out.close();
}
}
}
The main method as you know runs the serialization and prints out success else prints unsuccessful. We use the ObjectOutputStream and its method writeObject to serialize objects.
Deserialization
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = “filename here”;
Person person = new Person(1, “John”);
// Deserialization
ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
try {
Person personObj = (Person)in.readObject();
System.out.println(“Person Id is “ +personObj.id + “ while name is “ + personObj.name);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in != null) {
in.close();
}
}
}
Deserialization is the reverse it uses the ObjectInputStream and the readObject method to reconstruct the object from the byte state. You will notice that it was cast to a Person data type this is to enable access to the fields in the Person class.
An object of a class that does not implement serialize interface cannot be serialized and any class that references a class that implements serialize interface must itself implement the serialize interface or else an exception will be thrown.
Serialization is platform-independent i.e serialize byte stream can be deserialized by a different Java Virtual Machine.
Static and transient fields are not serialized, so if you have a field you do not want to be serialized make it transient or static. In the case of static, it is not serialized because the static field is owned by the class, not the object while transient prevents the field from being serialized. Serialization is applied in Hibernate, JPA, and RMI.
Serialization can be customized but this is beyond the scope of this article you can read more on custom serialization and how to implement it or I may write something on it sometime.
I hope you understood what serialization/deserialization means, thank you for your time. Have an impactful day ahead.