JSON Serialization and Deserialization in ASP.NET Core

JSON Serialization and Deserialization in ASP.NET Core

This blog is going to explain how to serialize and deserialize the JSON(JavaScript Object Notation) in ASP.NET Core. Here I used System.Text.Json namespace to serialize and deserialize the JSON. Let see an example, on how to serialize and deserialize the JSON.

First, I have created the Movies class as below.

public class Movies
 {
  public string Title { get; set; }
  public DateTime ReleaseDate { get; set; }
  public string Coproduction { get; set; }
 }

Serialization

The JSON Serialization is converting the object to JSON string

Serialization

The following is the C# Main method code. Here I have assigned some of the movie’s details and created a movieList object. Then I have serialized the object using JsonSerializer.Serialize() method.

using System.Text.Json;

class Program
{
    static void Main(string[] args)
    {
        List<Movies> movieList = new List<Movies>
        {
            new Movies()
        {
            Title = "Brave",
            ReleaseDate = DateTime.Parse("June 22, 2012"),
            Coproduction = "Pixar Animation Studios"
        },
            new Movies()
        {
            Title = "Frozen",
            ReleaseDate = DateTime.Parse("November 27, 2013"),
            Coproduction = "Walt Disney Animation Studios"
        },
            new Movies()
        {
            Title = "Aladdin",
            ReleaseDate = DateTime.Parse("May 24, 2019"),
            Coproduction = "Lin Pictures, Rideback and Marc Platt Productions"
        }
        };

        string jsonString = JsonSerializer.Serialize(movieList);

        Console.WriteLine(jsonString);
    }
}

The following is an output of the above code. Here you can see the movieList object values are serialized in JSON format.

 [{"Title":"Brave","ReleaseDate":"2012-06-22T00:00:00","Coproduction":"Pixar Animation Studios"},{"Title":"Frozen","ReleaseDate":"2013-11-27T00:00:00","Coproduction":"Walt Disney Animation Studios"},{"Title":"Aladdin","ReleaseDate":"2019-05-24T00:00:00","Coproduction":"Lin Pictures, Rideback and Marc Platt Productions"}] 

The serialization output above is unreadable.To make it readable, the .NET provides JsonSerializerOptions. WriteIndented = true property of JsonSerializerOptions helps to format the JSON string result. It makes the JSON result readable.

string jsonString = JsonSerializer.Serialize(movieList, new JsonSerializerOptions
{
    WriteIndented = true,
});

Console.WriteLine(jsonString);

The below is an output of the above code. Here you can see the JSON string is formatted and more readable.

[
  {
    "Title": "Brave",
    "ReleaseDate": "2012-06-22T00:00:00",
    "Coproduction": "Pixar Animation Studios"
  },
  {
    "Title": "Frozen",
    "ReleaseDate": "2013-11-27T00:00:00",
    "Coproduction": "Walt Disney Animation Studios"
  },
  {
    "Title": "Aladdin",
    "ReleaseDate": "2019-05-24T00:00:00",
    "Coproduction": "Lin Pictures, Rideback and Marc Platt Productions"
  }
]

Deserialization

The deserialization is converting the JSON string to object.

Next, we are going to see how to deserialize the JSON string.  In the following code, I have assigned the JSON string to the jsonString variable.  Then the string variable has been deserialized using  JsonSerializer.Deserialize() method

using System.Text.Json;
class Program
{
    static void Main(string[] args)
    {
        string jsonString = "[{\"Title\":\"Brave\",\"ReleaseDate\":\"2012-06-22T00:00:00\",\"Coproduction\":\"Pixar Animation Studios\"},{\"Title\":\"Frozen\",\"ReleaseDate\":\"2013-11-27T00:00:00\",\"Coproduction\":\"Walt Disney Animation Studios\"},{\"Title\":\"Aladdin\",\"ReleaseDate\":\"2019-05-24T00:00:00\",\"Coproduction\":\"Lin Pictures, Rideback and Marc Platt Productions\"}]";

        var movies = JsonSerializer.Deserialize<Movies[]>(jsonString);
    }
}

The following is an output from visual studio’s Immediate Window output. Here you can see how the JSON string values are deserialized.

movies
{SampleConsoleApp.Movies[3]}
    [0]: {SampleConsoleApp.Movies}
    [1]: {SampleConsoleApp.Movies}
    [2]: {SampleConsoleApp.Movies}

movies[0]
{SampleConsoleApp.Movies}
    Coproduction: "Pixar Animation Studios"
    ReleaseDate: {6/22/2012 12:00:00 AM}
    Title: "Brave"

movies[1]
{SampleConsoleApp.Movies}
    Coproduction: "Walt Disney Animation Studios"
    ReleaseDate: {11/27/2013 12:00:00 AM}
    Title: "Frozen"

movies[2]
{SampleConsoleApp.Movies}
    Coproduction: "Lin Pictures, Rideback and Marc Platt Productions"
    ReleaseDate: {5/24/2019 12:00:00 AM}
    Title: "Aladdin"

This article explains how to serialize and deserialize the JSON string.

If you have any questions, please leave a comment below.

Leave a Reply

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