[04] .NET Core 3.0 新朋友 System.Text.JSON
System.Text.Json 特色
效能比較
撰寫方式的差異
撰寫方式的差異
序列器行為差異
強調效能
主要是啟用了 .NET Core 的祕密武器 - Span
低記憶體耗用量
主要是啟用了 .NET Core 的祕密武器 - Span
低記憶體耗用量
效能比較
System.Text.Json 吞吐量(PRS)明顯都高於JSON.NET (Newtonsoft.Json)


撰寫方式的差異
JSON.NET (JObject)
dynamic md = JsonConvert.DeserializeObject(strJSON);
if (md != null)
query.VipNo = md["VipNo"] == null ? "" : md["VipNo"].ToString();
System.Text.Json (JsonElement)
dynamic md = JsonSerializer.Deserialize(strJSON,);
if (md.ValueKind != null)
{
JsonElement TMP = new JsonElement();
if (Source.TryGetProperty(Name, out TMP))
query.VipNo = TMP.GetString();
}
dynamic md = JsonConvert.DeserializeObject
if (md != null)
query.VipNo = md["VipNo"] == null ? "" : md["VipNo"].ToString();
System.Text.Json (JsonElement)
dynamic md = JsonSerializer.Deserialize
if (md.ValueKind != null)
{
JsonElement TMP = new JsonElement();
if (Source.TryGetProperty(Name, out TMP))
query.VipNo = TMP.GetString();
}
撰寫方式的差異
JSON.NET (JObject)
dynamic md = JsonConvert.DeserializeObject(strJSON);
if (md != null)
query.VipNo = md["VipNo"] == null ? "" : md["VipNo"].ToString();
System.Text.Json (JsonElement)
//自定義轉換器
var options = new JsonSerializerOptions
{
AllowTrailingCommas = true
};
dynamic md = JsonSerializer.Deserialize(strJSON, options);
if (md.ValueKind != null)
{
JsonElement TMP = new JsonElement();
if (Source.TryGetProperty(Name, out TMP))
query.VipNo = TMP.GetString();
}
dynamic md = JsonConvert.DeserializeObject
if (md != null)
query.VipNo = md["VipNo"] == null ? "" : md["VipNo"].ToString();
System.Text.Json (JsonElement)
//自定義轉換器
var options = new JsonSerializerOptions
{
AllowTrailingCommas = true
};
dynamic md = JsonSerializer.Deserialize
if (md.ValueKind != null)
{
JsonElement TMP = new JsonElement();
if (Source.TryGetProperty(Name, out TMP))
query.VipNo = TMP.GetString();
}
序列器行為差異
字元轉換
在序列化期間Newtonsoft.Json,對於讓字元通過而不逃避字元相對寬鬆。 也就是說,它不會將它們替換為\uxxxx``xxxx字元的代碼點。
System.Text.Json預設情況下,將轉義所有非 ASCII 字元
尾隨逗號
在反序列化期間Newtonsoft.Json,預設情況下忽略尾隨逗號。
(例如, [{"Color":"Red"},{"Color":"Green"},,]。
System.Text.Json 預設情況下會發生錯誤。
JSON字串
在反序列化期間Newtonsoft.Json,接受由雙引號、單引號或無引號括起來的屬性名稱。 它接受由雙引號或單引號括起來的字串值。
{
"name1": "value",
'name2': "value",
name3: 'value'
}
System.Text.Json僅接受雙引號的屬性名稱和字串值。
在序列化期間Newtonsoft.Json,對於讓字元通過而不逃避字元相對寬鬆。 也就是說,它不會將它們替換為\uxxxx``xxxx字元的代碼點。
System.Text.Json預設情況下,將轉義所有非 ASCII 字元
尾隨逗號
在反序列化期間Newtonsoft.Json,預設情況下忽略尾隨逗號。
(例如, [{"Color":"Red"},{"Color":"Green"},,]。
System.Text.Json 預設情況下會發生錯誤。
JSON字串
在反序列化期間Newtonsoft.Json,接受由雙引號、單引號或無引號括起來的屬性名稱。 它接受由雙引號或單引號括起來的字串值。
{
"name1": "value",
'name2': "value",
name3: 'value'
}
System.Text.Json僅接受雙引號的屬性名稱和字串值。
留言