What is Serialization and Deserialization in C#?

Serialization and deserialization

Serialization and deserialization are processes used to convert objects into a format that can be easily stored, transmitted, or persisted and then reconstructed back into objects when needed. Serialization is the process of converting objects into a byte stream, XML, or JSON format, while deserialization is the process of converting these serialized data back into objects.

In C#, serialization and deserialization can be implemented using various built-in mechanisms and third-party libraries. Here's how you can implement serialization and deserialization using some common approaches:

1. Binary Serialization

Binary serialization is a built-in mechanism in .NET for converting objects into a binary format.

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

// Sample class to serialize
[Serializable]
public class MyClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of MyClass
        MyClass obj = new MyClass { Id = 1, Name = "Example" };

        // Binary serialization
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream stream = new FileStream("data.bin", FileMode.Create))
        {
            formatter.Serialize(stream, obj);
        }

        // Binary deserialization
        using (FileStream stream = new FileStream("data.bin", FileMode.Open))
        {
            MyClass newObj = (MyClass)formatter.Deserialize(stream);
            Console.WriteLine($"Id: {newObj.Id}, Name: {newObj.Name}");
        }
    }
}

2. XML Serialization

XML serialization allows you to convert objects into XML format.

using System;
using System.IO;
using System.Xml.Serialization;

// Sample class to serialize
[Serializable]
public class MyClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of MyClass
        MyClass obj = new MyClass { Id = 1, Name = "Example" };

        // XML serialization
        XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
        using (FileStream stream = new FileStream("data.xml", FileMode.Create))
        {
            serializer.Serialize(stream, obj);
        }

        // XML deserialization
        using (FileStream stream = new FileStream("data.xml", FileMode.Open))
        {
            MyClass newObj = (MyClass)serializer.Deserialize(stream);
            Console.WriteLine($"Id: {newObj.Id}, Name: {newObj.Name}");
        }
    }
}

3. JSON Serialization

JSON serialization converts objects into JSON format.

using System;
using System.IO;
using System.Text.Json;

// Sample class to serialize
public class MyClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of MyClass
        MyClass obj = new MyClass { Id = 1, Name = "Example" };

        // JSON serialization
        string jsonString = JsonSerializer.Serialize(obj);
        File.WriteAllText("data.json", jsonString);

        // JSON deserialization
        string jsonText = File.ReadAllText("data.json");
        MyClass newObj = JsonSerializer.Deserialize<MyClass>(jsonText);
        Console.WriteLine($"Id: {newObj.Id}, Name: {newObj.Name}");
    }
}

These are some common approaches to implement serialization and deserialization in C#. Depending on your requirements and the complexity of your data, you can choose the appropriate serialization method.


Similar Articles