? 前言
说起 C# 对 JSON 的操作(序列化与反序列化),大家都会想到 javascriptSerializer、DataContractJsonSerializer 与 Newtonsoft.Json 等。以上三种都可以操作 JSON 的,它们又有什么区别呢?
? 主要有以下区别:
1. 应用场景
1) JavaScriptSerializer,位于 System.Web.Extensions.dll 程序集。适用于 Web 项目中,因为每当新建一个 Web 项目后,VS 都会自动帮我引用该程序集。
2) DataContractJsonSerializer,位于 System.Runtime.Serialization.dll 程序集。适用于 WCF 项目中,因为每当新建一个 WCF 项目后,VS 都会自动帮我引用该程序集。另外,由名称可见它是一种“数据契约 JSON 序列化”。
3) Newtonsoft.Json,一个第三方的开源类库,主要处理数据的 JSON 序列化操作,在性能或功能方面与前两者相比都有优势,官网:https://www.newtonsoft.com/json,适用于对 JSON 数据操作平凡的各种项目中。
2. 性能比较
1) 下图是 Newtonsoft.Json 官网给出的执行耗时比较
2) Newtonsoft.Json 官网当然说自己的牛逼,所以,本人在本机进行了代码验证,代码如下:
string jssStr, dcjsStr, jcStr;
Person[] jssArr, dcjsArr, jcArr;
int count = 100000;
Person[] persons = new Person[count];
for (int i = 0; i < count; i++)
{
persons[i] = new Person()
{
Name = string.Format("李三毛_{0}", i),
Age = 29,
Sex = true,
Address = "湖北省利川市",
Spouse = new Person()
{
Name = string.Format("大美妞_{0}", i),
Age = 25,
Sex = false,
Address = "湖北省利川市",
Spouse = null
}
};
}
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
//3.1 JavaScriptSerializer
JavaScriptSerializer jss = new JavaScriptSerializer();
jss.MaxJsonLength = 92097152; //默认为:2097152
stopwatch.Restart();
jssStr = jss.Serialize(persons);
stopwatch.Stop();
Console.WriteLine("JavaScriptSerializer Serialize Time【{0:F1}s,{1}ms】,String Length:{2}",
TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds, stopwatch.ElapsedMilliseconds, jssStr.Length);
stopwatch.Restart();
jssArr = jss.Deserialize<Person[]>(jssStr);
stopwatch.Stop();
Console.WriteLine("JavaScriptSerializer Deserialize Time【{0:F1}s,{1}ms】,Array Length:{2}",
TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds, stopwatch.ElapsedMilliseconds, jssArr.Length);
//3.2 DataContractJsonSerializer
DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(Person[]));
stopwatch.Restart();
using (MemoryStream stream = new MemoryStream())
{
dcjs.WriteObject(stream, persons);
dcjsStr = Encoding.UTF8.GetString(stream.ToArray());
}
stopwatch.Stop();
Console.WriteLine("DataContractJsonSerializer Serialize Time【{0:F1}s,{1}ms】,String Length:{2}",
TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds, stopwatch.ElapsedMilliseconds, dcjsStr.Length);
stopwatch.Restart();
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(dcjsStr)))
{
dcjsArr = dcjs.ReadObject(stream) as Person[];
}
stopwatch.Stop();
Console.WriteLine("DataContractJsonSerializer Deserialize Time【{0:F1}s,{1}ms】,Array Length:{2}",
TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds, stopwatch.ElapsedMilliseconds, dcjsArr.Length);
//3.3 Newtonsoft.Json
stopwatch.Restart();
jcStr = JsonConvert.SerializeObject(persons);
stopwatch.Stop();
Console.WriteLine("Newtonsoft.Json Serialize Time【{0:F1}s,{1}ms】,String Length:{2}",
TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds, stopwatch.ElapsedMilliseconds, jcStr.Length);
stopwatch.Restart();
jcArr = JsonConvert.DeserializeObject<Person[]>(jcStr);
stopwatch.Stop();
Console.WriteLine("Newtonsoft.Json Deserialize Time【{0:F1}s,{1}ms】,Array Length:{2}",
TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds, stopwatch.ElapsedMilliseconds, jcArr.Length);
? 运行结果(三者分别运行三次后的最短耗时)
1. Serialize(序列化)
1) DataContractJsonSerializer Serialize Time【0.7s,696ms】,String Length:14377781
2) Newtonsoft.Json Serialize Time【1.7s,1741ms】,String Length:14377781
3) JavaScriptSerializer Serialize Time【6.2s,6189ms】,String Length:14377781
2. Deserialize(反序列化)
1) DataContractJsonSerializer Deserialize Time【2.0s,2037ms】,Array Length:100000
2) Newtonsoft.Json Deserialize Time【2.4s,2407ms】,Array Length:100000
3) JavaScriptSerializer Deserialize Time【3.7s,3733ms】,Array Length:100000
? 可见,耗时最短的并不是 Newtonsoft.Json,而是 .NET 自带的 DataContractJsonSerializer,最差的是 JavaScriptSerializer,序列化用了6秒,而 DataContractJsonSerializer 则之需要1秒时间不到。所以这一局,DataContractJsonSerializer 胜出,而 Newtonsoft.Json 则稍微靠后,由于它的功能方面比较强大,所以还是继续研究它。