-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Support filtering property names on dictionary deserialization #87868
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsSee #79059
|
@@ -16,6 +17,12 @@ internal abstract class JsonDictionaryConverter<TDictionary> : JsonResumableConv | |||
private protected sealed override ConverterStrategy GetDefaultConverterStrategy() => ConverterStrategy.Dictionary; | |||
|
|||
protected internal abstract bool OnWriteResume(Utf8JsonWriter writer, TDictionary dictionary, JsonSerializerOptions options, ref WriteStack state); | |||
|
|||
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options) |
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.
Seems like this isn't doing anything?
} | ||
} | ||
|
||
TKey key; |
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.
This is repeating the logic of the existing ReadDictionaryKey
method. It seems like you could simply extract the DictionaryKeyFilter
logic into a standalone helper?
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonDictionaryKeyFilter.cs
Outdated
Show resolved
Hide resolved
{ | ||
|
||
/// <summary> | ||
/// Ignores any metadata keys starting with $, such as `$schema`. |
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.
Rather than ignoring every key starting with $
, I would recommend only checking for metadata property names that have built-in support in JSON:
Lines 19 to 22 in 5ca6826
private static readonly byte[] s_idPropertyName = "$id"u8.ToArray(); | |
private static readonly byte[] s_refPropertyName = "$ref"u8.ToArray(); | |
private static readonly byte[] s_typePropertyName = "$type"u8.ToArray(); | |
private static readonly byte[] s_valuesPropertyName = "$values"u8.ToArray(); |
@@ -105,6 +106,7 @@ public JsonSerializerOptions(JsonSerializerOptions options) | |||
// 2. _typeInfoResolverChain can be created lazily as it relies on | |||
// _typeInfoResolver as its source of truth. | |||
|
|||
_dictionaryKeyFilter = options._dictionaryKeyFilter; |
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.
You should also update the EqualityComparer implementation in JsonSerializerOptions.Caching
to account for the new property:
Line 480 in 5ca6826
private sealed class EqualityComparer : IEqualityComparer<JsonSerializerOptions> |
Assert.Equal(JsonValueKind.Null, myClass.ExtensionData["$metadataNull"].ValueKind); | ||
} | ||
|
||
//[Fact] |
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.
Commented out code?
…ion/JsonDictionaryKeyFilter.cs Co-authored-by: Eirik Tsarpalis <[email protected]>
This pull request has been automatically marked |
This pull request will now be closed since it had been marked |
See #79059
I need some help with implementation.