从 Newtonsoft.Json 迁移到 System.Text.Json

2024-04-24 10:28

本文主要是介绍从 Newtonsoft.Json 迁移到 System.Text.Json,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一.写在前面#

System.Text.Json 是 .NET Core 3 及以上版本内置的 Json 序列化组件,刚推出的时候经常看到踩各种坑的吐槽,现在经过几个版本的迭代优化,提升了易用性,修复了各种问题,是时候考虑使用 System.Text.Json 了。本文将从使用层面来进行对比。

System.Text.Json 在默认情况下十分严格,避免进行任何猜测或解释,强调确定性行为。比如:字符串默认转义,默认不允许尾随逗号,默认不允许带引号的数字等,不允许单引号或者不带引号的属性名称和字符串值。 该库是为了实现性能和安全性而特意这样设计的。Newtonsoft.Json 默认情况下十分灵活。

Newtonsoft.Json 使用 13.0.2 版本,基于 .NET 7。

二.序列化#

1.序列化#

定义 Class

public class Cat
{public string? Name { get; set; }public int Age { get; set; }
}

序列化

var cat = new Cat() { Name = "xiaoshi", Age = 18 };Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat));
// output: {"Name":"xiaoshi","Age":18}
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat));
// output: {"Name":"xiaoshi","Age":18}

变化:JsonConvert.SerializeObject()->JsonSerializer.Serialize()

2.忽略属性#

2.1 通用#
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public int Age { get; set; }

输出:

var cat = new Cat() { Name = "xiaoshi", Age = 18 };Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat));
// output: {"Name":"xiaoshi"}
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat));
// output: {"Name":"xiaoshi"}

变化:无

2.2 忽略所有只读属性#

代码:

public class Cat
{public string? Name { get; set; }public int Age { get;  }public Cat(int age){Age = age;}
}var cat = new Cat(18) { Name = "xiaoshi"};
var options = new System.Text.Json.JsonSerializerOptions
{IgnoreReadOnlyProperties = true,
};
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: {"Name":"xiaoshi"}

Newtonsoft.Json 需要自定义 ContractResolver 才能实现:c# - Newtonsoft JSON - Way of ignoring properties without adding [JsonIgnore] - Stack Overflow

2.3 忽略所有 null 属性#

代码:

var cat = new Cat() { Name = null,Age = 18};var op = new Newtonsoft.Json.JsonSerializerSettings()
{NullValueHandling =NullValueHandling.Ignore
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat,op));
// output: {"Name":"xiaoshi"}var options = new System.Text.Json.JsonSerializerOptions
{DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: {"Name":"xiaoshi"}

默认情况下两者都是不忽略的,需要自行设置

2.4 忽略所有默认值属性#

代码:

var cat = new Cat() { Name = "xiaoshi",Age = };var op = new Newtonsoft.Json.JsonSerializerSettings()
{DefaultValueHandling = DefaultValueHandling.Ignore
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat,op));
// output: {"Name":"xiaoshi"}var options = new System.Text.Json.JsonSerializerOptions
{DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: {"Name":"xiaoshi"}

不管是引用类型还是值类型都具有默认值,引用类型为 null,int 类型为 0。

两者都支持此功能。

3.大小写#

默认情况下两者序列化都是 Pascal 命名,及首字母大写,在 JavaScript 以及 Java 等语言中默认是使用驼峰命名,所以在实际业务中是离不开使用驼峰的。

代码:

var cat = new Cat() { Name = "xiaoshi",Age = };var op = new Newtonsoft.Json.JsonSerializerSettings()
{ContractResolver = new CamelCasePropertyNamesContractResolver()
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat,op));
// output: {"name":"xiaoshi","age":0}var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: {"name":"xiaoshi","age":0}

4.字符串转义#

System.Text.Json 默认会对非 ASCII 字符进行转义,会将它们替换为 \uxxxx,其中 xxxx 为字符的 Unicode 代码。这是为了安全而考虑(XSS 攻击等),会执行严格的字符转义。而 Newtonsoft.Json 默认则不会转义。

