In today programming technology we are dealing with JSON objects regularly. According to wikipedia, JavaScript Object Notation or JSON is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types. JSON can contain collection of name/value pairs or Ordered list of values. for example these may be the type of json styles:
var person ={"name":"yogesh gautam" ,"country":"nepal", "gender":"male"} (In Object Format)
var persons= [{"person1":{"name": "yogesh gautam" , "country":"nepal" ,"gender":"male"}} , {"person2":: {"name":"dipesh", "country":"nepal" ,"gender":"male" }}] (In Array Format)
var person= "{\"name\":\"yogesh gautam\", \"country\":\"nepal\", \"gender\":\"male\"}" (In String Format)
Nowadays json is used widely because of its features like easy to read and write, language independent , lightweight etc. It is generally used for writing javascript based application in client side. Similarly in server side application we will have models for transmitting and storing data. Then what is serialization and deserialization? Explaining in simple programming words, serialization is the process of converting models into the json format. Whereas deserialization is the reverse process. i.e. It is the process of converting json data into models. For example: In simple web application we may have client side architecture build with vuejs or angular and in server side we may have c# or java or any other architecture. Now sometimes in transferring data from client to server there might be some complication in sending data In such case we may send data from client to server in simple json string format. and then that json string can be converted into the object, this technique is called deserialization. Basically we can implement JSON Serialization and DeSerialization in following ways.
1) Using JavaScriptSerializer class
2) Using DataContractJsonSerializer class
3) Using JSON.NET library
Using DataContract JSON Serializer
DataContractSerializer class helps in serialization and deserialization of json. It is present in System.Runtime.Serialization.Json namespace which is present in assembly System.Runtime.Serialization.dll. So with the help of this class we can perform serialization and deserialization.For example, first of all let us create a simple model class Person as shown below:
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set;}
[DataMember]
public string Country { get; set; }
[DataMember]
public string Gender { get; set; }
}
Now let us serialize this object into JSON string. here is the example for serialization:
Person person = new Person()
{
Name="yogesh gautam",
Country="Nepal",
Gender="Male"
};
DataContractJsonSerializer js=new DataContractJsonSerializer(typeof(Person));
MemoryStream memoryStream=new MemoryStream();
js.WriteObject(memoryStream, person);
memoryStream.Position=0;
StreamReader sr=new StreamReader(memoryStream);
string json = sr.ReadToEnd(); //Output is in Json String Format shown previously
sr.Close();
memoryStream.Close();
Similary for deserialization i.e. for converting Json data into c# object the following code can be used:
var person="{\"name\":\"yogesh gautam\", \"country\":\"nepal\", \"gender\":\"male\"}" ;
using (var ms= new MemoryStream (Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer deserializer= new DataContractJsonSerializer(typeof(Person));
Person person= (Person) deserializer.ReadObject(ms); //here we got json data in object
}
Using JavaScriptSerializer class
Another way of performing serialization and deserialization is by using JavaScriptSerializer class. It is present in namespace System.Web.Script.Serialization which is available in assembly System.Web.Extensions.dll. Let us now try to implement serialization and deserialization. first of all let us create a model object as shown below:
public class Person
{
public string Name { get; set; }
public string Country { get; set; }
public string Gender {get; set; }
}
Now , serialization can be done as shown below:
Person person = new Person()
{
Name = "yogesh gautam",
Country = "Nepal",
Gender = "Male"
}
JavaScriptSerializer js = new JavaScriptSerializer();
string jsonData= js.Serialize(); //output is json string as shown above.
Similarly for deserialization we can use the following code:
JavaScriptSerializer js = new JavaScriptSerializer();
Person person = js.Deserialize<Person> (jsonData); //json data converted to object here
Using JSON.NET Library
Another way of serialization and deserialization in C# is by using Json.NET third party library which helps to convert Json data and .NET objects using the JsonSerializer. It is open source software and free for commercial purposes. To begin with, in visual studio open Nuget Package Manager and search for "JSON.NET" and then install it. After that we can perform serialization as shown below:
Person person = new Person()
{
Name = "yogesh gautam",
Country = "Nepal",
Gender = "Male"
}
string jsonData=JsonConvert.SerializeObject(person); //object is converted to json string.
Similarly for deserialization we can perform as shown below:
Person person= JsonConvert.DeserializeObject <Person>(jsonData);
Although we have discussed 3 different ways of implementing serialization and deserialization. JSON.NET is the best because it facilitates more functionality of JSON validation, JSON schema, LINQ to JSON etc.
References
1) JSON-serialization-and-deserialization-in-c-sharp