system-text-json-net11
它能做什么
这项技能说明如何使用 .NET 11 中三项新的 JSON 功能:内置的 PascalCase 命名选项,以及 JSON 类型信息的类型化查找。适合在 .NET 11 上处理 C# 中 JSON 的开发者。
适用场景
- ✓当 .NET 11 应用需要 PascalCase 形式的 JSON 属性名,而无需编写自定义命名策略时。
- ✓当代码调用 GetTypeInfo 并将结果强制转换为类型化的 JsonTypeInfo 时。
- ✓当你需要检查某个 JSON 类型是否可用,而不必把调用包在 try/catch 中的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet11/skills/system-text-json-net11/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“在我的 .NET 11 应用中,把我的模型序列化为 PascalCase 属性名的 JSON,不需要任何自定义命名代码。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会在序列化器选项中设置内置的 PascalCase 命名策略,在需要的地方使用类型化的查找方法,并运行代码展示输出。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
System.Text.Json — .NET 11
Three APIs were added to System.Text.Json in .NET 11. This skill tells you exactly
when to reach for each one, what to write, what not to write, and how to prove the
result runs. Do not describe these APIs to the user — apply them, then run the code and
show the output.
| API | Replaces the pre-.NET-11 workaround of... |
|---|---|
JsonNamingPolicy.PascalCase (static property) |
writing a custom JsonNamingPolicy subclass or hand-annotating every member with [JsonPropertyName] |
JsonSerializerOptions.GetTypeInfo<T>() |
calling non-generic GetTypeInfo(typeof(T)) and casting to JsonTypeInfo<T> |
JsonSerializerOptions.TryGetTypeInfo<T>(out JsonTypeInfo<T>? info) |
wrapping GetTypeInfo in try/catch to probe availability |
Step 0 — Make the requested net11.0 validation possible
These APIs only exist in the .NET 11 base class library. Before writing code:
- Run
dotnet --list-sdksand confirm an SDK that can targetnet11.0is present — an11.xSDK, or a later SDK with thenet11.0targeting pack. - If the user explicitly asks you to run the sample and no suitable SDK is installed,
use the official
dotnet-installscript to install the current .NET 11 SDK into a temporary or project-local directory. Prefer the GA channel build. Use a preview only when GA is not yet available or the user explicitly requested a preview. Do not require administrator access, change the machine-widePATH, or replace an installed SDK. - Run the sample with that local
dotnetexecutable. If download or execution is blocked, still provide the completenet11.0program and report that it was not run. Never substitutenet10.0, a custom naming policy, or a different API and present that as validation of the .NET 11 feature.
Use the channel, not a guessed version. Try the GA channel first:
$installScript = Join-Path $env:TEMP "dotnet-install-$([guid]::NewGuid()).ps1"
try {
Invoke-WebRequest -Uri 'https://dot.net/v1/dotnet-install.ps1' -OutFile $installScript
& $installScript -Channel 11.0 -InstallDir .\.dotnet
& .\.dotnet\dotnet.exe run --project <PATH_TO_NET11_PROJECT>
}
finally {
Remove-Item -LiteralPath $installScript -Force -ErrorAction SilentlyContinue
}
install_script="$(mktemp "${TMPDIR:-/tmp}/dotnet-install.XXXXXX")"
trap 'rm -f "$install_script"' EXIT
curl -fsSL https://dot.net/v1/dotnet-install.sh -o "$install_script"
bash "$install_script" --channel 11.0 --install-dir ./.dotnet
./.dotnet/dotnet run --project <PATH_TO_NET11_PROJECT>
Before .NET 11 GA, retry the install with -Quality preview (PowerShell) or
--quality preview (shell).
Decision table — symptom → do this → never do this
Match the user's request to a row, apply the Do this cell verbatim, and confirm the Verify column before you are done.
| User asks for… | Do this (on net11.0) |
Never do this | Verify |
|---|---|---|---|
| PascalCase JSON property names | options.PropertyNamingPolicy = JsonNamingPolicy.PascalCase; |
define class …: JsonNamingPolicy; add per-member [JsonPropertyName]; string-case the names yourself |
output JSON keys are PascalCase — e.g. "Name", "Age" |
| PascalCase dictionary keys | options.DictionaryKeyPolicy = JsonNamingPolicy.PascalCase; |
set only PropertyNamingPolicy; pre-transform the dictionary; define a custom policy |
dictionary keys such as pendingOrders become "PendingOrders" |
Strongly-typed metadata JsonTypeInfo<T> |
set TypeInfoResolver = new DefaultJsonTypeInfoResolver(), then JsonTypeInfo<T> ti = options.GetTypeInfo<T>(); |
(JsonTypeInfo<T>)options.GetTypeInfo(typeof(T)) |
variable is typed JsonTypeInfo<T>, no cast |
| Probe whether metadata is resolved | if (options.TryGetTypeInfo<T>(out var ti)) { … } else { … } |
try { options.GetTypeInfo<T>(); } catch (…) { … } |
no try/catch; both branches handled |
Rule 1 — PascalCase property names
When the user wants JSON output whose property names are PascalCase (Name, Age)
and asks for the built-in / framework-provided way:
- Create or reuse a
JsonSerializerOptionsand setPropertyNamingPolicy = JsonNamingPolicy.PascalCase. - Serialize with those options.
Do not write a JsonNamingPolicy subclass, do not add [JsonPropertyName("…")]
attributes to force casing, and do not upper-case the first letter of each name by
hand. `JsonNamingPolicy
常见问题
它适用于 .NET 10 吗?
不适用。这些 API 只存在于 .NET 11 基础库中。
它会安装更新的 SDK 吗?
如果你要求它运行示例,而系统中没有合适的 .NET 11 SDK,它可以安装一个。
TryGet 查找替代了什么?
它替代了那种仅为检查某个类型是否可用而把查找包在 try/catch 中的写法。
详情
作者的原始描述
Imperative guidance for the System.Text.Json APIs added in .NET 11: the built-in `JsonNamingPolicy.PascalCase` naming policy, and the strongly-typed generic `JsonSerializerOptions.GetTypeInfo` and `JsonSerializerOptions.TryGetTypeInfo` metadata accessors (generic overloads that return a typed `JsonTypeInfo`). USE ONLY when the user is targeting net11.0 or later and needs PascalCase JSON property or dictionary-key names without writing a custom naming policy, a strongly-typed generic `JsonTypeInfo` instead of the non-generic `JsonTypeInfo`, or a no-throw way to probe whether a type's serializat
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14656
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。