微軟官方博客中描述了為什么構造了全新的Json解析器而不是繼續使用行業准則Json.Net
微軟博客地址:https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/
在官方的Github中,也有關於此問題的詳細描述:https://github.com/dotnet/corefx/issues/33115
簡單說明就是.Net Core有一個Span<T>類,它類似於數組,但它不托管於堆上,因此提供了操作內存的高性能,這將對序列化和反序列化提供更高的性能。
官方API完整介紹https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json?view=netcore-3.0
同時UTF-8和UTF-16也是微軟考慮的一個點。
開始詳細了解一下吧:
class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); string person = "[{\"name\":\"fanqi\",\"age\":25,\"phone\":[\"10086\",\"10010\"]},{\"name\":\"zhangrong\",\"age\":24,\"phone\":[\"10000\",\"10010\"]}]"; List<Person> personList = JsonSerializer.Deserialize<List<Person>>(person); string json = JsonSerializer.Serialize(personList, personList.GetType()); JsonDocument document = JsonDocument.Parse(person); Console.ReadKey(); } } class Person { public string name { get; set; } public int? age { get; set; } public List<string> phone { get; set; } }