Friday, October 2, 2015

Convert C# Object into JSON and vice versa using Jil

I have also written couple of posts about converting C# object into JSON and vice versa. Following is a list of post for the same.

Converting a C# Object into JSON string
How to convert C# object into JSON string with JSON.NET

In this blog post we will learn about how we can convert C# object into JSON with Jil- Fast .NET JSON (De)Serializer, Built On Sigil an open source library by stack exchange team.

What is Jil:

Jil is a open source (De) Serializer library written by Kevin Montrose and stack exchange team. It is built on Sigil- with a number of crazy optimization tricks.Sigil is also a custom fast validation helper for .NET CIL Generation. Jil is one of fastest Serializer and deserializer library. It is also open source. You can find more information about at following link.


So let's create example which demonstrate both Serializing and Deserializing object. So first we will create JSON string from C# object via serializing with Jil and then we again deserialize that string to C# object. I'm going to create a console application for this.


After creating a application first thing we have to do is to add Nuget package from JIl via following command.


I'm going to use a Employee class for this example which have four properties Employee Id, First Name, Last Name and Designation.
namespace CsharpJSON
    public class Employee
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Designation { get; set; }
Then I have created a function Get Employee function to initialize Employee class with some data.
private static Employee GetEmployee()
    var employee = new Employee
        EmployeeId = 1,
        FirstName = "Jalpesh",
        LastName = "Vadgama",
        Designation = "Technial Architect"
    return employee;
Now it's time to serialize that employee class into JSON string so I have created  Serialize Employee function.
private static string SerializeEmployee(Employee employee)
    using (var output = new StringWriter())
        return output.ToString();
Here you can see I have use JSON.Serialize function which serialize C# object into string writer. Same way I have created Deserialize Employee function to deserialize JSON string created by previous function.
private static Employee DeserializeEmployee(string employeeString)
    return JSON.Deserialize<Employee>(employeeString);

Here you can see I have used  JSON. Deserialize function to convert JSON string into C# object. Finally I have called this function in my console application Main method and print it with Console.Writeline method.
static void Main(string[] args)
    var employee = GetEmployee();
    string employeeSerializedString = SerializeEmployee(employee);

    Console.WriteLine("Serialize Employee");

    Console.WriteLine("\n\nDeserializing Employee");
    var employeeDeserialized = DeserializeEmployee(employeeSerializedString);



Now when you run application it will generate output like following.


You can find complete source code of this application at following location on GitHub-

Hope you like it. Stay tuned for more!


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews