Jil - Fast .NET JSON (De)Serializer

Jil 是 JSON 處理的套件,號稱比 JSON.NET 更快,甚至是當前套件中處理起來第二快的,僅次於 Protobuf。







只要加入 NuGet 參考,Using Jil 命名空間即可開始使用。


要序列化時,可將物件帶入 JSON.Serialize 方法,方法會回傳序列化後的 JSON 字串。

1
2
3
...
var json = JSON.Serialize(larry);
...

解序列化時,可將 JSON 字串帶入 JSON.Deserialize,並利用範型指定所要解回的物件型態即可。

1
2
3
...
larry = JSON.Deserialize<Person>(json);
...


此外,它也支援動態解析的能力,使用上只要將 JSON 字串帶入 JSON.DeserializeDynamic 即會回傳 Dynamic 物件。

1
2
3
...
var person = JSON.DeserializeDynamic(json);
...


完整的操作範例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Jil;
using System;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var larry = new Person
{
Name = "Larry Nung",
NickName = "Larry"
};

var json = JSON.Serialize(larry);

Console.WriteLine(json);


larry = JSON.Deserialize<Person>(json);

Console.WriteLine("{0} ({1})", larry.Name, larry.NickName);


var person = JSON.DeserializeDynamic(json);

Console.WriteLine("{0} ({1})", person.Name, person["NickName"]);
}
}

public class Person
{
public String Name { get; set; }

public String NickName { get; set; }
}
}


接著我們看一下序列化時效能上的比較:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Jil;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Person Larry = new Person
{
Name = "Larry Nung",
NickName = "蹂躪"
};

var count = 1000000;

Console.WriteLine("Newtonsoft: {0} ms", DoTest(count, () =>
{
var json = JsonConvert.SerializeObject(Larry);
}));

Console.WriteLine("Jil: {0} ms", DoTest(count, () =>
{
var json = JSON.Serialize(Larry);
}));
}

static long DoTest(int count, Action action)
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i) action();
return sw.ElapsedMilliseconds;
}
}

public class Person
{
public String Name { get; set; }

public String NickName { get; set; }
}
}



接著看一下解列化時的效能比較:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Jil;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Person Larry = new Person
{
Name = "Larry Nung",
NickName = "蹂躪"
};

var json = JsonConvert.SerializeObject(Larry);
var count = 1000000;

Console.WriteLine("Newtonsoft: {0} ms", DoTest(count, () =>
{
var person = JsonConvert.DeserializeObject<Person>(json);
}));

Console.WriteLine("Jil: {0} ms", DoTest(count, () =>
{
var person = JSON.Deserialize<Person>(json);
}));
}

static long DoTest(int count, Action action)
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i) action();
return sw.ElapsedMilliseconds;
}
}

public class Person
{
public String Name { get; set; }

public String NickName { get; set; }
}
}



可以看到 Jil 在 JSON 的處理上的確有著較佳的效率。