默认:

var cat = new Cat() { Name = "小时",Age = };var op = new Newtonsoft.Json.JsonSerializerSettings()
{ContractResolver = new CamelCasePropertyNamesContractResolver()
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat,op));
// output: {"name":"小时","age":0}var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: {"name":"\u5C0F\u65F6","age":0}

System.Text.Json 关闭转义:

var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = JsonNamingPolicy.CamelCase,Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: {"name":"小时","age":0}

Newtonsoft.Json 开启转义:

var op = new Newtonsoft.Json.JsonSerializerSettings()
{ContractResolver = new CamelCasePropertyNamesContractResolver(),StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat,op));
// output: {"name":"\u5c0f\u65f6","age":0}

5.自定义转换器#

自定义转换器 Converter,是我们比较常用的功能,以自定义 Converter 来输出特定的日期格式为例。

Newtonsoft.Json:

public class CustomDateTimeConverter : IsoDateTimeConverter
{public CustomDateTimeConverter(){DateTimeFormat = "yyyy-MM-dd";}public CustomDateTimeConverter(string format){DateTimeFormat = format;}
}// test
var op = new Newtonsoft.Json.JsonSerializerSettings()
{ContractResolver = new CamelCasePropertyNamesContractResolver(),Converters = new List<JsonConverter>() { new CustomDateTimeConverter() }
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat,op));
// output: {"name":"xiaoshi","now":"2023-02-13","age":0}

System.Text.Json:

public class CustomDateTimeConverter : JsonConverter<DateTime>
{public override DateTime Read(ref Utf8JsonReader reader,Type typeToConvert,JsonSerializerOptions options) =>DateTime.ParseExact(reader.GetString()!,"yyyy-MM-dd", CultureInfo.InvariantCulture);public override void Write(Utf8JsonWriter writer,DateTime dateTimeValue,JsonSerializerOptions options) =>writer.WriteStringValue(dateTimeValue.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
}// test
var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = JsonNamingPolicy.CamelCase,Converters = { new CustomDateTimeConverter() }
};
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: {"name":"xiaoshi","age":0,"now":"2023-02-13"}

两者的使用方法都是差不多的,只是注册优先级有所不同。

Newtonsoft.Json:属性上的特性>类型上的特性>Converters 集合

System.Text.Json:属性上的特性>Converters 集合>类型上的特性

6.循环引用#

有如下定义:

public class Cat
{public string? Name { get; set; }public int Age { get; set; }public Cat Child { get; set; }public Cat Parent { get; set; }
}var cat1 = new Cat() { Name = "xiaoshi",Age = };
var cat2 = new Cat() { Name = "xiaomao",Age = };cat1.Child = cat2;
cat2.Parent = cat1;

序列化 cat1 默认两者都会抛出异常,如何解决?

Newtonsoft.Json:

var op = new Newtonsoft.Json.JsonSerializerSettings()
{ContractResolver = new CamelCasePropertyNamesContractResolver(),ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat1,op));

设置 ReferenceLoopHandling.Ignore 即可。

System.Text.Json:

var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = JsonNamingPolicy.CamelCase,ReferenceHandler = ReferenceHandler.IgnoreCycles
};
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat1, options));

等效设置

System.Text.JsonNewtonsoft.Json
ReferenceHandler = ReferenceHandler.PreservePreserveReferencesHandling=PreserveReferencesHandling.All
ReferenceHandler = ReferenceHandler.IgnoreCycles

ReferenceLoopHandling = ReferenceLoopHandling.Ignore

8.支持字段(Field)#

在序列化和反序列时支持字段,字段不能定义为 private。

public class Cat
{public string? Name { get; set; }public int _age;public Cat(){_age = 13;}
}var op = new Newtonsoft.Json.JsonSerializerSettings()
{ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat,op));
// output: {"_age":13,"name":"xiaoshi"}var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,IncludeFields = true // 或者 JsonIncludeAttribute
};Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: {"name":"xiaoshi","_age":13}

