Curriculum
Serialization and Deserialization in C# are essential concepts for storing, transmitting, and reconstructing application data. Modern applications constantly exchange data between servers, databases, APIs, mobile applications, desktop applications, cloud platforms, and third-party services. Serialization and Deserialization in C# make this communication possible by converting objects into transferable formats such as JSON and XML and then converting them back into usable objects.
Serialization and Deserialization in C# are heavily used in ASP.NET Core Applications, Web APIs, Microservices, Mobile Applications, Cloud Computing, Enterprise Software Systems, E-Commerce Platforms, Banking Applications, and Hospital Management Systems.
Understanding Serialization and Deserialization in C# is crucial because almost every modern .NET application relies on data exchange and persistent storage.
Serialization is the process of converting an object into a format that can be:
Example:
Student student =
new Student();
student.Id = 101;
student.Name = "Rahul";
Serialization converts the object into:
{
"Id": 101,
"Name": "Rahul"
}
The object becomes portable and transferable.
Deserialization is the reverse process.
It converts:
{
"Id": 101,
"Name": "Rahul"
}
back into:
Student student
This allows applications to reconstruct objects from stored or transmitted data.
Serialization and Deserialization in C# help developers:
Without serialization, object data could not easily move between systems.
Modern .NET applications primarily use:
JavaScript Object Notation
Most popular format today.
Extensible Markup Language
Common in legacy and enterprise systems.
Used in older .NET applications.
Common in DevOps and configuration systems.
JSON is currently the most widely used format.
JSON stands for:
JavaScript Object Notation
Example:
{
"Id": 101,
"Name": "Rahul",
"Course": ".NET"
}
JSON is:
Most Web APIs use JSON.
Example:
public class Student
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string Course
{
get;
set;
}
}
This object can be serialized into JSON or XML.
Namespace:
using System.Text.Json;
Example:
Student student =
new Student
{
Id = 101,
Name = "Rahul",
Course = ".NET"
};
string json =
JsonSerializer.Serialize(
student);
Console.WriteLine(json);
Output:
{
"Id":101,
"Name":"Rahul",
"Course":".NET"
}
The object is converted into JSON.
Example:
string json =
@"{
""Id"":101,
""Name"":""Rahul"",
""Course"":"".NET""
}";
Deserialization:
Student student =
JsonSerializer.Deserialize<
Student>(json);
Output:
Rahul
The JSON data becomes a Student object again.
Example:
List<Student> students =
new List<Student>()
{
new Student
{
Id = 1,
Name = "Rahul"
},
new Student
{
Id = 2,
Name = "Amit"
}
};
Serialization:
string json =
JsonSerializer.Serialize(
students);
Output:
[
{
"Id":1,
"Name":"Rahul"
},
{
"Id":2,
"Name":"Amit"
}
]
Collections can also be serialized.
Example:
string json =
JsonSerializer.Serialize(
student);
File.WriteAllText(
"student.json",
json);
Output:
JSON File Created
This creates persistent storage.
Example:
string json =
File.ReadAllText(
"student.json");
Deserialization:
Student student =
JsonSerializer.Deserialize<
Student>(json);
Output:
Object Restored
The data is reconstructed.
Example:
JsonSerializerOptions options =
new JsonSerializerOptions
{
WriteIndented = true
};
string json =
JsonSerializer.Serialize(
student,
options);
Output:
{
"Id": 101,
"Name": "Rahul",
"Course": ".NET"
}
This improves readability.
XML remains important in many enterprise systems.
Namespace:
using System.Xml.Serialization;
Example:
XmlSerializer serializer =
new XmlSerializer(
typeof(Student));
XML Serialization converts objects into XML documents.
Student Object:
Student student =
new Student
{
Id = 101,
Name = "Rahul"
};
Serialization:
XmlSerializer serializer =
new XmlSerializer(
typeof(Student));
TextWriter writer =
new StreamWriter(
"student.xml");
serializer.Serialize(
writer,
student);
writer.Close();
Output:
<Student>
<Id>101</Id>
<Name>Rahul</Name>
</Student>
The object becomes XML.
Example:
XmlSerializer serializer =
new XmlSerializer(
typeof(Student));
TextReader reader =
new StreamReader(
"student.xml");
Student student =
(Student)
serializer.Deserialize(
reader);
Output:
Rahul
The XML data becomes a Student object.
| Feature | JSON | XML |
|---|---|---|
| Readability | High | Medium |
| Size | Smaller | Larger |
| Performance | Faster | Slower |
| API Usage | Very Common | Less Common |
| Enterprise Integration | Common | Very Common |
| Configuration Files | Common | Common |
Most modern APIs prefer JSON.
Controller:
return Ok(student);
ASP.NET Core automatically serializes:
Student
into:
{
"id":101,
"name":"Rahul"
}
This automatic serialization is heavily used in APIs.
Microservices exchange data using:
JSON Messages
REST APIs
Message Queues
Serialization enables communication between services.
Serialization used for:
Transaction Data
Customer Records
Audit Logs
Serialization used for:
Orders
Products
Shopping Cart Data
Serialization used for:
Patient Records
Appointments
Medical Reports
Serialization used for:
Student Information
Attendance
Exam Results
Serialization is everywhere in enterprise software.
Avoid exposing:
Passwords
Authentication Tokens
Credit Card Information
Always validate incoming JSON.
Large objects may affect performance.
Handle serialization exceptions properly.
JSON is lightweight and efficient.
Example:
Student
instead of:
object
Prevent malformed requests.
Avoid exposing confidential information.
Data Transfer Objects improve security and maintainability.
Example:
try
{
string json =
JsonSerializer.Serialize(
student);
}
catch(Exception ex)
{
Console.WriteLine(
ex.Message);
}
Always handle serialization failures gracefully.
Serialization converts objects into transferable formats such as JSON or XML.
Deserialization converts serialized data back into objects.
JSON is lightweight, readable, and API-friendly.
System.Text.Json
System.Xml.Serialization
Serialization enables data storage, transmission, and communication between systems.
Serialization converts objects into formats such as JSON or XML for storage or transmission.
Deserialization converts serialized data back into objects.
JSON is lightweight, fast, and widely supported.
It is Microsoft’s built-in JSON serialization library.
XML Serialization converts objects into XML documents.
They enable data exchange, storage, API communication, and enterprise integration.
WhatsApp us