It is very basic need to convert data into JSON and Serialize / Deserialize Objects in C#, so in this I have mentioned how to Serialize or Deserialize Object using System.Text.Json in C# using console application example.
Step 1: We will create a new console application in Visual Studio, so open Visual Studio 2019/2022 and then click on "Create new project" -> select "Console application" template and then select .NET Core Version(.NET 5, 6, or .NET 7) and click "OK"
Now, since we are using System.Text.Json which already comes with .NET Core 3.1 so, we don't need to install any package and we can directly create our sample class.
Step2: I will create sample class inside Program.cs to serialize/deserialize objects using System.Text.Json in C#
public class Student
{
public string Name { get; set; }
public List<string> Subjects { get; set; }
}
Step 3: Create methods to serialize and deserialize
public static string Serialize(Student student)
{
string jsonString = JsonSerializer.Serialize(student);
return jsonString;
}
public static Student DeSerialize(string jsonString)
{
Student? student = JsonSerializer.Deserialize<Student>(jsonString);
return student;
}
Step 4: After puting this all together, we can check serialize or deserialize class using System.Text.Json in C# as below
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace SerializeDeserializeCsharp
{
public class Student
{
public string Name { get; set; }
public List<string> Subjects { get; set; }
}
public class Program
{
static void Main(string[] args)
{
var student = new Student();
student.Name = "Vikram";
student.Subjects = new List<string>();
student.Subjects.Add("Maths");
student.Subjects.Add("Science");
student.Subjects.Add("Physics");
Console.WriteLine("Student Object after serialization:");
Console.WriteLine("----------------------------");
var jsonStr = Serialize(student);
Console.WriteLine(jsonStr);
Console.WriteLine();
Console.WriteLine();
var stu = DeSerialize(jsonStr);
Console.WriteLine("after DeSerialization:");
Console.WriteLine("---------------------------");
Console.WriteLine(stu.Name);
Console.WriteLine(string.Join(", ", stu.Subjects));
}
public static string Serialize(Student student)
{
string jsonString = JsonSerializer.Serialize(student);
return jsonString;
}
public static Student DeSerialize(string jsonString)
{
Student? student = JsonSerializer.Deserialize<Student>(jsonString);
return student;
}
}
}
Output:
Student Object after serialization:
----------------------------
{"Name":"Vikram","Subjects":["Maths","Science","Physics"]}
after DeSerialization:
---------------------------
Vikram
Maths, Science, Physics
As you can see from above output, after serialization using System.Text.JSON
in ASP.NET Core, student object is converted in JSON string.
Similarly, we can easily convert JSON string back to Student object using JsonSerializer.Deserialize<Type>(jsonString)
in C#.
In the above example, we did whole process of serialization or deserialization without installing Newtonsoft.JSON nuget package.
You may also like to read:
Deserialize XML string to Object in C#