Working With Jil Serializer And Deserializer Library In C#

JIL is one of the fastest Serializer and Deserializer libraries. It is also open source. Jil is an open source library, written by Kevin Montrose and the stack exchange team. It is built on Sigil --  with a number of crazy optimization tricks.To work with JIL Library, you can download it from NuGet Package Manager. Here, I will explain how to Serialize and Deserialize an object, using JIL Library. If we compare JIL with other Serializers and Deserializers available, we can get that JIL is very fast among them.This is the  comparison table of JIL with other Serializers and Deserializers.

comparison table

Now, I will show you the simple speed tester graph for JIL.

graph

Now I will create a simple console application which shows how to work with JIL.

Create a simple console application and add JIL library from Nuget Package as follows.

Nuget Package

Now write the following code for Serializing and Deserializing the data as follows.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Diagnostics;  
  7. using Newtonsoft.Json;  
  8. using System.IO;  
  9. using Jil;  
  10.   
  11.   
  12. namespace Serialization  
  13. {  
  14.   
  15.     public class Employee  
  16.     {  
  17.         public int id { get; set; }  
  18.         public string Name { get; set; }  
  19.         public string Email { get; set; }  
  20.         public string Dept { get; set; }  
  21.           
  22.     }  
  23.     class Program  
  24.     {  
  25.           
  26.         
  27.        
  28.         static void Main(string[] args)  
  29.         {  
  30.             List<Employee> liemp = new List<Employee>();  
  31.             Employee emp = new Employee();  
  32.            
  33.   
  34.          liemp.Add(new Employee { id = 2, Name = "Debendra", Email = "[email protected]", Dept = "IT" });  
  35.          liemp.Add(new Employee { id = 3, Name = "Manoj", Email = "[email protected]", Dept = "Sales" });  
  36.          liemp.Add(new Employee { id = 6, Name = "Kumar", Email = "[email protected]", Dept = "IT" });  
  37.          
  38.           
  39.          string jsonData1 = SerializeEmployee(liemp);  
  40.   
  41.          Console.WriteLine(jsonData1);  
  42.          Console.WriteLine("............JIL Deserialization........");  
  43.          
  44.          var employeeDeserialized = DeserializeEmployee(jsonData1);  
  45.          foreach (var data in employeeDeserialized)  
  46.          {  
  47.              Console.WriteLine("Id=" + data.id);  
  48.              Console.WriteLine("Name=" + data.Name);  
  49.              Console.WriteLine("Id=" + data.Email);  
  50.              Console.WriteLine("Id=" + data.Dept);  
  51.          }  
  52.         
  53.   
  54.          Console.ReadLine();  
  55.   
  56.         }  
  57.   
  58.         private static string SerializeEmployee(List<Employee> liemployee)  
  59.         {  
  60.             using (var output = new StringWriter())  
  61.             {  
  62.                 JSON.Serialize(  
  63.                    liemployee,  
  64.                     output  
  65.                 );  
  66.                 return output.ToString();  
  67.             }  
  68.         }  
  69.         private static List<Employee> DeserializeEmployee(string liemployee)  
  70.         {  
  71.                 List<Employee> li = new List<Employee>();  
  72.                 li = JSON.Deserialize<List<Employee>>(liemployee);  
  73.                 return li;  
  74.         }  
  75.     }  
  76. }  
Now if you run the application you will get the following output.

output

Thus in this way we can use JIL  library to serialize and deserialize the data.

Up Next
    Ebook Download
    View all
    Learn
    View all