Serialization#
Definition of Serialization
Serialization is the process of converting an object into a format that can be stored or transmitted and later reconstructed. This allows objects to be saved to a file, sent over a network, or stored in a database in a way that they can be reconstructed later.
Uses of Serialization
Persistence – Storing objects in a file or database for later retrieval.
Communication – Transmitting objects over a network (e.g., in distributed applications).
Caching – Storing objects in memory to speed up future access.
Deep Copying – Cloning objects by serializing and deserializing them.
Interoperability – Sharing data between different programming languages or systems.
Example in Java
In Java, serialization is implemented using the
Serializable
interface.import java.io.*; // Class that implements Serializable class Person implements Serializable { private static final long serialVersionUID = 1L; // Ensure versioning compatibility String name; int age; // Constructor public Person(String name, int age) { this.name = name; this.age = age; } // Method to display object data public void display() { System.out.println("Name: " + name + ", Age: " + age); } } public class SerializationExample { public static void main(String[] args) { Person person = new Person("John", 30); // Serialize the object try (FileOutputStream fileOut = new FileOutputStream("person.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut)) { out.writeObject(person); System.out.println("Object serialized successfully."); } catch (IOException e) { e.printStackTrace(); } // Deserialize the object try (FileInputStream fileIn = new FileInputStream("person.ser"); ObjectInputStream in = new ObjectInputStream(fileIn)) { Person deserializedPerson = (Person) in.readObject(); System.out.println("Object deserialized successfully."); deserializedPerson.display(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
Explanation:
The
Person
class implementsSerializable
.ObjectOutputStream
writes the object to a file (person.ser
).ObjectInputStream
reads the object back and restores it.
Equivalent Example in Python
In Python,
pickle
is commonly used for serialization.import pickle # Class definition class Person: def __init__(self, name, age): self.name = name self.age = age def display(self): print(f"Name: {self.name}, Age: {self.age}") # Serialize object person = Person("John", 30) with open("person.pkl", "wb") as file: pickle.dump(person, file) print("Object serialized successfully.") # Deserialize object with open("person.pkl", "rb") as file: deserialized_person = pickle.load(file) print("Object deserialized successfully.") deserialized_person.display()
Key Differences:
pickle.dump()
serializes the object.pickle.load()
deserializes the object.
Equivalent Example in C#
In C#,
System.Runtime.Serialization
andSystem.Xml.Serialization
are used.using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; [Serializable] class Person { public string Name { get; set; } public int Age { get; set; } public Person(string name, int age) { Name = name; Age = age; } public void Display() { Console.WriteLine($"Name: {Name}, Age: {Age}"); } } class SerializationExample { static void Main() { Person person = new Person("John", 30); string filePath = "person.dat"; // Serialize object IFormatter formatter = new BinaryFormatter(); using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { formatter.Serialize(stream, person); Console.WriteLine("Object serialized successfully."); } // Deserialize object using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { Person deserializedPerson = (Person)formatter.Deserialize(stream); Console.WriteLine("Object deserialized successfully."); deserializedPerson.Display(); } } }
Key Differences:
Uses
[Serializable]
attribute.BinaryFormatter
is used for serialization and deserialization.FileStream
handles writing and reading from a file.
Summary
Java: Uses
Serializable
interface andObjectOutputStream
/ObjectInputStream
.Python: Uses
pickle.dump()
andpickle.load()
.C#: Uses
[Serializable]
attribute andBinaryFormatter
.
Each language has different serialization approaches but achieves the same goal—converting objects into a storable/transmittable format and reconstructing them later.