What is JSON Serialization and Deserialization in C#

Introduction

JSON(JavaScript Object Notation) is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). JSON has become a ubiquitous data interchange format, especially in web development.

In C#, developers have multiple options for serializing C# objects to JSON and deserializing JSON strings back to C# objects. In this article, we'll explore two popular approaches to achieve this:

using the built-in System.Text.Json namespace and the widely used JSON.NET library (Newtonsoft.Json).

1. Using System.Text.Json


1.1 Serialization

System.Text.Json, introduced in .NET Core 3.0 and .NET 5, provides a fast and lightweight JSON serializer. To serialize an object to JSON using System.Text.Json, developers can use the JsonSerializer.Serialize method. This method serializes objects to JSON strings with options for customization.

using System;
using System.Text.Json;

class Program
{
    static void Main(string[] args)
    {
        // Object to serialize
        var person = new Person { Name = "John", Age = 30, City = "New York" };

        // Serialize object to JSON
        string jsonString = JsonSerializer.Serialize(person);

        // Display serialized JSON
        Console.WriteLine("Serialized JSON:");
        Console.WriteLine(jsonString);
    }

    // Sample class
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
}

1.2 Deserialization

Deserializing JSON strings back to objects is achieved using the JsonSerializer.Deserialize method. This method parses JSON strings and constructs C# objects based on the JSON data provided.

using System;
using System.Text.Json;

class Program
{
    static void Main(string[] args)
    {
        // JSON string to deserialize
        string jsonString = "{\"Name\":\"John\",\"Age\":30,\"City\":\"New York\"}";

        // Deserialize JSON to object
        Person person = JsonSerializer.Deserialize<Person>(jsonString);

        // Display deserialized object
        Console.WriteLine("Deserialized Object:");
        Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, City: {person.City}");
    }

    // Sample class
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
}

2. Using Newtonsoft.Json (JSON.NET)


2.1 Serialization

JSON.NET, also known as Newtonsoft.Json, is a popular and feature-rich JSON library for .NET. It offers extensive customization options and is widely adopted in the .NET ecosystem. Serialization with JSON.NET is accomplished using the JsonConvert.SerializeObject method, which converts C# objects to JSON strings.

using System;
using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        // Object to serialize
        var person = new Person { Name = "John", Age = 30, City = "New York" };

        // Serialize object to JSON
        string jsonString = JsonConvert.SerializeObject(person);

        // Display serialized JSON
        Console.WriteLine("Serialized JSON:");
        Console.WriteLine(jsonString);
    }

    // Sample class
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
}

2.2 Deserialization

Deserialization in JSON.NET is performed using the JsonConvert.DeserializeObject method. This method parses JSON strings and reconstructs C# objects based on the JSON data provided.

using System;
using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        // JSON string to deserialize
        string jsonString = "{\"Name\":\"John\",\"Age\":30,\"City\":\"New York\"}";

        // Deserialize JSON to object
        Person person = JsonConvert.DeserializeObject<Person>(jsonString);

        // Display deserialized object
        Console.WriteLine("Deserialized Object:");
        Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, City: {person.City}");
    }

    // Sample class
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
}

3. Comparison between JSON Serialization and Deserialization

  • 3.1 Performance: System.Text.Json is known for its performance and low memory usage, making it an excellent choice for high-performance scenarios. JSON.NET, while still performant, may not match the raw speed of System.Text.Json in certain scenarios.
  • 3.2 Flexibility and Features: JSON.NET offers a wide range of features and customization options, allowing developers to fine-tune the serialization and deserialization process. It provides extensive support for handling complex JSON structures, custom converters, and more. System.Text.Json, on the other hand, aims for simplicity and may lack some advanced features compared to JSON.NET.

Conclusion

Both System.Text.Json and JSON.NET offer powerful tools for working with JSON data in C#. Developers can choose between the two based on their specific requirements, performance considerations, and familiarity with the libraries. System.Text.Json provides a lightweight and performant option built into the .NET platform, while JSON.NET offers extensive features and flexibility for handling complex JSON scenarios. Ultimately, the choice between the two comes down to the specific needs of the project and the preferences of the development team.


Similar Articles