Serialization And Deserialization(JSON) Using C#

Introduction

 
Serialization
 
Serialization means converting the state of an object into formatted text (json, xml, etc.).
 
Deserialization
 
Deserialization means converting a formatted text (json, xml, etc.) into sthe tate of an object.
 
We will see with an example by converting an object to Json and Json to an object.
 
For this we will use Newtonsoft.Json package (you can download it from Nuget Package Manager).
 
First we will create one class Person to refer as object.
 
Below is the Person class,
  1. public class Person  
  2. {  
  3.        public string Name;  
  4.        public int Age;  
  5.        public string Gender;  
  6.        public bool IsStudent;  
  7. }  
Below is the code to covert from object to Json.
  1. namespace Serialization.Deserialization.Demo  
  2. {  
  3.     using Newtonsoft.Json;  
  4.     using static System.Console;      
  5.     public class Program  
  6.     {  
  7.         public static void Main()  
  8.         {  
  9.             Person person = new Person  
  10.             {  
  11.                 Name = "ABC",  
  12.                 Age = 20,  
  13.                 Gender = "Mail",  
  14.                 IsStudent = true  
  15.             };  
  16.   
  17.             string json = JsonConvert.SerializeObject(person, Formatting.Indented);  
  18.             WriteLine(json);  
  19.             ReadLine();  
  20.         }  
  21.     }  
  22. }  
In the above code we can see that we defined Person class with Name, Age, Gender, IsStudent.
 
And I'm using JsonConvert.SerializeObject method to convert it to object to json format. This phenomenon is known as serialization
 
Below is the output snap, In output snap we can see that the person object we converted to text i.e. in json format,
 
Serialization and Deserialization(JSON) using  C#  
 
Below is the code to convert Json string to object.
  1. namespace Serialization.Deserialization.Demo  
  2. {  
  3.     using Newtonsoft.Json;  
  4.     using static System.Console;      
  5.     public class Program  
  6.     {  
  7.         public static void Main()  
  8.         {  
  9.             string jsonText = "{\"Name\":\"ABC\",\"Age\":20,\"Gender\":\"Mail\",\"IsStudent\":true}";  
  10.             Person person = JsonConvert.DeserializeObject<Person>(jsonText);  
  11.             WriteLine($"person.Name : {person.Name}");  
  12.             WriteLine($"person.Age : {person.Age}");  
  13.             WriteLine($"person.Gender : {person.Gender}");  
  14.             WriteLine($"person.IsStudent : {person.IsStudent}");  
  15.             ReadLine();  
  16.         }  
  17.     }  
  18. }  
In the above code we can see that, we have used JsonConvert.DeserializeObject to convert Json string to Person object and when we are invoking JsonConvert.DeserializeObject we are passing Person object to indicate that, the input string or whatever we provide should be Deserialized into the Person object.
 
Below is the output snap. In the output snap we can see that from deserialized object I am able to access Name, Age, Gender and IsStudent Property.
 
Serialization and Deserialization(JSON) using  C# 
 

Summary

 
From this blog we understood about serialization and deserialization. We can serialize object into Json, XML, Yaml  as well and vice versa. We will see about it in upcoming days.