JSON Serialization and Deserialization in C#

Prakash Kumar
6 min readAug 11, 2021

--

In this post, I will demonstrate JSON serialization and deserialization and tricks relating to JSON formats.

The Sections of this post will be as follows:

1. What is JSON?

2. What are the different styles of JSON?

3. JSON serialization

4. JSON deserialization

5. LINQ to JSON

If you are ready let’s get started.

  1. What is JSON?

JSON stands for JavaScript Object Notation.

JSON is a text format for storing and transporting data.

JSON is one kind of data format which is designed for running JavaScript on websites. At present, JSON is widely used in web.

JSON is “name/value” assembly. Its structure is made up with {}, [], comma, colon and double quotation marks.

1.1) What are the valid data types in JSON? ?

Object, Number, Boolean, String, Array, Null

1.2) What are the invalid data types in JSON?

JSON values cannot be a function, date or undefined.

2.What are the different styles of JSON?

JSON has three styles:

1. Object
An unordered “name/value” assembly. An object begins with “{“ and ends with “}”. Behind each “name”, there is a colon. And comma is used to separate much “name/value”. For example,

2. Array
Value order sets. An array begins with “[“ and end with “]”. And values are separated with commas. For example,

3. String
Any quantity Unicode character assembly which is enclosed with quotation marks. It uses a backslash to escape.

Now that we have an idea about JSON let’s do a bit of coding and understand how serialization, deserialization and LINQ to JSON works.

To demonstrate this, I created a console application in visual studio.

First lets install the required Nuget package.

In Visual Studio, go to Tools Menu -> Choose Library Package Manger -> Package Manager Console. It opens a command window where we need to put the following command to install Newtonsoft. Json.

Install-Package Newtonsoft.Json

Or In Visual Studio, Tools menu -> Manage Nuget Package Manger Solution and search “Newtonsoft.Json”. Here’s the figure:

Newtonsoft. Json is a third party library which helps conversion between JSON text and .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent text and back again by mapping the .NET object property names to the JSON property names. It is open source software and free for commercial purpose.

Benefits and Features:

  • Flexible JSON serializer for converting between .NET objects and JSON.
  • LINQ to JSON for manually reading and writing JSON.
  • High performance, faster than .NET’s built-in JSON serializers.
  • Easy to read JSON.
  • Convert JSON to and from XML.
  • Supports .NET 2, .NET 3.5, .NET 4, Silverlight and Windows Phone.

The JSON serializer in this library is a good choice when the JSON you are reading or writing maps closely to a .NET class.

3. JSON Serialization

It converts a .NET object to JSON format text. For instance, there is a Student object holding data and we need to convert the object to JSON format. To demonstrate JSON serialization, we will create a Student class with ID, Name and Country properties. We will utilize this class in coming sections of this article. Here’s the code:

Create a class Student.cs having three properties ID, Name, Country.

Now in Program.cs create an object of Student class and assign a required value to its properties. Then call SerializeObject() of JsonConvert class with a passing student object — it returns JSON format text. Here is the code:

The following figure shows the result after generating JSON text from .NET object:

4. JSON Deserialization

It is a reverse process of JSON Serialization, which we discussed in the previous section. It converts JSON format text to .NET objects. For instance, we have a string value in JSON format text that holds information about a student. Now we want to convert that JSON text to a .NET student object which will map all properties of student objects.

For demonstrating deserialization we have JSON formatted data:

Now we will convert it to a .NET object using DeserializeObject () method of JsonConvert class. Here is the code:

The following output shows the properties of .NET after deserialization of JSON data.

5. LINQ to JSON

LINQ to JSON is an API for working with JSON objects. It has been designed with LINQ in mind to enable quick querying and creation of JSON objects. LINQ to JSON sits under the Newtonsoft.Json.Linq namespace.

LINQ to JSON is good for situations where you are only interested in getting values from JSON, you don’t have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects.

Now lets install the necessary “Newtonsoft.Json.Schema” nuget package the same way we installed “Newtonsoft.Json” package.

Install-Package Newtonsoft.Json.Schema

Here’s the figure after package installation:

It is designed to parse JSON data and querying it like LINQ. For instance, a Student who plays multiple games and we want to fetch only games that contain character “B” (it is discussed below). Here LINQ to JSON plays a vital role to accomplish this. It also helps to create JSON Objects.

In the following example, we have student information in JSON format. And the target is to fetch all games that contain character “B”. Here’s the code:

Lets Discuss about few JSON classes used in the above code.

JObject

It represents a JSON Object. It is used to parse JSON data and apply querying (LINQ) to filter out required data. It is presented in Newtonsoft.Json.Linq namespace.

JArray

It represents a JSON Array. We can add elements to JArray object and convert into JSON string. It presents in Newtonsoft.Json.Linq namespace.

In this article we discussed what is JSON and its different styles. We used “Newtonsoft.Json” package to serialize and deserialize an object in ASP.NET.

Lastly, we checked LINQ to JSON its advantages and ease of use to query a JSON data.

If you love the article a clap would be appreciated.

Twitter: https://twitter.com/realkumar07

--

--

Prakash Kumar

A software developer who loves to learn new things and share my knowledge with the world