Add serialization support for tuple types#126054
Add serialization support for tuple types#126054eiriktsarpalis wants to merge 2 commits intodotnet:mainfrom
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
|
Put my two cents here: Tuples should be serialized into array, because that's how tuples works in JavaScript: const [first, second, third, ...] = tuple; |
It's in the cards, but that would necessarily have to be an opt in feature (aka necessitating new API to opt us into it). |
Why? |
Tuples serialize as objects today (albeit with issues). I think there is merit in keeping this as the default behaviour |
e5d40c5 to
42149f9
Compare
The existing behavior is already incorrect:
Flattening it will be a breaking change, too. So in either way this will be a breaking change. I think the right move should be making breaking change toward correcting the behavior to match JavaScript given that the breaking change here is unavoidable. |
It would not break people serializing tuples of arity < 8 (which represents 95% of tuple use cases) that have enabled
If you're talking about TypeScript tuple types, these represent a refinement over js arrays. Their serialization as JSON arrays is therefore a natural consequence of their run-time representation. While I don't disagree that we should try to support this mode, I don't see it as a natural or better way of representing tuples. |
…x trimming - Replace string-based IsReferenceTupleType in source generator parser with symbol comparison via KnownTypeSymbols (addresses @stephentoub feedback) - Move IsTupleType from DefaultJsonTypeInfoResolver to JsonHelpers to avoid rooting DefaultJsonTypeInfoResolver from the source-gen metadata path, which prevented it from being trimmed when IsReflectionEnabledByDefault=false Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
42149f9 to
677ba98
Compare
Note
This PR was AI/Copilot-generated.
Summary
Adds first-class serialization support for
ValueTupleandSystem.Tupletypes in System.Text.Json. Tuples now serialize without requiringIncludeFields = true.Key feature: Tuple element flattening
For tuples with more than 7 elements, the internal
Restnesting structure is flattened so that elements appear with logical names (Item1throughItemN) at the top level:This works for both
ValueTuple(struct tuples) andSystem.Tuple(class tuples), and for both the reflection-based and source-generated serializers.Implementation
Source generator:
Restchain and emits flattened property specs withRest.Rest...ItemNaccessor patternsIsTupleElementflag onPropertyGenerationSpecbypassesIncludeFieldscheck in fast pathReflection path:
FlattenTuplePropertiespost-processes the property list to replaceRestwith synthesizedItem8+propertiesMemberAccessor.CreateTupleElementGetter/Setterprovides IL-emitted accessors for nested field chainsReflectionMemberAccessorprovides a reflection-based fallback for AOT scenariosNo new public API surface. Tuple detection and field inclusion are handled internally.
Fixes #70352