Skip to content

Add System.Text.Json union support#128162

Open
eiriktsarpalis wants to merge 4 commits into
dotnet:mainfrom
eiriktsarpalis:feature/json-unions
Open

Add System.Text.Json union support#128162
eiriktsarpalis wants to merge 4 commits into
dotnet:mainfrom
eiriktsarpalis:feature/json-unions

Conversation

@eiriktsarpalis
Copy link
Copy Markdown
Member

@eiriktsarpalis eiriktsarpalis commented May 13, 2026

Summary

Implements System.Text.Json support for C# union-like shapes:

  • adds union contract metadata (JsonTypeInfoKind.Union, JsonUnionCaseInfo, JsonUnionInfoValues<T>, union constructor/deconstructor hooks)
  • adds JsonUnionAttribute, JsonTypeClassifier, and classifier factory/context APIs for union and polymorphic type classification
  • adds union converter support for reflection and source-generated metadata
  • teaches the source generator to discover union case constructors and emit union metadata
  • adds reflection, source-generation, and source-generator unit coverage for union and structural classifier scenarios

Fixes #127299.

Adds System.Text.Json contract metadata, converters, source generation support, and tests for C# union type serialization.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds System.Text.Json support for C# union-like types, including runtime metadata, custom JSON type classifier APIs, converter support, source-generation plumbing, diagnostics, schema handling, and test coverage.

Changes:

  • Adds union metadata/converter infrastructure and classifier APIs for unions and polymorphic types.
  • Extends source generation to emit union and polymorphism/classifier metadata plus diagnostics.
  • Adds reflection/source-generation tests and updates generated baselines/resources.

Reviewed changes

Copilot reviewed 163 out of 163 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
src/libraries/System.Text.Json/src/System.Text.Json.csproj Includes new union/classifier source files and shared compiler attributes.
src/libraries/System.Text.Json/ref/System.Text.Json.csproj Adds shared compiler attribute references for ref build.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonUnionAttribute.cs Adds union customization attribute.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonPolymorphicAttribute.cs Adds polymorphic classifier hook.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonTypeClassifier*.cs Adds classifier delegate, context, factory, and kind APIs.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonValueType.cs Adds internal JSON value-shape flags.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Union/* Adds union converter and converter factory.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/* Advertises supported JSON value shapes for built-in converters.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/*Union* Adds union metadata values and reflection/source-gen metadata support.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo*.cs Adds union constructor/deconstructor, union cases, classifier resolution, and kind support.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonObjectInfoValuesOfT.cs Adds generated polymorphism/classifier metadata slots.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonCollectionInfoValuesOfTCollection.cs Adds generated polymorphism/classifier metadata slots for collections.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonMetadataServices*.cs Adds union creation and generated polymorphism metadata wiring.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver*.cs Adds reflection resolver union discovery and classifier wiring.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/MemberAccessor*.cs Adds constructor/getter helpers used by union metadata.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions*.cs Adds classifier list option and cache/equality integration.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs Adds polymorphic classifier dispatch during metadata handling.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter*.cs Extends converter strategy/value-shape and classifier polymorphic handling.
src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs Adds schema generation for union contracts.
src/libraries/System.Text.Json/src/Resources/Strings.resx Adds runtime union/classifier error messages.
src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs Adds source-generation classifier option.
src/libraries/System.Text.Json/gen/* Adds source-generator model, diagnostics, parser/emitter, resources, and target updates for unions/classifiers.
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/* Adds reflection serializer union/classifier test wrappers and project includes.
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/* Adds source-generation union/classifier/polymorphism tests and project includes.
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/* Adds diagnostics/output support and baseline updates.
docs/project/list-of-diagnostics.md Documents new SYSLIB1227/SYSLIB1228 diagnostics.

Comment thread src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Add netcoreapp type forwards for compiler support attributes embedded in downlevel System.Text.Json assets so package ApiCompat sees matching surface area across target frameworks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 14, 2026 17:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI review requested due to automatic review settings May 14, 2026 17:54
Clarify classifier documentation and align union constructor dispatch between reflection and source generation. Reject byref union case constructors and add regression coverage for overlapping and nullable cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

[API Proposal]: System.Text.Json union type support

2 participants