JSON Serialization and Deserialization in C# involve converting objects to and from JSON format. The JsonSerializer class, available in the System.Text.Json namespace, is commonly used for this purpose. Here's an example demonstrating JSON serialization and deserialization in C#:
Here's a basic description about namespace that is required for serialization and deserialization with JSON. | |
using System.Text.Json; | Here, System.Text.Json is a namespace in .NET that provides high-performance APIs for working with JSON data. It allows for serializing and deserializing JSON objects, and is designed to be faster and more memory-efficient compared to other libraries like Newtonsoft.Json. It supports reading, writing, and manipulating JSON in a lightweight manner. |
Here's a basic breakdown of Serializable class | |
[Serializable] | Here, [Serializable] keyword in C# marks a class or struct as eligible for serialization, meaning its instances can be converted into a format (like binary or XML) for storage or transmission. It allows objects of the marked type to be easily saved and later restored. |
public class Fruit | Here, Fruit is parent class that is public which mean this class is accessible outside the class. |
public string? Name { get; set; } | Here, Name is property that will store name of fruit class. |
public int Price { get; set; } | Here, Price is property that will store price of fruit class. |
Here's a breakdown of CSharpProgram main class. | |
Fruit fruit = new Fruit { Name = "Apple", Price = 26 }; | This code creates a new instance of the Fruit class and initializes its properties, Name and Price, with the values "Apple" and 26, respectively, using an object initializer syntax. |
string json = SerializeObjectToJson(fruit); | This code calls the SerializeObjectToJson function, passing the fruit object, and stores the resulting JSON string in the json variable. The function likely converts the object into a JSON-formatted string. |
Console.WriteLine("Serialized Json:"); Console.WriteLine(json); |
This code prints the message "Serialized Json:" to the console, followed by the JSON string stored in the json variable, displaying the serialized object data. |
Fruit deserializedPerson = DeserializeJsonToObject(json); | This code calls the DeserializeJsonToObject function, passing the json string, and stores the resulting Fruit object in deserializedPerson. This function likely converts the JSON string back into a Fruit object. |
Console.WriteLine("Deserialized Json:"); Console.WriteLine($"Name: {deserializedPerson.Name}, Price: {deserializedPerson.Price}"); |
This code prints "Deserialized Json:" to the console, followed by the Name and Price properties of the deserializedPerson object, which was reconstructed from the JSON string. |
static string SerializeObjectToJson(Fruit obj) { return JsonSerializer.Serialize(obj); } |
This method takes a Fruit object (obj) as input and returns its JSON representation as a string using JsonSerializer.Serialize(). |
static Fruit DeserializeJsonToObject(string json) { return JsonSerializer.Deserialize } |
This method takes a JSON string as input and deserializes it into a Fruit object using JsonSerializer.Deserialize |
// Sample class to be serialized
// Create an instance of the class
// Serialize the object to JSON
// Display the serialized JSON
// Deserialize the JSON to an object
// Display the deserialized object
// Serialize an object to JSON
// Deserialize JSON to an object
// Sample class to be serialized
// Create an instance of the class
// Serialize the object to JSON
// Display the serialized JSON
// Deserialize the JSON to an object
// Display the deserialized object
// Serialize an object to JSON
// Deserialize JSON to an object