Serializing and Deserializing Objects

Serialization and deserialization are powerful concepts that deal with converting an object into a stream of bytes and vice versa, respectively. These concepts are essential when we need to store the state of an object (e.g., to save it to a file or transmit it over a network) and later recreate it in its original form. Serialization and deserialization are useful in a variety of situations:

  • Persistence: Saving the state of an object to disk for future retrieval (e.g., saving a user’s settings or game state).
  • Communication: Sending objects over a network, such as when working with remote procedure calls (RPCs) or web services.
  • Caching: Storing objects temporarily (e.g., in a cache) to avoid recalculating or re-fetching them.

Serialization

Serialization is the process of converting an object into a byte stream. This byte stream can be saved to a file, transferred over a network, or stored in memory. The object can then be restored (deserialized) later.

  • Object: To serialize an object, we need to create an instance of ObjectOutputStream and pass a FileOutputStream to it. The writeObject() method is used to write the object to the output stream.

The Person object is serialized and stored in a file named person.ser.

  • Collection: We can serialize a collection just like any other object. Here’s an example of serializing an ArrayList:

This serializes an ArrayList of String objects into the file names.ser.

Deserialization

Deserialization is the reverse process of serialization: it involves converting a byte stream back into an object. During deserialization, the byte stream is read, and an exact replica of the original object is created, with its state restored.

  • Object: To deserialize an object, we use the ObjectInputStream class, which reads the byte stream from a file and converts it back into an object using the readObject() method.

The Person object is read from the file and cast back into its original class.

  • Collection: To deserialize a collection, we simply cast the read object into the appropriate collection type.

This code deserializes the ArrayList from the file names.ser and prints its contents.

Custom Serialization

While Java provides default serialization, we might need more control over the process. Custom serialization allows us to specify how an object should be serialized and deserialized by overriding two special methods: writeObject() and readObject().

Let’s say we have a class where we want to serialize only certain fields or add custom logic during the serialization process. We can override writeObject() and readObject() as shown below:

The password field is marked as transient, meaning it won’t be serialized by default. However, it is explicitly serialized and deserialized using custom methods.