System.Text.Json 默认不支持直接序列化和反序列化字段,需要设置 IncludeFields = true或者 JsonIncludeAttribute 特性。

8.顺序#

自定义属性在 Json 输出中的顺序:

public class Cat
{public string? Name { get; set; }[System.Text.Json.Serialization.JsonPropertyOrder()][Newtonsoft.Json.JsonProperty(Order = )]public int Age { get; set; }
}

System.Text.Json 使用 JsonPropertyOrder,Newtonsoft.Json 使用 JsonProperty(Order)

9.字节数组#

Newtonsoft.Json 不支持直接序列化为字节数组,System.Text.Json 支持直接序列化为 UTF-8 字节数组。

System.Text.Json:

var bytes = JsonSerializer.SerializeToUtf8Bytes(cat)

序列化为 UTF-8 字节数组比使用基于字符串的方法大约快 5-10%。

10.重命名#

public class Cat
{public string? Name { get; set; }[System.Text.Json.Serialization.JsonPropertyName("catAge")][Newtonsoft.Json.JsonProperty("catAge")]public int Age { get; set; }
}

重命名 Json 属性名称,System.Text.Json 使用 JsonPropertyName,Newtonsoft.Json 使用 JsonProperty

11.缩进#

Newtonsoft.Json:

var op = new Newtonsoft.Json.JsonSerializerSettings()
{ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),// this optionFormatting = Newtonsoft.Json.Formatting.Indented,
};Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(cat,op));
// output: 
// {
//     "name": "xiaoshi",
//     "catAge": 0
// }

System.Text.Json

var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,// this optionWriteIndented = true,
};Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(cat, options));
// output: 
// {
//     "name": "xiaoshi",
//     "catAge": 0
// }

三.反序列化#

1.反序列化#

定义:

public class Cat
{public string? Name { get; set; }public int Age { get; set; }
}var json = """{"name":"xiaoshi","age":16} """;
Cat cat;

Newtonsoft.Json:

var op = new Newtonsoft.Json.JsonSerializerSettings()
{ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
};cat=Newtonsoft.Json.JsonConvert.DeserializeObject<Cat>(json, op);Console.WriteLine($"CatName {cat.Name}, Age {cat.Age}");
// output: CatName xiaoshi, Age 16

System.Text.Json:

var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
};cat=System.Text.Json.JsonSerializer.Deserialize<Cat>(json,options);Console.WriteLine($"CatName {cat.Name}, Age {cat.Age}");
// output: CatName xiaoshi, Age 16

变化 JsonConvert.DeserializeObject->JsonSerializer.Deserialize

2.允许注释#

在反序列化过程中,Newtonsoft.Json 在默认情况下会忽略 JSON 中的注释。 System.Text.Json 默认是对注释引发异常,因为 System.Text.Json 规范不包含它们。

var json = """
{"name": "xiaoshi", // cat name"age": 16
}
""";
Cat cat;var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,// 不设置会引发异常ReadCommentHandling = System.Text.Json.JsonCommentHandling.Skip,
};cat=System.Text.Json.JsonSerializer.Deserialize<Cat>(json,options);Console.WriteLine($"CatName {cat.Name}, Age {cat.Age}");
// output: CatName xiaoshi, Age 16

设置 ReadCommentHandling=JsonCommentHandling.Skip即可忽略注释。

3.尾随逗号#

尾随逗号即 Json 末尾为逗号:

无尾随逗号:

{"name": "xiaoshi","age": 16
}

有尾随逗号:

{"name": "xiaoshi","age": 16,
}

System.Text.Json 默认对尾随逗号引发异常,可以通过 AllowTrailingCommas = true 来设置

