Object Serialization in C# (.NET): JSON, XML and Binary
Serialization is the process of converting a complex object (or object graph) into a byte stream or text format (JSON, XML). This is necessary for saving an object's state to a file, transmitting data over a network (REST API, gRPC), or caching. The reverse process — deserialization — restores an object from its serialized representation. In the .NET ecosystem (including .NET Core and .NET 5/6/7/8), there are several built-in serialization mechanisms.
1. System.Text.Json — Modern JSON Serialization
Starting with .NET Core 3.0, Microsoft recommends using System.Text.Json as the primary way to work with JSON. The library is lightweight, high-performance, and does not require third-party dependencies (unlike Newtonsoft.Json).
Basic Serialization and Deserialization Example
using System;using System.Text.Json;
public class Person{ public string Name { get; set; } public int Age { get; set; } public List<string> Hobbies { get; set; }}
class Program{ static void Main() { var person = new Person { Name = "Alexey", Age = 30, Hobbies = new List<string> { "Reading", "Programming" } };
// Serialization string json = JsonSerializer.Serialize(person); Console.WriteLine(json); // {"Name":"Alexey","Age":30,"Hobbies":["Reading","Programming"]}
// Deserialization Person restored = JsonSerializer.Deserialize<Person>(json); Console.WriteLine(restored.Name); // Alexey }}Configuring Serialization with Attributes
You can control property names in JSON, ignore null values, or use custom converters:
using System.Text.Json.Serialization;
public class Product{ [JsonPropertyName("product_id")] public int Id { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; }
[JsonConverter(typeof(JsonStringEnumConverter))] public Status Status { get; set; }}
public enum Status { Active, Inactive }2. XmlSerializer — XML Serialization
The XmlSerializer class (namespace System.Xml.Serialization) is available in all .NET versions and is ideal for compatibility with legacy systems or configuration XML files.
Working with XmlSerializer Example
using System;using System.IO;using System.Xml.Serialization;
[XmlRoot("Employee")]public class Employee{ [XmlElement("FullName")] public string Name { get; set; }
[XmlAttribute("id")] public int Id { get; set; }
[XmlArray("Projects")] [XmlArrayItem("Project")] public List<string> Projects { get; set; }}
class Program{ static void Main() { var emp = new Employee { Id = 101, Name = "Maria Ivanova", Projects = new List<string> { "Alpha", "Beta" } };
var serializer = new XmlSerializer(typeof(Employee)); using (var writer = new StreamWriter("employee.xml")) { serializer.Serialize(writer, emp); }
// Deserialization using (var reader = new StreamReader("employee.xml")) { Employee restored = (Employee)serializer.Deserialize(reader); Console.WriteLine(restored.Name); // Maria Ivanova } }}Important: XmlSerializer requires the class to have a public parameterless constructor, and the serializable properties must be public and have setters.
3. BinaryFormatter (deprecated, but mentioned for legacy)
Previously, System.Runtime.Serialization.Formatters.Binary.Bi
Also in library
Working with CSV in Java: A Complete Guide to Reading and Writing
Database Migrations in Swift: Tools and Approaches for iOS/macOS
Working with XML in Swift (iOS/macOS): Foundation and SwiftXML
Pistache for C++: Complete Guide with Examples
Swift Application Configuration (iOS/macOS): Libraries and Tools