Introduction
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is becoming increasingly popular for communication between applications and services. It is a text-based format that is easy to read and write, and it is platform-independent, meaning that it can be used on any operating system. So, how do you serialise and deserialise JSON?
In C#, two main libraries can be used to serialize and deserialize JSON data:
- Newtonsoft.Json: This is a popular open-source library that is included in the NuGet package repository. It is a mature and well-supported library that is widely used in the C# community.
- System.Text.Json: This is a new library that was introduced in C# 10. It is more performant than Newtonsoft.Json and it is also more secure. It is also the default library for serialization and deserialization in C# 10 and later.
In this blog post, we will show you how to use both of these libraries to serialize and deserialize JSON data.
Serialisation with Newtonsoft.Json
Serialisation is the process of converting data from a program into a format that can be stored or transmitted. In the case of JSON, serialisation involves converting objects into a string representation of the object’s data.
To serialize an object using Newtonsoft.Json, you can use the JsonSerializer.SerializeObject() method. This method takes an object as input and returns a JSON string as output.
Here is an example of how to serialize a Product object into a JSON string using Newtonsoft.Json:
using Newtonsoft.Json;
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Product product = new Product
{
Id = 1,
Name = "Laptop",
Price = 1299.99
};
string jsonString = JsonSerializer.SerializeObject(product);
The jsonString variable now contains the following JSON string:
{
"Id": 1,
"Name": "Laptop",
"Price": 1299.99
}
Deserialisation with Newtonsoft.Json
Deserialisation is the process of converting data from a JSON format into a program. In the case of JSON, deserialisation involves converting a JSON string back into an object.
To deserialise a JSON string using Newtonsoft.Json, you can use the JsonSerializer.DeserializeObject() method. This method takes a JSON string as input and returns an object of the specified type.
Here is an example of how to deserialise a JSON string into a Product object using Newtonsoft.Json:
using Newtonsoft.Json;
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
var jsonString = "{ \"Id\": 1, \"Name\": \"Laptop\", \"Price\": 1299.99 }";
var product = JsonSerializer.DeserializeObject<Product>(jsonString);
The product variable now contains a Product object with the following properties:
Id = 1
Name = "Laptop"
Price = 1299.99
Serialisation with System.Text.Json
Serialisation with System.Text.Json is similar to serialisation with Newtonsoft.Json. The main difference is that System.Text.Json uses a different syntax for defining the object structure.
Here is an example of how to serialise a Product object into a JSON string using System.Text.Json:
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
var product = new Product
{
Id = 1,
Name = "Laptop",
Price = 1299.99
};
var options = new JsonSerializerOptions
{
WriteIndented = true
};
var jsonString = JsonSerializer.Serialize(product, options);
Conclusion
Serialisation and deserialising data is a vital tool in today’s applications.
You can find the official documentation for the Newtonsoft JSON library here: – https://www.newtonsoft.com/json/help/html/serializingjson.htm.
I have a related post here: – SQL JSON: Taming the Wild World of JSON Data in SQL Server.