var json = """
{"name": "xiaoshi","age": 16,
}
""";Cat cat;var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,AllowTrailingCommas = true,
};cat=System.Text.Json.JsonSerializer.Deserialize<Cat>(json,options);Console.WriteLine($"CatName {cat.Name}, Age {cat.Age}");
// output: CatName xiaoshi, Age 16

尾随逗号一般和允许注释一起使用,因为行注释必须写在引号以后。

4.带引号数字#

在标准 Json 里,数字类型是不带引号的,如:{"Name":"xiaoshi","Age":18},但有时我们可能会遇到不标准的异类,Newtonsoft.Json 默认是支持直接反序列化为数字类型的,而 System.Text.Json 基于严格的标准出发,默认不支持,但是可配置。

var options = new System.Text.Json.JsonSerializerOptions
{PropertyNamingPolicy = JsonNamingPolicy.CamelCase,NumberHandling = JsonNumberHandling.AllowReadingFromString
};// C# 11 原始字符串
var json="""{"name":"xiaoshi","age":"13"}""";Console.WriteLine(System.Text.Json.JsonSerializer.Deserialize<Cat>(json, options).Age);
// output: 13

设置 NumberHandling = JsonNumberHandling.AllowReadingFromString 即可。

5.Json DOM#

不直接反序列化为对象,比如 Newtonsoft.Json 里的 JObject.Parse。在 System.Text.Json 里可以使用 JsonNode、JsonDocument、JsonObject 等。

6.JsonConstructor#

通过 JsonConstructor 特性指定使用的反序列化构造方法,两者是一致的。

四.无法满足的场景#

官方给出了对比 Newtonsoft.Json 没有直接支持的功能,但是可以通过自定义 Converter 来支持。如果需要依赖这部分功能,那么在迁移过程中需要进行代码更改。

Newtonsoft.JsonSystem.Text.Json
支持范围广泛的类型⚠️ ⚠
将推断类型反序列化为 object 属性⚠️ ⚠
将 JSON null 文本反序列化为不可为 null 的值类型⚠️ ⚠
DateTimeZoneHandlingDateFormatString 设置⚠️ ⚠
JsonConvert.PopulateObject 方法⚠️ ⚠
ObjectCreationHandling 全局设置⚠️ ⚠
在不带 setter 的情况下添加到集合⚠️ ⚠
对属性名称采用蛇形命名法⚠️ ⚠

以下功能 System.Text.Json 不支持:

Newtonsoft.JsonSystem.Text.Json
支持 System.Runtime.Serialization 特性❌❌
MissingMemberHandling 全局设置❌❌
允许不带引号的属性名称❌❌
字符串值前后允许单引号❌❌
对字符串属性允许非字符串 JSON 值❌❌
TypeNameHandling.All 全局设置❌❌
支持 JsonPath 查询❌❌
可配置的限制❌❌

五.结束#

在 Ms Learn(Docs) 和 Google 之间频繁切换写完了这篇文章,希望对大家在从 Newtonsoft.Json 迁移到 System.Text.Json 有所帮助。就我个人而言我是打算使用 System.Text.Json 了。

这篇关于从 Newtonsoft.Json 迁移到 System.Text.Json的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/931496

相关文章

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

Oracle迁移PostgreSQL隐式类型转换配置指南

《Oracle迁移PostgreSQL隐式类型转换配置指南》Oracle迁移PostgreSQL时因类型差异易引发错误,需通过显式/隐式类型转换、转换关系管理及冲突处理解决,并配合验证测试确保数据一致... 目录一、问题背景二、解决方案1. 显式类型转换2. 隐式转换配置三、维护操作1. 转换关系管理2.

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

详解MySQL中JSON数据类型用法及与传统JSON字符串对比

《详解MySQL中JSON数据类型用法及与传统JSON字符串对比》MySQL从5.7版本开始引入了JSON数据类型,专门用于存储JSON格式的数据,本文将为大家简单介绍一下MySQL中JSON数据类型... 目录前言基本用法jsON数据类型 vs 传统JSON字符串1. 存储方式2. 查询方式对比3. 索引

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对