System.Text.Json – Serialization and Deserialization in .NET Core

SystemTextJson Serialize and Deserialize in NET

Today in this article we will see how to perform System.Text.Json – Serialization and Deserialization in .NET.

We shall see how to convert Object to JSON and JSON to object in .NET or .NET Core using a new serializer i.e System.Text.Json

We shall see below aspects in today’s article,

.NET allows us to perform serialize and deserialize using 3 techniques like Binary serialization, XML or SOAP serialization, and very popular JSON serialization.

Serialization and Deserialization is a process of transforming the state of an object into a form that can be Persisted or Transported and as and when required it can be restored.

Today we shall be covering in brief how to perform JSON serialization and deserialization in this article.

Getting started

Let’s create any .NET application.

Please note that System.Text.Json is built into the .NET Core 3.0 + above frameworks like .NET 3.1 and recently released .NET 6

I shall be using the .NET Core console application today to articulate the usage of JSON.Net.

To use System.Text.Json – Serialization, and Deserialization in .NET Core, all you need is the below-using namespaces to be added to each of your files requiring serializing or deserialize logic.

using System.Text.Json;

using System.Text.Json.Serialization;

System.Text.Json – .NET Framework and lower .NET Core framework support

If you need to perform JSON serialization and deserialization in other applications like .NET Standards or .NET framework then System.Text.Json is not built into the framework and it needs to be installed through Nuget packages.

For the below frameworks, System.Text.Json  is available through NuGet Package.

  • .NET Standard 2.0 and +
  • .NET Framework 4.7.2 and +
  • .NET Core 2.0, 2.1, and 2.2

Please install the System.Text.Json using the NuGet package.

Using PMC,

PM> Install-Package System.Text.Json -Version <version>

System.Text.Json – Serialize .NET objects to JSON

I have a simple C# class entity i.e Employee as shown below,

public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmployeeId { get; set; }
        public Address Address { get; set; }
    }
    public class Address
    {
        public string ZipCode { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }

Let’s use below JsonSerializer.Serialize() method to serialize the .NET object to JSON string ,

        Employee empDetails = new Employee
            {
                FirstName = "ABCD",
                LastName = "TEST",
                Address = new Address { Country = "USA", State = "CA", ZipCode = "1234" },
                EmployeeId = "1111"
            };
           
            string jsonOutPut = JsonSerializer.Serialize(empDetails);
..
..

If this is serialized to JSON, below is the output we shall get,

SystemTextJson Serialization and Deserialization in NET

Serialize Newtonsoft VS SystemText.JSON

The below method comparison is just to show you the highlight of basic differences in the API/method used for JSON serialization and deserialization using both techniques.

NewtonSoft.JSON method

While using Newtonsoft JSON we use JsonConvert.SerializeObject method for serialization.

  JsonConvert.SerializeObject(emp); 

System.Text.Json

For Text.Json, we use JsonSerializer.Serialize method for serialization.

JsonSerializer.Serialize(emp)

System.Text.Json – De-Serialize JSON to .NET Objects

Let’s use a sample JSON string to deserialize it as below.

{
  "FirstName": "ABCD",
  "LastName": "TEST",
  "EmployeeId": "1111",
  "Address": {
    "ZipCode": "1234",
    "State": "CA",
    "Country": "USA"
  }
}

Please use the below JsonSerializer.Deserialize() method to deserialize as below,

Employee empObject =  JsonSerializer.Deserialize<Employee>(jsonResponseString);

SystemTextJson Serialize

De-Serialize API Newtonsoft VS Text.JSON

NewtonSoft.JSON API

While using Newtonsoft JSON we use JsonConvert.DeserializeObject method for de-serialization.

  JsonConvert.DeserializeObject<Type>(jsonString)

System.Text.Json

For Text.Json, we use JsonSerializer.Deserialize method for serialization.

 JsonSerializer.Deserialize<Type>(jsonString);

As per Microsoft,

A value enclosed in single quotes will result in a JsonException. System.Text.Json shall accept property names and string values only in double-quotes as per  RFC 8259 specification.

SystemTextJson Deserialize in NET Core

There are a few additional limitations as per the design including reference loop handling supports or support for System.Type etc. which I shall be covering in my next article.

This was very much basic on the Serialization and Deserialization techniques we covered today using System.Text.Json.

References :

That’s All !! Happy Coding.

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “System.Text.Json – Serialization and Deserialization in .NET

  1. Nicely organized article. Thank you for writing. Could you please provide an example to deserialize into Data Table? Also, if possible asynchronous calls.

    Thanks,

    1. Hello Khushi,Glad it helped you. Deserializing the data table can be done by writing Custom Converter if using System.Text.json.
      I shall soon put an article on the custom converter for DataTable or Dataset!

Leave a Reply

Your email address will not be published. Required fields are marked *