Serialization Modes in C#
What is Serialization in C#?
**Serialization** is the process of converting an object into a format that can be stored or transmitted and later reconstructed. C# supports different **serialization modes**, each suited for specific use cases, such as storing data, transferring objects, or persisting object states.
Types of Serialization in C#
The following are the different modes of serialization supported in C#:
Serialization Mode | Description | Use Case |
---|---|---|
Binary Serialization | Converts an object into a **binary format**. | Used for **performance-efficient** storage and communication. |
XML Serialization | Converts an object into a **human-readable XML format**. | Used for **data sharing** and **configuration files**. |
JSON Serialization | Converts an object into a **lightweight JSON format**. | Used for **REST APIs** and **web services**. |
Custom Serialization | Allows **manual control** over serialization. | Used when **custom serialization logic** is required. |
Binary Serialization
Binary serialization stores objects in a **compact and fast** format but is **not human-readable**.
Example: Binary Serialization
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
// Writing Binary Data
class Program
{
static void Main()
{
Person person = new Person { Name = "Alice", Age = 28 };
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream("person.dat", FileMode.Create))
{
formatter.Serialize(stream, person);
}
Console.WriteLine("Object serialized successfully.");
}
}
// Reading Binary Data
class DeserializeExample
{
static void Main()
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream("person.dat", FileMode.Open))
{
Person person = (Person)formatter.Deserialize(stream);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
}
}
}
// Output:
// Object serialized successfully.
// Name: Alice, Age: 28
The **[Serializable]** attribute is required to serialize an object.
XML Serialization
XML serialization converts an object into a **structured XML format** that is human-readable.
Example: XML Serialization
using System;
using System.IO;
using System.Xml.Serialization;
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
// Writing XML Data
class Program
{
static void Main()
{
Person person = new Person { Name = "Bob", Age = 35 };
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (FileStream stream = new FileStream("person.xml", FileMode.Create))
{
serializer.Serialize(stream, person);
}
Console.WriteLine("Object serialized to XML.");
}
}
// Reading XML Data
class DeserializeExample
{
static void Main()
{
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (FileStream stream = new FileStream("person.xml", FileMode.Open))
{
Person person = (Person)serializer.Deserialize(stream);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
}
}
}
// Output:
// Object serialized to XML.
// Name: Bob, Age: 35
XML serialization is useful for **data sharing between systems**.
JSON Serialization
JSON serialization is widely used for **REST APIs and web applications**.
Example: JSON Serialization
using System;
using System.IO;
using System.Text.Json;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
// Writing JSON Data
class Program
{
static void Main()
{
Person person = new Person { Name = "Charlie", Age = 40 };
string json = JsonSerializer.Serialize(person);
File.WriteAllText("person.json", json);
Console.WriteLine("Object serialized to JSON.");
}
}
// Reading JSON Data
class DeserializeExample
{
static void Main()
{
string json = File.ReadAllText("person.json");
Person person = JsonSerializer.Deserialize(json);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
}
}
// Output:
// Object serialized to JSON.
// Name: Charlie, Age: 40
JSON is lightweight and easy to use for **web and mobile applications**.