Serialization and deserialization are processes used in software development to convert objects into a format that can be easily stored, transmitted, or persisted, and then restored back to their original form.
- Serialization is the process of converting an object into a format that can be stored, transmitted, or reconstructed later.
- Deserialization is the process of converting serialized data, such as binary/ XML/ json data, back into an object.
- When to use serialization?? It is mostly used in Web API to convert class objects into JSON string.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
// A sample class to serialize and deserialize
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Program
{
public static void Main()
{
// Create an instance of the Person class
Person person = new Person
{
Name = "John Smith",
Age = 30
};
// Serialize the object to a file
string filePath = "person.dat";
SerializeObject(person, filePath);
Console.WriteLine("Object serialized successfully.");
// Deserialize the object from the file
Person deserializedPerson = DeserializeObject(filePath);
Console.WriteLine($"Deserialized object: Name = {deserializedPerson.Name}, Age = {deserializedPerson.Age}");
}
// Serialize an object to a file
public static void SerializeObject(object obj, string filePath)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, obj);
}
}
// Deserialize an object from a file
public static T DeserializeObject<T>(string filePath)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
return (T)binaryFormatter.Deserialize(fileStream);
}
}
}
In this example, the Person
class is marked with the [Serializable]
attribute, indicating that it can be serialized. The Main
method creates an instance of the Person
class, serializes it to a file using the SerializeObject
method, and then deserializes it back using the DeserializeObject
method.
The SerializeObject
method uses a BinaryFormatter
to serialize the object to a file. It takes an object and a file path as parameters and writes the serialized data to the file.
The DeserializeObject
method reads the serialized data from the file using a BinaryFormatter
and returns the deserialized object. The generic type parameter T
is used to specify the type of the object being deserialized.
After running the program, it will output the serialized object to a file (person.dat
) and then deserialize it, displaying the deserialized object's properties.
Note that binary serialization is just one type of serialization available in C#. There are different serialization techniques and libraries available for XML, JSON, SOAP, and custom serialization, depending on your specific requirements and preferences.