What does it mean to serialize an object?

The process whereby an object or data structure is translated into a format suitable for transfer over a network, or storage (e.g. in an array buffer or file format).

In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON.stringify().

CSS values are serialized by calling the function CSSStyleDeclaration.getPropertyValue().

See also

  • Serialization on Wikipedia

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.

What does it mean to serialize an object?

The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.

To make a Java object serializable we implement the java.io.Serializable interface.
The ObjectOutputStream class contains writeObject() method for serializing an Object.

public final void writeObject(Object obj) throws IOException

The ObjectInputStream class contains readObject() method for deserializing an object.

public final Object readObject() throws IOException, ClassNotFoundException

Advantages of Serialization
1. To save/persist state of an object.
2. To travel an object across a network.

What does it mean to serialize an object?

Only the objects of those classes can be serialized which are implementing java.io.Serializable interface.
Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and Remote.

Points to remember
1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-versa is not true.
2. Only non-static data members are saved via Serialization process.
3. Static data members and transient data members are not saved via Serialization process.So, if you don’t want to save value of a non-static data member then make it transient.
4. Constructor of object is never called when an object is deserialized.
5. Associated objects must be implementing Serializable interface.
Example :

class A implements Serializable{ // B also implements Serializable // interface. B ob=new B(); }

SerialVersionUID
The Serialization runtime associates a version number with each Serializable class called a SerialVersionUID, which is used during Deserialization to verify that sender and receiver of a serialized object have loaded classes for that object which are compatible with respect to serialization. If the receiver has loaded a class for the object that has different UID than that of corresponding sender’s class, the Deserialization will result in an InvalidClassException. A Serializable class can declare its own UID explicitly by declaring a field name.
It must be static, final and of type long.
i.e- ANY-ACCESS-MODIFIER static final long serialVersionUID=42L;

If a serializable class doesn’t explicitly declare a serialVersionUID, then the serialization runtime will calculate a default one for that class based on various aspects of class, as described in Java Object Serialization Specification. However it is strongly recommended that all serializable classes explicitly declare serialVersionUID value, since its computation is highly sensitive to class details that may vary depending on compiler implementations, any change in class or using different id may affect the serialized data.

It is also recommended to use private modifier for UID since it is not useful as inherited member.

serialver
The serialver is a tool that comes with JDK. It is used to get serialVersionUID number for Java classes.
You can run the following command to get serialVersionUID

serialver [-classpath classpath] [-show] [classname…]

What does it mean to serialize an object?

Example 1:

import java.io.*;

class Demo implements java.io.Serializable

{

    public int a;

    public String b;

    public Demo(int a, String b)

    {

        this.a = a;

        this.b = b;

    }

}

class Test

{

    public static void main(String[] args)

    {   

        Demo object = new Demo(1, "geeksforgeeks");

        String filename = "file.ser";

        try

        {   

            FileOutputStream file = new FileOutputStream(filename);

            ObjectOutputStream out = new ObjectOutputStream(file);

            out.writeObject(object);

            out.close();

            file.close();

            System.out.println("Object has been serialized");

        }

        catch(IOException ex)

        {

            System.out.println("IOException is caught");

        }

        Demo object1 = null;

        try

        {   

            FileInputStream file = new FileInputStream(filename);

            ObjectInputStream in = new ObjectInputStream(file);

            object1 = (Demo)in.readObject();

            in.close();

            file.close();

            System.out.println("Object has been deserialized ");

            System.out.println("a = " + object1.a);

            System.out.println("b = " + object1.b);

        }

        catch(IOException ex)

        {

            System.out.println("IOException is caught");

        }

        catch(ClassNotFoundException ex)

        {

            System.out.println("ClassNotFoundException is caught");

        }

    }

}

Output :

Object has been serialized Object has been deserialized a = 1 b = geeksforgeeks

Example 2:

import java.io.*;

class Emp implements Serializable {

private static final long serialversionUID =

                                 129348938L;

    transient int a;

    static int b;

    String name;

    int age;

public Emp(String name, int age, int a, int b)

    {

        this.name = name;

        this.age = age;

        this.a = a;

        this.b = b;

    }

}

public class SerialExample {

public static void printdata(Emp object1)

    {

        System.out.println("name = " + object1.name);

        System.out.println("age = " + object1.age);

        System.out.println("a = " + object1.a);

        System.out.println("b = " + object1.b);

    }

public static void main(String[] args)

    {

        Emp object = new Emp("ab", 20, 2, 1000);

        String filename = "shubham.txt";

        try {

            FileOutputStream file = new FileOutputStream

                                           (filename);

            ObjectOutputStream out = new ObjectOutputStream

                                           (file);

            out.writeObject(object);

            out.close();

            file.close();

            System.out.println("Object has been serialized\n"

                              + "Data before Deserialization.");

            printdata(object);

            object.b = 2000;

        }

        catch (IOException ex) {

            System.out.println("IOException is caught");

        }

        object = null;

        try {

            FileInputStream file = new FileInputStream

                                         (filename);

            ObjectInputStream in = new ObjectInputStream

                                         (file);

            object = (Emp)in.readObject();

            in.close();

            file.close();

            System.out.println("Object has been deserialized\n"

                                + "Data after Deserialization.");

            printdata(object);

        }

        catch (IOException ex) {

            System.out.println("IOException is caught");

        }

        catch (ClassNotFoundException ex) {

            System.out.println("ClassNotFoundException" +

                                " is caught");

        }

    }

}

Output:

Object has been serialized Data before Deserialization. name = ab age = 20 a = 2 b = 1000 Object has been deserialized Data after Deserialization. name = ab age = 20 a = 0 b = 2000

Description for Output:
You have seen while deserializing the object the values of a and b has changed. The reason being a was marked as transient and b was static.
In case of transient variables:- A variable defined with transient keyword is not serialized during serialization process.This variable will be initialized with default value during deserialization. (e.g: for objects it is null, for int it is 0).
In case of static Variables:- A variable defined with static keyword is not serialized during serialization process.This variable will be loaded with current value defined in the class during deserialization.

This article is contributed by Mehak Narang and Shubham Juneja. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


What does it mean to serialize an object Python?

Serialization refers to the process of converting a data object (e.g., Python objects, Tensorflow models) into a format that allows us to store or transmit the data and then recreate the object when needed using the reverse process of deserialization.

Why do we serialize objects in Java?

Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.

What is serialization with example?

In computing, serialization (US and Oxford spelling) or serialisation (UK spelling) is the process of translating a data structure or object state into a format that can be stored (for example, in a file or memory data buffer) or transmitted (for example, over a computer network) and reconstructed later (possibly in a ...

What is the use of serialize ()?

The serialize() function converts a storable representation of a value. To serialize data means to convert a value to a sequence of bits, so that it can be stored in a file, a memory buffer, or transmitted across a network.