You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
KeyValuePair is not properly serialized with JsonNamingPolicy.CamelCase - System.Text.Json #1197
Reacted by Dominic Sachs, untaught, KKanev, Jonathan Hiben, ArrowCase, NoneGiven, Guilherme Meinlschmiedt Abdo, Matt, Pavel Anikhouski, Hans Bakker and 5 more
As a work-around it is possible to create a new converter that does this by coping the converter code from JsonValueConverterKeyValuePair.cs and KeyValuePairConverter and modifying appropriately.
Is KeyValuePair the only type that isn't covered by current options? Where else can the concept of "key" show up in .NET types? I don't know if adding a new policy just for a specific type makes sense (unless it happens to be a commonly used type/pattern). Alternatively, we have "KeyPolicy" which works for all "keys" (dictionary, keyvaluepair, etc.).
Key and Value are just properties on the struct. PropertyNamingPolicy should apply to them.
That's a good point. It looks like because we have a built-in converter for KeyValuePair, PropertyInfo is null when we try to determine the property name to emit and we bail out. When creating a JsonClassInfo, since ClassType is identified as Value, we don't reflect over the KVP object and get it's properties.
For user-defined structs, that isn't the case, and we (correctly) go down the code-path which calls PropertyNamingPolicy.ConvertName
A trivial fix would be to update JsonValueConverterKeyValuePair to honor PropertyNamingPolicy but I don't think that's the right place to apply the fix (especially since it would force us to go back from JsonEncodedText to string). I think us identifying an object (like KVP, or any type that has a built-in converter) as a ClassType.Value is probably not the right approach and we should re-evaluate that code path.
if(options.PropertyNamingPolicy!=null){// Not great, but would workstringmodifiedName=options.PropertyNamingPolicy.ConvertName(name.ToString());writer.WritePropertyName(modifiedName);}else{writer.WritePropertyName(name);}
@ahsonkhan I'm interested in seeing this fixed and can contribute.
What is the purpose of JsonValueConverterKeyValuePair? It seems like if it were removed, there would be no behavioral difference, except that PropertyNamingPolicy would start being respected.
If the converter is required for some reason, then its design is simply flawed, since it is not capable of respecting PropertyNamingPolicy while keeping statically cached property name values.
What is the purpose of JsonValueConverterKeyValuePair? It seems like if it were removed, there would be no behavioral difference, except that PropertyNamingPolicy would start being respected.
What is the purpose of JsonValueConverterKeyValuePair? It seems like if it were removed, there would be no behavioral difference, except that PropertyNamingPolicy would start being respected.
The serializer only supports public properties with public getters and setters for deserialization. KeyValuePair's Key and Value properties are readonly (no setter), so we use a converter to create them with their constructor.
If the converter is required for some reason, then its design is simply flawed, since it is not capable of respecting PropertyNamingPolicy while keeping statically cached property name values.
@layomia If done at that point, we would only be able to include support for Default and CamelCase, correct? No support for user-defined policies, and no ability to automatically use new built-in policies that may be added in the future.
With this constructor, the JsonEncodedText values for Key and Value could be obtained from the policy, cached in the factory, then passed to created constructors in the CreateConverter method (this means adding a new constructor for the JsonValueKeyValuePairConverter<,>).
needs-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
breaking-changeIssue or PR that represents a breaking API or functional change over a previous release.
and removed
breaking-changeIssue or PR that represents a breaking API or functional change over a previous release.
needs-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
needs-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
needs-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Activity
ahsonkhan commentedon Sep 25, 2019
I believe the camel case option for
DictionaryKeyPolicyonly works for Dictionary/IDictionary (which KeyValuePair isn't).https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.dictionarykeypolicy?view=netcore-3.0
https://github.com/dotnet/corefx/blob/ac99a1b7168bd32046a954c3f06012c0fa909bed/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleDictionary.cs#L151-L160
@steveharter - can you clarify what your expectations are for that feature? Is there anything we can do to support this scenario?
I'd imagine this can be resolved by a custom converter.
cc @layomia
steveharter commentedon Sep 25, 2019
This would require a new naming policy. I don't think we can expand
DictionaryKeyPolicyto coverKeyValuePairsince that would break existing users.e.g. we'd need to add a
KeyValuePairNamingPolicysteveharter commentedon Sep 25, 2019
As a work-around it is possible to create a new converter that does this by coping the converter code from
JsonValueConverterKeyValuePair.csandKeyValuePairConverterand modifying appropriately.ahsonkhan commentedon Sep 25, 2019
Is KeyValuePair the only type that isn't covered by current options? Where else can the concept of "key" show up in .NET types? I don't know if adding a new policy just for a specific type makes sense (unless it happens to be a commonly used type/pattern). Alternatively, we have "KeyPolicy" which works for all "keys" (dictionary, keyvaluepair, etc.).
steveharter commentedon Sep 25, 2019
Note that Dictionary<,> uses KeyValuePair<,> internally so a new policy
KeyValuePairPolicycould be implied for both Dictionary and KeyValuePair.JamesNK commentedon Sep 25, 2019
{ Key: "Test", Value: "Test1" }Key and Value are just properties on the struct.
PropertyNamingPolicyshould apply to them.ahsonkhan commentedon Sep 26, 2019
That's a good point. It looks like because we have a built-in converter for KeyValuePair,
PropertyInfois null when we try to determine the property name to emit and we bail out. When creating aJsonClassInfo, sinceClassTypeis identified asValue, we don't reflect over the KVP object and get it's properties.https://github.com/dotnet/corefx/blob/b832f6ccdebd18469e4b6d03c1c9b2c9c6c14374/src/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs#L88-L114
For user-defined structs, that isn't the case, and we (correctly) go down the code-path which calls
PropertyNamingPolicy.ConvertNameA trivial fix would be to update
JsonValueConverterKeyValuePairto honorPropertyNamingPolicybut I don't think that's the right place to apply the fix (especially since it would force us to go back fromJsonEncodedTextto string). I think us identifying an object (like KVP, or any type that has a built-in converter) as aClassType.Valueis probably not the right approach and we should re-evaluate that code path.https://github.com/dotnet/corefx/blob/b832f6ccdebd18469e4b6d03c1c9b2c9c6c14374/src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterKeyValuePair.cs#L113-L117
ArrowCase commentedon Dec 17, 2019
@ahsonkhan I'm interested in seeing this fixed and can contribute.
What is the purpose of
JsonValueConverterKeyValuePair? It seems like if it were removed, there would be no behavioral difference, except thatPropertyNamingPolicywould start being respected.If the converter is required for some reason, then its design is simply flawed, since it is not capable of respecting
PropertyNamingPolicywhile keeping statically cached property name values.ahsonkhan commentedon Dec 18, 2019
@steveharter, @layomia - can you please help here.
ahsonkhan commentedon Dec 18, 2019
From @vladinvancouver in https://github.com/dotnet/corefx/issues/41813
layomia commentedon Dec 20, 2019
The serializer only supports public properties with public getters and setters for deserialization. KeyValuePair's
KeyandValueproperties are readonly (no setter), so we use a converter to create them with their constructor.A solution here might be to obtain and cache the correct
KeyandValueproperty names during the construction of theJsonKeyValuePairConverterfactory, and pass those to each created instance of theJsonConverter<TKey, TValue>converters.@ahsonkhan @ArrowCase
ArrowCase commentedon Dec 20, 2019
@layomia If done at that point, we would only be able to include support for
DefaultandCamelCase, correct? No support for user-defined policies, and no ability to automatically use new built-in policies that may be added in the future.layomia commentedon Dec 20, 2019
@ArrowCase
options.PropertyNamingPolicywill have the specified naming policy. I'm imagining a new constructor forJsonKeyValuePairConverterwhich takes an options instance. The call to the constructor would look like this:With this constructor, the
JsonEncodedTextvalues forKeyandValuecould be obtained from the policy, cached in the factory, then passed to created constructors in theCreateConvertermethod (this means adding a new constructor for theJsonValueKeyValuePairConverter<,>).1 remaining item
ericstj commentedon Jul 24, 2020
@layomia can you please file a breaking change issue: https://github.com/dotnet/docs/issues/new?template=dotnet-breaking-change.md
ericstj commentedon Sep 25, 2020
Removing breaking change as it is tracked with #36424