跳到主要内容
版本:2.0.0

Json序列化

定义

命名空间:TouchSocket.Core
程序集:TouchSocket.Core.dll

一、说明

在TouchSocket中,内置了Json序列化与反序列化。

string jsonstr = SerializeConvert.ToJsonString(new object());//序列化
object obj = SerializeConvert.FromJsonString<object>(jsonstr);//反序列化

二、动态调整的Json策略 弃用

默认情况下: 在net45和netstandard2.0平台时,序列化方式是由JsonFast(群友老江)提供的单文件json序列化。该json工具能够序列化大多数数据结构,且性能和Newtonsoft.Json不相上下(见下测试)。 在netcoreapp3.1及以上平台时,序列化方式使用System.Text.Json。

但是 当应用中加载了Newtonsoft.Json的程序集后,所有的平台的序列化,均会使用Newtonsoft.Json。可通过**SerializeConvert.NewtonsoftJsonIsSupported**静态属性获取当前是否支持Newtonsoft.Json。

也可以手动加载Newtonsoft.Json(一般在Unity3d中需要手动加载)。

bool IsSupported=SerializeConvert.LoadNewtonsoftJson(typeof(JsonConvert));//返回值指示是否成功加载

当加载了Newtonsoft.Json的程序集,但是不想使用该工具序列化时,可将**SerializeConvert.NewtonsoftJsonFirst**静态属性设为false。

三、JsonFast性能

【简单数据对象】

public class SimpleObject 
{
public int Age { get; set; }
public string Name { get; set; }
}
[Benchmark]
public void JsonFast_SimpleObject()
{
var v = new SimpleObject { Age = 40, Name = "John" };
for (int i = 0; i < Count; i++)
{
var str = JsonFastConverter.JsonTo(v);
var val = JsonFastConverter.JsonFrom<SimpleObject>(str);
}
}

下图为1w次的序列化与反序列化。JsonFast的效率甚至还稍高一些。 image.png

【复杂对象】

public class ComplexObject
{
public Dictionary<int, int> Dic1 { get; set; }
public Dictionary<int, string> Dic2 { get; set; }
public Dictionary<string, string> Dic3 { get; set; }
public Dictionary<int, Arg> Dic4 { get; set; }
public List<int> List1 { get; set; }
public List<string> List2 { get; set; }
public List<byte[]> List3 { get; set; }
public int P1 { get; set; }
public string P2 { get; set; }
public long P3 { get; set; }
public byte P4 { get; set; }
public DateTime P5 { get; set; }
public double P6 { get; set; }
public byte[] P7 { get; set; }
}

public class Arg
{
public Arg()
{
}

public Arg(int myProperty)
{
MyProperty = myProperty;
}

public int MyProperty { get; set; }
}

初始化

private ComplexObject GetComplexObject()
{
ComplexObject complexObject = new ComplexObject();
complexObject.P1 = 10;
complexObject.P2 = "天下无敌";
complexObject.P3 = 100;
complexObject.P4 = 0;
complexObject.P5 = DateTime.Now;
complexObject.P6 = 10;
complexObject.P7 = new byte[1024 * 64];

Random random = new Random();
random.NextBytes(complexObject.P7);

complexObject.List1 = new List<int>();
complexObject.List1.Add(1);
complexObject.List1.Add(2);
complexObject.List1.Add(3);

complexObject.List2 = new List<string>();
complexObject.List2.Add("1");
complexObject.List2.Add("2");
complexObject.List2.Add("3");

complexObject.List3 = new List<byte[]>();
complexObject.List3.Add(new byte[1024]);
complexObject.List3.Add(new byte[1024]);
complexObject.List3.Add(new byte[1024]);

complexObject.Dic1 = new Dictionary<int, int>();
complexObject.Dic1.Add(1, 1);
complexObject.Dic1.Add(2, 2);
complexObject.Dic1.Add(3, 3);

complexObject.Dic2 = new Dictionary<int, string>();
complexObject.Dic2.Add(1, "1");
complexObject.Dic2.Add(2, "2");
complexObject.Dic2.Add(3, "3");

complexObject.Dic3 = new Dictionary<string, string>();
complexObject.Dic3.Add("1", "1");
complexObject.Dic3.Add("2", "2");
complexObject.Dic3.Add("3", "3");

complexObject.Dic4 = new Dictionary<int, Arg>();
complexObject.Dic4.Add(1, new Arg(1));
complexObject.Dic4.Add(2, new Arg(2));
complexObject.Dic4.Add(3, new Arg(3));
return complexObject;
}
[Benchmark]
public void JsonFast_ComplexObject()
{
var v = GetComplexObject();
for (int i = 0; i < Count; i++)
{
var str = JsonFastConverter.JsonTo(v);
var val = JsonFastConverter.JsonFrom<ComplexObject>(str);
}
}

下图为100次序列化与反序列化,JsonFast性能稍弱,但是基本满足要求。 image.png