-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Support IReadOnlyDictionary as JsonExtensionData #120636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…ression Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@copilot, there are test failures:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR extends JsonExtensionData support to include IReadOnlyDictionary<string, object>
and IReadOnlyDictionary<string, JsonElement>
as valid extension data property types, alongside the existing IDictionary
support.
- Adds validation logic to accept IReadOnlyDictionary interface types when Dictionary can be assigned to them
- Updates instantiation logic to create Dictionary instances for IReadOnlyDictionary extension data properties
- Enhanced documentation to reflect the new supported types
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
JsonTypeInfo.cs | Added validation logic for IReadOnlyDictionary extension data properties |
JsonSerializer.Read.HandlePropertyName.cs | Added instantiation logic to create Dictionary instances for IReadOnlyDictionary properties |
JsonExtensionDataAttribute.cs | Updated XML documentation to include IReadOnlyDictionary support |
ExtensionDataTests.cs | Added comprehensive tests for IReadOnlyDictionary extension data functionality |
DefaultJsonTypeInfoResolverTests.JsonPropertyInfo.cs | Added test cases for IReadOnlyDictionary types in extension data validation |
Fixed in commit 3f9c7dc. The source generator's |
…ion data Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@copilot, please ensure you build and fully test all changes. There are build failures:
|
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
... Fixed in commit 8f4a3e0. The |
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Adds support for using
IReadOnlyDictionary<string, object>
andIReadOnlyDictionary<string, JsonElement>
as JsonExtensionData properties, enabling them to work identically toIDictionary
types.Changes Made
IsValidExtensionDataProperty
to acceptIReadOnlyDictionary<string, object>
andIReadOnlyDictionary<string, JsonElement>
interface typesCreateExtensionDataProperty
to create mutableDictionary
instances for IReadOnlyDictionary propertiesJsonExtensionDataAttribute
XML documentation to reflect IReadOnlyDictionary supportobject
andJsonElement
value typesImplementation Details
The implementation creates a mutable
Dictionary<TKey, TValue>
instance internally when deserializing to an IReadOnlyDictionary extension data property. This approach mirrors how JsonSerializer handles other read-only collection interfaces - by instantiating a mutable concrete type, populating it, and assigning it to the read-only interface property.For pre-populated properties, the existing dictionary contents are preserved by copying them to the new Dictionary instance before deserialization adds additional key-value pairs, with duplicate keys being overwritten by the deserialized values.
Compatibility
This change is fully backward compatible. Existing code using
IDictionary
extension data properties continues to work unchanged. The implementation reuses existing serialization/deserialization infrastructure, ensuring consistent behavior betweenIDictionary
andIReadOnlyDictionary
extension data properties.Fixes #31645
Original prompt
Fixes #31645
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.