JSON Serialization in C
JSON Serialization:
The process of transferring data structures or objects from a programming language's native format into JSON (JavaScript Object Notation) format is known as serialization. JSON is a simple, human-readable data exchange format that is frequently used for sharing structured data across systems, storing configuration information, and delivering data between a client and a server.
When a complicated data structure, such as an object or class, is serialized, it is done so that it may be simply represented as a string of text. This serialized string can then be used for data exchange by being sent over a network, saved in a file, or in another way.
JSON Serialization in C#:
- The process of serializing JSON (JavaScript Object Notation) in C# entails transforming C# objects or data structures into JSON format.
- You can express data using this approach in a way that is both legible by humans and portable.
- JSON serialization is possible in C# utilizing a variety of libraries and frameworks, with System.Text.Json (included in.NET Core and.NET 5+) and JSON.NET (Newtonsoft.Json) being the two most well-liked choices.
Here is a C# example of JSON serialization using Newtonsoft.Json and System.Text.Json. The following example shows how to serialize and deserialize a straightforward Person object:
using System;
using System.Text.Json;
public class Person
{
public string firstname { get; set; }
public string lastname { get; set; }
public int age { get; set; }
}
class Program
{
static void Main()
{
// Create a Person object
Person person = new Person
{
firstname = "John",
lastname = "Doe",
age = 30
};
// JSON Serialization using System.Text.Json
string jsonTextSystemTextJson = JsonSerializer.Serialize(person);
Console.WriteLine("System.Text.Json Serialization:");
Console.WriteLine(jsonTextSystemTextJson);
// JSON Deserialization using System.Text.Json
Person deserializedPersonSystemTextJson = JsonSerializer.Deserialize<Person>(jsonTextSystemTextJson);
Console.WriteLine("Deserialized Person:");
Console.WriteLine($"Name: {deserializedPersonSystemTextJson.FirstName} {deserializedPersonSystemTextJson.LastName}, Age: {deserializedPersonSystemTextJson.Age}");
}
}
Output:
![JSON Serialization in C#](https://static.tutorialandexample.com/c/json-serialization-in-c1.png)
Explanation:
- First, we import the required namespaces:
- The fundamental data types and essential input/output capabilities are provided via the system namespace.
- System.Text.JSON serialization and deserialization take place using the JSON namespace.
- FirstName, LastName, and Age are the three properties we define for the Person class. The data of a person is represented by this class.
- We designate the Main method as the program's entry point inside the Programme class. Using object initialization syntax, we construct an object called Person and initialize its properties.
- JsonSerializer is employed. To create a JSON-formatted string with the name jsonTextSystemTextJson, use the serialize command on the person object.
- To mark the beginning of the serialization output, we print the header "System.Text.Json Serialisation:".
- We use the Console to print the serialized person object's JSON-formatted string representation.WriteLine.
- JsonSerializer is employed. To convert the JSON-formatted text jsonTextSystemTextJson back into a Person object with the name deserializedPersonSystemTextJson, use the deserialize command.
- To mark the beginning of the deserialization output, we print the header "Deserialized Person:".
- The FirstName, LastName, and Age properties of the deserialized Person object are printed.
The program performs the following when it is run:
- Uses a system to serialize the person object into a JSON string. The serialized JSON is shown using Text.Json.
- Uses a system to deserialize the JSON string back into a Person object. Information about the deserialized person is displayed in Text.Json.
- The program uses a System to show how to serialize and deserialize JSON.Text.Json. It illustrates how to change a C# object into a JSON string, and subsequently, that JSON string can be changed back into a C# object.
Several scenarios make use of JSON serialization, including:
Web APIs:
Data is frequently serialized into JSON format before being transmitted as replies to client queries when developing web services or APIs. The client can then access the data by deserializing the JSON response.
Configuration Files:
JSON is frequently used to store application parameters or configuration information in a format that is readable by humans.
Client-server Communication:
JSON serialization is used for client-server communication, allowing applications running on the client and server to exchange structured data.
Data Storage:
JSON offers a standardized method to store and retrieve structured data. It can be used to persist data in databases or flat files.
Cross-Platform Data Exchange:
Because JSON is platform-independent, it may be used to transfer data between many platforms and programming languages.
JSON serialization is frequently used in the following scenarios:
Microservices and Service-Oriented Architecture (SOA):
Different services frequently exchange data across service boundaries using JSON serialization in microservices and SOA.
Data reporting and visualization:
Data visualization tools can quickly ingest JSON data, enabling you to generate reports and visualizations using the serialized data.
Develop mobile applications:
To transfer data between mobile apps and distant servers, JSON serialization is frequently employed in mobile app development.
IoT: "Internet of Things":
JSON serialization can be used in IoT applications to transfer data between linked devices and centralized servers.
Conclusion
JSON serialization, in general, facilitates effective data sharing, interoperability, and communication between various application components as well as between various platforms. It offers a standardized and well-accepted method of representing structured data in a format that is legible by humans.