system-text-json-net11

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

System.Text.Json — .NET 11

System.Text.Json — .NET 11

New APIs added to
System.Text.Json
across .NET 11 releases.
.NET 11各版本中为
System.Text.Json
新增的API。

When to Use

适用场景

  • Serializing or deserializing JSON in a .NET 11 (or later) project
  • Needing strongly-typed
    JsonTypeInfo<T>
    access instead of the untyped
    JsonTypeInfo
    overload
  • 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
    System.Text.Json
    (e.g., Newtonsoft.Json)
  • The existing untyped
    GetTypeInfo(Type)
    /
    TryGetTypeInfo(Type, ...)
    overloads are sufficient
  • 项目目标框架为.NET 10或更早版本——这些API在.NET 11之前不可用
  • 使用非
    System.Text.Json
    的JSON库(例如Newtonsoft.Json)
  • 现有的非类型化
    GetTypeInfo(Type)
    /
    TryGetTypeInfo(Type, ...)
    重载已满足需求

Target Framework

目标框架

xml
<TargetFramework>net11.0</TargetFramework>
xml
<TargetFramework>net11.0</TargetFramework>

New APIs

新增API

Typed
JsonTypeInfo
Access

类型化
JsonTypeInfo
访问

JsonSerializerOptions.GetTypeInfo<T>()

JsonSerializerOptions.GetTypeInfo<T>()

Returns a strongly-typed
JsonTypeInfo<T>
for the specified type, using the options' configured type-info resolver.
csharp
JsonTypeInfo<T> GetTypeInfo<T>()
返回指定类型的强类型
JsonTypeInfo<T>
,使用配置的类型信息解析器。
csharp
JsonTypeInfo<T> GetTypeInfo<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.PascalCase

A 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);