Object Serialization in C# (.NET): Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Object Serialization in C# (.NET): From Basics to Advanced Techniques



Serialization is the process of converting complex objects (classes, structures) into a format that can be saved to a file, transmitted over a network, or written to a database. The reverse process (restoring an object from a data stream) is called deserialization. In the .NET platform, there are several built-in and third-party tools for serialization, each with its own features, performance characteristics, and areas of application.



In this article, we will cover the main serialization methods in C#: JSON (System.Text.Json and Newtonsoft.Json), XML (XmlSerializer), binary serialization (BinaryFormatter — deprecated but still encountered), and Protocol Buffers (protobuf-net) for high-performance scenarios.



1. JSON Serialization with System.Text.Json



Starting with .NET Core 3.0, Microsoft recommends using the System.Text.Json namespace for working with JSON. It is a fast, low-level, and asynchronous API.



Serialization and deserialization example:



using System;using System.Text.Json;

public class Person{ public string Name { get; set; } public int Age { get; set; } public string Email { get; set; }}

class Program{ static void Main() { var person = new Person { Name = "Ivan Petrov", Age = 30, Email = "ivan@example.com" };

// Serialize to JSON string string jsonString = JsonSerializer.Serialize(person); Console.WriteLine(jsonString); // Output: {"Name":"Ivan Petrov","Age":30,"Email":"ivan@example.com"}

// Deserialize from JSON string jsonInput = @"{""Name"":""Maria"",""Age"":25,""Email"":""maria@test.ru""}"; Person restoredPerson = JsonSerializer.Deserialize<Person>(jsonInput); Console.WriteLine($"Name: {restoredPerson.Name}, Age: {restoredPerson.Age}"); }}


Serialization configuration:

  • Ignoring null properties: JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }
  • Pretty printing: JsonSerializerOptions { WriteIndented = true }
  • Handling circular references: ReferenceHandler.Preserve


2. Newtonsoft.Json (Json.NET) — The Classic



Before System.Text.Json, the de facto standard was the Newtonsoft.Json library (installed via NuGet). It supports more scenarios: dynamic objects, attributes, converters, and flexible settings.



using Newtonsoft.Json;

public class Order{ public int Id { get; set; } public decimal Amount { get; set; } [JsonIgnore] public string InternalNote { get; set; } // This field will not be serialized}

var order = new Order { Id = 1, Amount = 99.99m, InternalNote = "Secret" };string json = JsonConvert.SerializeObject(order, Formatting.Indented);Console.WriteLine(json);// {// "Id": 1,// "Amount": 99.99// }

var deserialized = JsonConvert.DeserializeObject<Order>(json);


3. XML Serialization (XmlSerializer)



For the XML format, .NET uses the XmlSerializer class. It is well-suited for compatibility with legacy systems and SOAP services.



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

[Serializable]public class Product{ [XmlAttribute] public int Id { get; set; } [XmlElement("ProductName")] public string Name { get; set; }}

class Program{ static void Main() { var product = new Product { Id = 42, Name = "Laptop" };

// Serialize

Recommendations