Skip to content
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

Closed
wants to merge 6 commits into from

Conversation

AlexRadch
Copy link
Contributor

See #79059
I need some help with implementation.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jun 21, 2023
@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

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.

@ghost
Copy link

ghost commented Jun 21, 2023

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

Issue Details

See #79059
I need some help with implementation.

Author: AlexRadch
Assignees: -
Labels:

area-System.Text.Json, new-api-needs-documentation, community-contribution

Milestone: -

@@ -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)
Copy link
Member

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;
Copy link
Member

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?

{

/// <summary>
/// Ignores any metadata keys starting with $, such as `$schema`.
Copy link
Member

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:

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;
Copy link
Member

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:

private sealed class EqualityComparer : IEqualityComparer<JsonSerializerOptions>

Assert.Equal(JsonValueKind.Null, myClass.ExtensionData["$metadataNull"].ValueKind);
}

//[Fact]
Copy link
Member

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 <eirik.tsarpalis@gmail.com>
@eiriktsarpalis eiriktsarpalis added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jun 28, 2023
@ghost ghost added the no-recent-activity label Jul 13, 2023
@ghost
Copy link

ghost commented Jul 13, 2023

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@ghost
Copy link

ghost commented Jul 27, 2023

This pull request will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the pull request, but please note that it will be locked if it remains inactive for another 30 days.

@ghost ghost closed this Jul 27, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 26, 2023
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json community-contribution Indicates that the PR has been added by a community member needs-author-action An issue or pull request that requires more info or actions from the author. new-api-needs-documentation no-recent-activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants