Skip to content

KeyValuePair is not properly serialized with JsonNamingPolicy.CamelCase - System.Text.Json #1197

Closed

Description

@untaught

When serializing a collection of KeyValuePair with CamelCase enabled the properties are serialized in PascalCase:

MVC Configuration is:

.AddJsonOptions(options =>
{
    options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
}

The result is:

{ Key: "Test", Value: "Test1" }

Expected result:

{ key: "Test", value: "Test1" }

Activity

  1. ahsonkhan commented on Sep 25, 2019

    @ahsonkhan
    Contributor
  2. steveharter commented on Sep 25, 2019

    @steveharter
    Contributor

    This would require a new naming policy. I don't think we can expand DictionaryKeyPolicy to cover KeyValuePair since that would break existing users.

    e.g. we'd need to add a KeyValuePairNamingPolicy

  3. steveharter commented on Sep 25, 2019

    @steveharter
    Contributor

    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.

  4. ahsonkhan commented on Sep 25, 2019

    @ahsonkhan
    Contributor

    This would require a new naming policy.

    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.).

  5. steveharter commented on Sep 25, 2019

    @steveharter
    Contributor

    Note that Dictionary<,> uses KeyValuePair<,> internally so a new policy KeyValuePairPolicy could be implied for both Dictionary and KeyValuePair.

  6. JamesNK commented on Sep 25, 2019

    @JamesNK
    Member

    { Key: "Test", Value: "Test1" }

    Key and Value are just properties on the struct. PropertyNamingPolicy should apply to them.

  7. ahsonkhan commented on Sep 26, 2019

    @ahsonkhan
    Contributor

    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.

    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.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.

    https://github.com/dotnet/corefx/blob/b832f6ccdebd18469e4b6d03c1c9b2c9c6c14374/src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterKeyValuePair.cs#L113-L117

    if (options.PropertyNamingPolicy != null)
    {
        // Not great, but would work
        string modifiedName = options.PropertyNamingPolicy.ConvertName(name.ToString());
        writer.WritePropertyName(modifiedName);
    }
    else
    {
        writer.WritePropertyName(name);
    }
  8. ArrowCase commented on Dec 17, 2019

    @ArrowCase

    @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.

  9. ahsonkhan commented on Dec 18, 2019

    @ahsonkhan
    Contributor

    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.

    @steveharter, @layomia - can you please help here.

  10. ahsonkhan commented on Dec 18, 2019

    @ahsonkhan
    Contributor

    From @vladinvancouver in https://github.com/dotnet/corefx/issues/41813

    CamelCase naming policy is not applied to list of KeyValuePair.

    Code to reproduce:

    JsonSerializerOptions options = new JsonSerializerOptions()
    {
    WriteIndented = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
    };

    Dictionary<string, object> dict = new Dictionary<string, object>();
    dict.Add("Item1", "Value1");
    dict.Add("Item2", 22);

    string json = JsonSerializer.Serialize(dict.ToList(), options);

    Console.WriteLine(json);

    Actual results:

    [
    {
    "Key": "Item1",
    "Value": "Value1"
    },
    {
    "Key": "Item2",
    "Value": 22
    }
    ]

    Expected results:

    [
    {
    "key": "Item1",
    "value": "Value1"
    },
    {
    "key": "Item2",
    "value": 22
    }
    ]

  11. layomia commented on Dec 20, 2019

    @layomia
    Contributor

    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.

    A solution here might be to obtain and cache the correct Key and Value property names during the construction of the JsonKeyValuePairConverter factory, and pass those to each created instance of the JsonConverter<TKey, TValue> converters.

    @ahsonkhan @ArrowCase

  12. ArrowCase commented on Dec 20, 2019

    @ArrowCase

    @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.

  13. layomia commented on Dec 20, 2019

    @layomia
    Contributor

    @ArrowCase options.PropertyNamingPolicy will have the specified naming policy. I'm imagining a new constructor for JsonKeyValuePairConverter which takes an options instance. The call to the constructor would look like this:

    converters.Add(new JsonKeyValuePairConverter(this));

    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<,>).

  14. transferred this issue fromdotnet/corefxon Dec 28, 2019
  15. 1 remaining item

  16. removed
    untriaged
    on Dec 28, 2019
  17. added this to the 5.0 milestone on Dec 28, 2019
  18. added
    breaking-change
    on Feb 26, 2020
  19. self-assigned this
    on Apr 9, 2020
  20. added
    needs-breaking-change-doc-created
    on Jul 30, 2020
  21. ericstj commented on Sep 25, 2020

    @ericstj
    Member

    Removing breaking change as it is tracked with #36424

  22. added
    breaking-change
    and removed
    breaking-change
    needs-breaking-change-doc-created
    on Sep 25, 2020
  23. ghost added
    needs-breaking-change-doc-created
    on Sep 25, 2020
  24. removed
    needs-breaking-change-doc-created
    on Sep 25, 2020
  25. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Text.Jsonbreaking-changebug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions