编辑
2025-10-14
C#
00

目录

JSON 架构导出器
示例代码
输出示例
可空引用类型支持
示例代码
输出示例
自定义序列化缩进
示例代码
输出示例
JsonSerializerOptions.Web
示例代码
输出示例
JsonObject 属性排序
示例代码
输出示例
JsonElement 深度比较方法
示例代码
输出示例
自定义枚举成员名称
示例代码
输出示例
总结

在 .NET 9 中,System.Text.Json 库得到了显著增强,为开发者提供了更强大和灵活的 JSON 处理能力。这些改进主要集中在 JSON 架构支持、智能应用功能以及序列化和反序列化过程的自定义选项上。本文将详细介绍这些新特性,并提供示例代码,帮助开发者更好地理解和应用这些功能。

JSON 架构导出器

在 .NET 9 中,新增了 JsonSchemaExporter 类,使开发者能够从 .NET 类型中提取 JSON 架构文档。这一特性有助于验证和文档化 JSON 数据结构,确保应用程序之间的一致性。

示例代码

C#
using System.Text.Json; using System.Text.Json.Schema; namespace AppTextJson { public class Employee { public int Id { get; set; } public string Name { get; set; } public string Position { get; set; } } internal class Program { static void Main(string[] args) { var options = new JsonSerializerOptions { WriteIndented = true }; string jsonSchema = JsonSerializer.Serialize(new Employee(), options); Console.WriteLine(jsonSchema); Console.ReadKey(); } } }

输出示例

image.png

可空引用类型支持

为了与 C# 的可空引用类型注释保持一致,System.Text.Json 现在提供了 RespectNullableAnnotations 选项。当启用时,序列化和反序列化过程中会强制执行不可空引用类型,若不可空属性被赋值为 null,则会抛出异常。

示例代码

C#
using System.Text.Json; using System.Text.Json.Schema; using System.Text.Json.Serialization; namespace AppTextJson { public class Product { public string Name { get; set; } = null!; public decimal Price { get; set; } } internal class Program { static void Main(string[] args) { var options = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; var product = new Product { Name = null, Price = 9.99m }; string json = JsonSerializer.Serialize(product, options); Console.WriteLine(json); Console.ReadKey(); } } }

输出示例

image.png

自定义序列化缩进

System.Text.Json 引入了自定义缩进选项,允许开发者指定用于缩进的字符和大小,以满足特定的格式要求。这有助于使 JSON 输出更易读。

示例代码

C#
var options = new JsonSerializerOptions { WriteIndented = true }; var json = JsonSerializer.Serialize(new { Name = "Alice", Age = 30 }, options); Console.WriteLine(json);

输出示例

image.png

JsonSerializerOptions.Web

JsonSerializerOptions.Web 提供了一组预定义的选项,专为 Web 应用程序量身定制。这包括属性名称的 camelCase 格式和灵活的数字处理,使 JSON 序列化符合常见的 Web API 实践。

示例代码

C#
internal class Program { static void Main(string[] args) { var options = new JsonSerializerOptions(JsonSerializerDefaults.Web); var json = JsonSerializer.Serialize(new { FirstName = "John", LastName = "Doe" }, options); Console.WriteLine(json); Console.ReadKey(); } }

输出示例

image.png

JsonObject 属性排序

JsonObject 类现在允许开发者控制 JSON 对象中属性的顺序。这在某些序列化场景中尤其有用,或者在与对属性顺序敏感的系统交互时。

示例代码

C#
internal class Program { static void Main(string[] args) { var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var json = JsonSerializer.Serialize(new { LastName = "Doe", FirstName = "John" }, options); Console.WriteLine(json); Console.ReadKey(); } }

输出示例

image.png

JsonElement 深度比较方法

在 .NET 9 中,System.Text.Json 库引入了 JsonElement.DeepEquals 方法,使得两个 JsonElement 实例之间的深度比较变得更加简单。该方法可以判断两个 JSON 元素在结构和语义上是否相同。

示例代码

C#
internal class Program { static void Main(string[] args) { var json1 = JsonDocument.Parse("{\"name\":\"Alice\"}").RootElement; var json2 = JsonDocument.Parse("{\"name\":\"Alice\"}").RootElement; Console.WriteLine(JsonElement.DeepEquals(json1, json2)); Console.ReadKey(); } }

输出示例

image.png

自定义枚举成员名称

在 .NET 9 中,System.Text.Json 库引入了 JsonStringEnumMemberName 属性,允许开发者自定义单个枚举成员的 JSON 表示。这一增强提供了更大的灵活性,尤其是在需要特定命名约定或格式的场景中。

示例代码

C#
using System.Text.Json; using System.Text.Json.Schema; using System.Text.Json.Serialization; namespace AppTextJson { [JsonConverter(typeof(JsonStringEnumConverter))] public enum Status { [JsonStringEnumMemberName("Ready For Dev")] ReadyForDev, [JsonStringEnumMemberName("In Progress")] InProgress, [JsonStringEnumMemberName("Completed")] Completed } internal class Program { static void Main(string[] args) { var status = Status.ReadyForDev; string json = JsonSerializer.Serialize(status); Console.WriteLine(json); Console.ReadKey(); } } }

输出示例

image.png

总结

.NET 9 为 System.Text.Json 库带来了多项重要改进,使其在 JSON 序列化和反序列化方面更加强大、灵活和高效。通过本文的示例代码,开发者可以更好地理解和应用这些新特性,从而提升开发效率和代码质量。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!