-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Align dictionary key converter/metadata retrieval with pattern for collection elements #50074
Conversation
…llection elements
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsAlternative approach to #48232. Here we create a This is needed so incoming STJ source generator can pre-generate metadata for dictionary key types, and pass that directly to the serializer. This is analogous to the design for collection elements e.g. here. It should also help with other scenarios described in #48232. The breaking change described in #48232 (comment) where custom converters will throw
|
Are we OK with "breaking" this and taking a hard dependency on #46520? |
Yes, I believe so. It is rare for users to provide custom simple converters (aside from |
|
||
internal virtual void WriteWithQuotes(Utf8JsonWriter writer, [DisallowNull] T value, JsonSerializerOptions options, ref WriteStack state) | ||
=> throw new InvalidOperationException(); | ||
=> ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(TypeToConvert, this); | ||
|
||
internal sealed override void WriteWithQuotesAsObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options, ref WriteStack state) |
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.
Why do we box here?
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.
The only caller of this specific method is from ObjectConverter
, when serializing System.Object
-typed keys. The values to serialize are boxed at this point. ObjectConverter
forwards the (boxed) value to the typed converter for the runtime type of the instance. The typed converter then unboxes the value and serializes it.
It's the same as how TryWriteAsObject
works.
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.
Looks good; we may need to react if anyone with a primitive custom converter also needs quoted number support.
However, even before this PR that scenario could be considered broken since their custom converter wasn't used for quoted numbers.
...tem.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Dictionary.NonStringKey.cs
Outdated
Show resolved
Hide resolved
Hello @layomia! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
The change dotnet#50074 made rooting of built-in serializers lazier. By analyzing the library code I have figured out that I can root the default serializers by calling Serialize. This fixes the test failure. Thanks Tomas
Alternative approach to #48232. Here we create a
KeyClassInfo
for dictionary keys, similar to how it is done for dictionary and collection elements/values.This is needed so incoming STJ source generator can pre-generate metadata for dictionary key types, and pass that directly to the serializer. This is analogous to the design for collection elements e.g. here. It should also help with other scenarios described in #48232.
The breaking change described in #48232 (comment) where custom converters will throw
InvalidOperationExeption
(nowNotSupportedException
, following this change) can be mitigated by exposing the functionality to handle dictionary keys: #46520.