system-text-json-net11
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSystem.Text.Json — .NET 11
System.Text.Json — .NET 11
New APIs added to across .NET 11 releases.
System.Text.Json.NET 11各版本中为新增的API。
System.Text.JsonWhen to Use
适用场景
- Serializing or deserializing JSON in a .NET 11 (or later) project
- Needing strongly-typed access instead of the untyped
JsonTypeInfo<T>overloadJsonTypeInfo - Wanting to safely check whether type metadata is available without catching exceptions ()
TryGetTypeInfo<T> - Requiring PascalCase property naming during JSON serialization
- 在.NET 11(或更高版本)项目中进行JSON序列化或反序列化
- 需要强类型访问,而非非类型化的
JsonTypeInfo<T>重载JsonTypeInfo - 希望在不捕获异常的情况下安全检查类型元数据是否可用()
TryGetTypeInfo<T> - JSON序列化期间需要使用PascalCase属性命名
When Not to Use
不适用场景
- The project targets .NET 10 or earlier — these APIs are not available before .NET 11
- Using a JSON library that is not (e.g., Newtonsoft.Json)
System.Text.Json - The existing untyped /
GetTypeInfo(Type)overloads are sufficientTryGetTypeInfo(Type, ...)
- 项目目标框架为.NET 10或更早版本——这些API在.NET 11之前不可用
- 使用非的JSON库(例如Newtonsoft.Json)
System.Text.Json - 现有的非类型化/
GetTypeInfo(Type)重载已满足需求TryGetTypeInfo(Type, ...)
Target Framework
目标框架
xml
<TargetFramework>net11.0</TargetFramework>xml
<TargetFramework>net11.0</TargetFramework>New APIs
新增API
Typed JsonTypeInfo
Access
JsonTypeInfo类型化JsonTypeInfo
访问
JsonTypeInfoJsonSerializerOptions.GetTypeInfo<T>()
JsonSerializerOptions.GetTypeInfo<T>()JsonSerializerOptions.GetTypeInfo<T>()
JsonSerializerOptions.GetTypeInfo<T>()Returns a strongly-typed for the specified type, using the
options' configured type-info resolver.
JsonTypeInfo<T>csharp
JsonTypeInfo<T> GetTypeInfo<T>()返回指定类型的强类型,使用配置的类型信息解析器。
JsonTypeInfo<T>csharp
JsonTypeInfo<T> GetTypeInfo<T>()JsonSerializerOptions.TryGetTypeInfo<T>(out JsonTypeInfo<T>?)
JsonSerializerOptions.TryGetTypeInfo<T>(out JsonTypeInfo<T>?)JsonSerializerOptions.TryGetTypeInfo<T>(out JsonTypeInfo<T>?)
JsonSerializerOptions.TryGetTypeInfo<T>(out JsonTypeInfo<T>?)Attempts to retrieve typed metadata without throwing if the type is not resolved.
csharp
bool TryGetTypeInfo<T>(out JsonTypeInfo<T>? typeInfo)尝试检索类型化元数据,若类型未解析则不抛出异常。
csharp
bool TryGetTypeInfo<T>(out JsonTypeInfo<T>? typeInfo)JsonNamingPolicy.PascalCase
JsonNamingPolicy.PascalCaseJsonNamingPolicy.PascalCase
JsonNamingPolicy.PascalCaseA new static property that converts property names to PascalCase during
serialization.
csharp
static JsonNamingPolicy PascalCase { get; }一个新的静态属性,用于在序列化期间将属性名称转换为PascalCase格式。
csharp
static JsonNamingPolicy PascalCase { get; }Examples
示例
Get Typed JsonTypeInfo
获取类型化JsonTypeInfo
csharp
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
// Retrieve strongly-typed metadata for MyClass
JsonTypeInfo<MyClass> typeInfo = options.GetTypeInfo<MyClass>();
Console.WriteLine($"Type: {typeInfo.Type.Name}");csharp
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
// 检索MyClass的强类型元数据
JsonTypeInfo<MyClass> typeInfo = options.GetTypeInfo<MyClass>();
Console.WriteLine($"Type: {typeInfo.Type.Name}");TryGetTypeInfo for Safe Access
使用TryGetTypeInfo实现安全访问
csharp
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
if (options.TryGetTypeInfo<MyClass>(out var info))
{
Console.WriteLine($"Resolved type info for {info!.Type.Name}");
}
else
{
Console.WriteLine("Type info not available");
}csharp
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
if (options.TryGetTypeInfo<MyClass>(out var info))
{
Console.WriteLine($"Resolved type info for {info!.Type.Name}");
}
else
{
Console.WriteLine("Type info not available");
}PascalCase Naming Policy
PascalCase命名策略
csharp
using System.Text.Json;
var opts = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.PascalCase
};
var obj = new { firstName = "John", lastName = "Doe" };
string json = JsonSerializer.Serialize(obj, opts);
Console.WriteLine(json);
// Output: {"FirstName":"John","LastName":"Doe"}csharp
using System.Text.Json;
var opts = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.PascalCase
};
var obj = new { firstName = "John", lastName = "Doe" };
string json = JsonSerializer.Serialize(obj, opts);
Console.WriteLine(json);
// 输出: {"FirstName":"John","LastName":"Doe"}Combined: Serialize with Typed Metadata
组合使用:通过类型化元数据进行序列化
csharp
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.PascalCase
};
JsonTypeInfo<Person> typeInfo = options.GetTypeInfo<Person>();
string json = JsonSerializer.Serialize(new Person("Jane", 30), typeInfo);
Console.WriteLine(json);
// Output: {"Name":"Jane","Age":30}
public record Person(string Name, int Age);csharp
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.PascalCase
};
JsonTypeInfo<Person> typeInfo = options.GetTypeInfo<Person>();
string json = JsonSerializer.Serialize(new Person("Jane", 30), typeInfo);
Console.WriteLine(json);
// 输出: {"Name":"Jane","Age":30}
public record Person(string Name, int Age);