Introduction
JSON (JavaScript Object Notation) is a lightweight data interchange format that has become the de facto standard for communication between server and client, as well as for storing configurations. In the C# (.NET) ecosystem, working with JSON is implemented at the highest level: developers have access to both built-in libraries and powerful third-party solutions. In this article, we will use practical examples to break down how to perform JSON parsing, serialization, and deserialization in C# using modern approaches.
We will look at two main tools: System.Text.Json (built-in library since .NET Core 3.0+) and Newtonsoft.Json (popular third-party library). You will learn how to convert C# objects to JSON and back, how to work with nested structures, and how to handle errors.
Basics of Serialization and Deserialization in C#
Before diving into the code, let's define the terms. Serialization is the process of converting a C# object (e.g., a class instance) into a JSON string. Deserialization is the reverse process: we reconstruct a C# object from a JSON string. In C#, attributes and special serializer classes are used for this.
1. Working with System.Text.Json (Built-in Library)
Starting with .NET Core 3.0, Microsoft introduced its own library for working with JSON — System.Text.Json. It is faster than Newtonsoft.Json and does not require installing additional packages.
Let's consider a simple Person class:
public class Person{ public string Name { get; set; } public int Age { get; set; } public string Email { get; set; }}Serialization to JSON:
using System.Text.Json;
var person = new Person{ Name = "Alexey", Age = 30, Email = "alex@example.com"};
string json = JsonSerializer.Serialize(person);Console.WriteLine(json);// Output: {"Name":"Alexey","Age":30,"Email":"alex@example.com"}Deserialization from JSON:
string jsonString = @"{""Name"":""Maria"",""Age"":25,""Email"":""maria@example.com""}";Person? person = JsonSerializer.Deserialize<Person>(jsonString);Console.WriteLine(person?.Name); // MariaNote the ? operator (nullable) — deserialization may return null on error.
2. Working with Newtonsoft.Json (Json.NET)
Despite the emergence of System.Text.Json, Newtonsoft.Json remains a popular choice due to its flexibility and extensive customization options. To use it, install the NuGet package Newtonsoft.Json.
Example with the same Person class:
using Newtonsoft.Json;
// Serializationvar person = new Person { Name = "Ivan", Age = 35, Email = "ivan@example.com" };string json = JsonConvert.SerializeObject(person);Console.WriteLine(json);
// Deserializationstring jsonInput = @"{'Name':'Olga','Age':28,'Email':'olga@example.com'}";Person? personDeserialized = JsonConvert.DeserializeObject<Person>(jsonInput);Console.WriteLine(personDeserialized?.Name); // OlgaNewtonsoft.Json by default supports both double and single quotes, making it more tolerant of "dirty" JSON.
Advanced Scenarios: Working with Nested Objects and Collections
In real projects, JSON is rarely flat. Let's look at an example with nested data.
Suppose we have a Company class containing a list of employees:
public class Employee{ public string Name { get; set; } public string Position { get; set; }}
public class Company{ public string CompanyName { get; set; } public List<Employee> Employees { get; set; }}Serialization (System.Text.Json):
Blogs
Book Recommendations
Read also
Top 10 Mistakes Beginner Java Developers Make and How to Avoid Them
GraphQL API with Go: Step-by-Step Practical Tutorial
Algorithms and Data Structures in PHP: A Practical Guide for Web Developers
Regular Expressions in PHP: A Complete Guide with Examples
Working with Databases in TypeScript: PostgreSQL and MongoDB for Beginners