Skip to content

System.Text.Json source-gen docs: prefer JsonTypeInfo overloads and remove options/context overload examples#54105

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/remove-jsonserializeroptions-examples
Draft

System.Text.Json source-gen docs: prefer JsonTypeInfo overloads and remove options/context overload examples#54105
Copilot wants to merge 2 commits into
mainfrom
copilot/remove-jsonserializeroptions-examples

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 1, 2026

The source-generation article still demonstrated JsonSerializerOptions and JsonSerializerContext overloads as primary usage, which can produce trimming warnings and misleading Native AOT guidance. This update shifts examples and guidance to JsonTypeInfo<T>-first usage and documents the options-based workaround pattern when options flow is required.

  • Default guidance now centers on JsonTypeInfo<T>

    • Updated the “Use source-generation defaults” and serialization/deserialization example sections to remove context/options-overload emphasis.
    • Replaced option-overload examples with patterns that resolve JsonTypeInfo<T> first, then call JsonSerializer type-info overloads.
  • Removed obsolete/non-preferred overload examples from snippets

    • BothModesNoOptions.cs: removed SerializeWithContext / DeserializeWithContext and direct options-overload samples.
    • Added SerializeWithTypeInfoFromOptions / DeserializeWithTypeInfoFromOptions snippets using options.GetTypeInfo(...).
    • SerializeOnlyWithOptions.cs: removed context-overload sample block.
    • RoundtripEnumSourceGeneration.cs: replaced options-overload enum serialization calls with generated JsonTypeInfo<T> calls.
  • Added Native AOT footnote for options-overload behavior

    • Documented why options-only overloads are unsuitable in source-gen Native AOT scenarios (historical reflection fallback for backward compatibility).
    • Added explicit workaround guidance for options-driven flows.
JsonTypeInfo<MyType> typeInfo =
    (JsonTypeInfo<MyType>)options.GetTypeInfo(typeof(MyType));

string json = JsonSerializer.Serialize(value, typeInfo);
MyType? model = JsonSerializer.Deserialize(json, typeInfo);

Copilot AI review requested due to automatic review settings June 1, 2026 14:03
Copilot AI review requested due to automatic review settings June 1, 2026 14:03
Copilot AI requested review from Copilot and removed request for Copilot June 1, 2026 14:08
Copilot AI changed the title [WIP] Remove examples using JsonSerializerOptions overloads System.Text.Json source-gen docs: prefer JsonTypeInfo overloads and remove options/context overload examples Jun 1, 2026
Copilot AI requested a review from eiriktsarpalis June 1, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The System.Text.Json source generator docs contain examples using JsonSerializerOptions overloads

2 participants