In this demonstration, you will see how to serialize and deserialize objects using JsonSerializer and JsonConvert
In the example below, we have a Person class with Name and Age properties. We create an instance of the Person class with some values. Then, we use JsonSerializer.Serialize to convert the object to a JSON string.
Next, we print the serialized JSON string to the console.
After that, we use JsonSerializer.Deserialize to convert the JSON string back into an instance of the Person class. Finally, we print the properties of the deserialized object to verify that the serialization and deserialization process worked correctly.
Make sure to include the System.Text.Json namespace in your code to access the JsonSerializer class and related types.
using System;
using System.IO;
using System.Text.Json;
namespace SerializationDemo
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the object
Person person = new Person
{
Name = "Raushan Ranjan",
Age = 25
};
// Serialize the object to JSON
string jsonString = JsonSerializer.Serialize(person);
Console.WriteLine("Serialized JSON:");
Console.WriteLine(jsonString);
Console.WriteLine();
// Deserialize the JSON back to an object
Person deserializedPerson = JsonSerializer.Deserialize<Person>(jsonString);
Console.WriteLine("Deserialized Object:");
Console.WriteLine($"Name: {deserializedPerson.Name}");
Console.WriteLine($"Age: {deserializedPerson.Age}");
}
}
}
If you prefer to use
Newtonsoft.Json (also known as Json.NET) for serialization and deserialization, here's an example using Newtonsoft.Json:
using System;
using Newtonsoft.Json;
namespace SerializationDemo
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the object
Person p1 = new Person
{
Name = "Raushan Ranjan",
Age = 25
};
// Serialize the object to JSON
string jsonString = JsonConvert.SerializeObject(p1);
Console.Write("Serialized JSON: ");
Console.WriteLine(jsonString);
Console.WriteLine();
// Deserialize the JSON back to an object
var deserializedPerson = JsonConvert.DeserializeObject<Person>(jsonString);
Console.Write("Deserialized Object: ");
Console.WriteLine($"Name: {deserializedPerson.Name}");
Console.WriteLine($"Age: {deserializedPerson.Age}");
}
}
}
In the above example, we have the same Person class with Name and Age properties. We use JsonConvert.SerializeObject to convert the object to a JSON string and then print it to the console.
For deserialization, we use JsonConvert.DeserializeObject to convert the JSON string back into an instance of the Person class. Finally, we print the properties of the deserialized object to verify the correctness of the serialization and deserialization process.
Make sure to include the Newtonsoft.Json namespace in your code to access the JsonConvert class and related types.
Note: Newtonsoft.Json is a popular third-party library for JSON serialization and deserialization, widely used in the .NET ecosystem.
Comments
Post a Comment