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.
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:
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.
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.PascalCase is the single correct answer on .NET 11.
Dictionary keys are a separate setting
PropertyNamingPolicy does not transform Dictionary<string, TValue> keys. For that
request, set DictionaryKeyPolicy:
Rule 2 — Strongly-typed JsonTypeInfo<T>
When the user wants type metadata back as JsonTypeInfo<T> (not the non-generic
JsonTypeInfo that needs a cast):
- Call
options.GetTypeInfo<T>()— it returnsJsonTypeInfo<T>directly. - Assign it to a
JsonTypeInfo<T>variable and use it (e.g. pass it toJsonSerializer.Serialize/Deserialize).
Do not call the non-generic GetTypeInfo(Type) overload and cast the result.
Requires a resolver.
GetTypeInfo<T>()throwsNotSupportedException(NoMetadataForType) unless the options have aTypeInfoResolver— setTypeInfoResolver = new DefaultJsonTypeInfoResolver()for reflection-based apps, or use a source-generatedJsonSerializerContextfor trimmed/AOT apps.
Rule 3 — Probe metadata without throwing
When the user wants to check whether metadata for T is available and branch on it —
without an exception being thrown when it is not:
- Call
options.TryGetTypeInfo<T>(out var info). - Handle the
truebranch (metadata resolved, useinfo) and thefalsebranch (not resolved) explicitly.
Do not wrap GetTypeInfo<T>() in try/catch to detect the missing case — that is
exactly the anti-pattern this API removes. TryGetTypeInfo<T> returns false (instead of
throwing) when no resolver can produce metadata for T, which is precisely the case you
want to branch on.
Producing runnable output on net11.0
The task is not done until the program runs on net11.0 and prints its JSON. Prefer a
console project — reflection-based serialization works there out of the box. A
file-based app also works but has one important caveat (below).
When the installed SDK cannot target net11.0 and execution was requested, install an
SDK locally with the official script and invoke it by full path. The install script is
non-administrative and does not persistently alter PATH.
Option A — console project (recommended)
Create a project whose .csproj contains <TargetFramework>net11.0</TargetFramework>,
put the code in Program.cs, then run dotnet run. Confirm the process exits with code 0
and prints the expected JSON.
Option B — file-based app (quickest, one caveat)
Save as app.cs, then run dotnet run app.cs; the first directive pins the framework.
Caveat — file-based apps disable System.Text.Json reflection. In a
dotnet run app.csfile-based app,JsonSerializer.IsReflectionEnabledByDefaultisfalse, so plain reflection serialization throwsNotSupportedException(NoMetadataForType). Set an explicitTypeInfoResolver = new DefaultJsonTypeInfoResolver()on the options (as below), or use a source-generatedJsonSerializerContext. A regular project does not need this.
Worked example — serialize with typed metadata + PascalCase
The record below uses lowercase member names on purpose, so the PascalCase policy visibly rewrites them in the output:
Validation checklist
Before reporting success, confirm every applicable box:
- The project or file-based app targets
net11.0(visible in the.csprojor the#:property TargetFramework=net11.0directive). - PascalCase requests use
JsonNamingPolicy.PascalCase— no customJsonNamingPolicysubclass and no per-member[JsonPropertyName]attributes just to change casing. - Dictionary-key requests set
DictionaryKeyPolicy, not onlyPropertyNamingPolicy. - Typed-metadata requests use the generic
GetTypeInfo<T>()— no cast of a non-genericJsonTypeInfo— and the options set aTypeInfoResolver(e.g.DefaultJsonTypeInfoResolver) so the call doesn't throwNoMetadataForType. - Probing requests use
TryGetTypeInfo<T>(out …)— notry/catcharoundGetTypeInfo. - The program was actually run (
dotnet run …), exited 0, and its printed JSON shows the expected property names (e.g."Name","Age"). - If a file-based app (
dotnet run app.cs) is used, everyJsonSerializerOptionssets aTypeInfoResolver— file-based apps disable reflection so plain serialization throwsNoMetadataForTypewithout one.
Common pitfalls
More info
- JsonNamingPolicy class — built-in naming policies including
PascalCase - JsonSerializerOptions.GetTypeInfo — typed and non-typed metadata access
- JsonTypeInfo<T> — strongly-typed serialization metadata
- DefaultJsonTypeInfoResolver — reflection-based resolver required by
GetTypeInfo/TryGetTypeInfo - File-based apps —
dotnet run app.csand the#:propertydirective


