C# - serialize object to JSON format using JavaScriptSerializer
In this article I will show you how to store runtime state of the object to file on disk but in JSON format.
For start, lets see what serialization really is.
What is SERIALIZATION?
"Serialization is the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be easily stored and transferred." (For more information please visit: http://msdn.microsoft.com/en-us/library/7ay27kt9(v=vs.110).aspx)
How to use JavaScriptSerialization?
To use JavaScriptSerialization, you must add reference to your project for using System.Web.Extensions.dll.
In this DLL there is class named System.Web.Script.Serialization.JavaScriptSerializer that does all the work.
JavaScriptSerialization in action
Custom object for data
First, we will create some custom class just to structure our data. Nothing special, just Company as object, that contains list of Employees
with some of their basic data - name, and type of employment.
/// <summary>
/// Company info
/// </summary>
public class Company
{
public string Title { get; set; }
public List<Employee> Employees { get; set; }
}
/// <summary>
/// Employee info
/// </summary>
public class Employee
{
public string Name { get; set; }
public EmployeeType EmployeeType { get; set; }
}
/// <summary>
/// Types of employees
/// (just to se how JSON deals with enums!)
/// </summary>
public enum EmployeeType
{
CEO,
Developer
}
Load some data
With upper classes we will create some data to work with.
/// <summary>
/// Function that use upper classes to simulate some data
/// </summary>
/// <returns></returns>
public static Company GetData()
{
return new Company()
{
Title = "Company Ltd",
Employees = new List<Employee>()
{
new Employee(){ Name = "Mark CEO", EmployeeType = EmployeeType.CEO },
new Employee(){ Name = "Matija Božičević", EmployeeType = EmployeeType.Developer },
new Employee(){ Name = "Steve Developer", EmployeeType = EmployeeType.Developer}
}
};
}
JavaScriptSerializer().Serialize
Converts an object to a JSON string.
// Load object with some sample data
Company company = GetData();
// Pass "company" object for conversion object to JSON string
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(company);
// Write that JSON to txt file
File.WriteAllText(Environment.CurrentDirectory + @"\JSON.txt", json);
The content of that "JSON.txt" file is following:
{"Title":"Company Ltd","Employees":[{"Name":"Mark CEO","EmployeeType":0},{"Name":"Matija
Božičević","EmployeeType":1},{"Name":"Steve Developer","EmployeeType":1}]}
JavaScriptSerializer().Deserialize
Converts the specified JSON string to an object of type T.
It's basically reverse process of serialization.
string json = File.ReadAllText(Environment.CurrentDirectory + @"\JSON.txt");
Company company = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Company>(json);
You can use debugger for testing the success of deserialization process.
