使用 System.Text.Json(.netcore-3.0) 而不是 Newtonsoft.Json 的扩展功能是什么?

what are the extended features using System.Text.Json(.netcore-3.0) instead of Newtonsoft.Json?

我使用 Newtonsoft.Json 将 JSON 对象转换为字符串,反之亦然。最近我在 https://docs.microsoft.com/en-gb/dotnet/api/system.text.json?view=netcore-3.0.

中读到 System.Text.Json

我看了这篇文章,比Newtonsoft.Json更快。

使用 Newtonsoft.Json;

using Newtonsoft.Json;
Product product = new Product();    
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small", "Medium", "Large" };    
string output = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "ExpiryDate": "2008-12-28T00:00:00",
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

使用 System.Text;

using System.Text.Json;
using System.Text.Json.Serialization;

Product product = new Product();    
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small", "Medium", "Large" }; 
string jsonString = JsonSerializer.Serialize(product);

var objproduct = JsonSerializer.Deserialize<Product>(jsonString);

是的,你可以。没有人阻止你,除非你遇到一些问题。 但是在一个项目中会有一些指导方针,所以最好按照指导方针去做,如果你正在实施新的东西,最好与团队讨论然后实施。

Html 在 System.Text.Json 中默认转义是差异的一个例子。

空值和循环引用处理是其他一些差异。

System.Text.Json 从 .NET Core 3.0 开始不序列化字段。

System.Json.Text 不是 Json.NET 的一对一替代品。