Skip to content

DictionaryKeyPolicy only works for deserialization but not serialization #30008

Closed
dotnet/corefx
#39032

Description

@davidikin45

DictionaryKeyPolicy only works for deserialization but not serialization. When serializing ValidationProblemDetails the keys aren't converting to camelCase

Activity

  1. ahsonkhan commented on Jun 25, 2019

    @ahsonkhan
    Contributor

    Can you share a sample repro of the issue you are seeing?

    cc @steveharter, @pranavkm, @layomia

  2. davidikin45 commented on Jun 25, 2019

    @davidikin45
    Author
    services.Configure<JsonOptions>(options =>
    {
    	options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    	options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
    });
    
    {
        "errors": {
            "Email": [
                "The Email field is not a valid e-mail address."
            ]
        },
        "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
        "title": "Bad Request",
        "status": 400,
        "detail": "Please refer to the errors property for additional details.",
        "instance": "/Home/Contact",
        "extensions": {
            "traceId": "|89c19b00-496b18c2ce38774e.",
            "timeGenerated": "2019-06-25T03:15:35.525124Z"
        }
    }
    
  3. ahsonkhan commented on Jun 25, 2019

    @ahsonkhan
    Contributor
    [Fact]
    public static void CamelCaseOptionSerialize()
    {
        var options = new JsonSerializerOptions();
        options.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
    
        const string JsonString = @"[{""Key1"":1,""Key2"":2},{""Key1"":3,""Key2"":4}]";
        Dictionary<string, int>[] obj = JsonSerializer.Parse<Dictionary<string, int>[]>(JsonString);
    
        Assert.Equal(2, obj.Length);
        Assert.Equal(1, obj[0]["Key1"]);
        Assert.Equal(2, obj[0]["Key2"]);
        Assert.Equal(3, obj[1]["Key1"]);
        Assert.Equal(4, obj[1]["Key2"]);
    
        string jsonCamel = JsonSerializer.ToString<object>(obj);
        Assert.Equal(JsonString, jsonCamel);
    
        const string JsonCamel = @"[{""key1"":1,""key2"":2},{""key1"":3,""key2"":4}]";
        jsonCamel = JsonSerializer.ToString<object>(obj, options);
                
        // Fails
        // Expected: [{"key1":1,"key2":2},{"key1":3,"key2":4}]
        // Actual:   [{"Key1":1,"Key2":2},{"Key1":3,"Key2":4}]
        Assert.Equal(JsonCamel, jsonCamel); 
    }
  4. pranavkm commented on Jun 25, 2019

    @pranavkm
    Contributor

    @ahsonkhan this would be the scenario:

    var dictionary = new Dictionary<string, string>
    {
        { "Test", "value" }
    };
    
    var json = JsonSerializer.ToString(dictionary, new JsonSerializerOptions { DictionaryKeyPolicy = JsonNamingPolicy.CamelCase });
    Console.WriteLine(json); // Outputs: {"Test":"value"}

    The expectation here is that serialization would also honor the dictionary key policy.

  5. ahsonkhan commented on Jun 25, 2019

    @ahsonkhan
    Contributor

    We should also verify that PropertyNamingPolicy is working correctly for serialization.

  6. JamesNK commented on Jun 28, 2019

    @JamesNK
    Member

    Why is DictionaryKeyPolicy being used for deserialization? It should only be used for serialization.

  7. steveharter commented on Jul 1, 2019

    @steveharter
    Contributor

    Why is DictionaryKeyPolicy being used for deserialization? It should only be used for serialization.

    Using the naming policy on deserialization results in dictionary keys being consistent regardless of input, which can happen when not non-round tripping the JSON. For example with a camel-casing policy {"Foo" : 1} and {"foo":1} will result in the same dictionary key of foo. This may be required for any logic using the keys.

    If we don't apply the policy during deserialization but that semantics is necessary, the "workaround" would be to have the POCO instantiate a case-insensitive dictionary.

    Skipping calling on deserialize should help improve performance a bit...

    What are the scenarios where leaving the keys "as-is" during deserialization is desired?

  8. steveharter commented on Jul 1, 2019

    @steveharter
    Contributor
  9. JamesNK commented on Jul 1, 2019

    @JamesNK
    Member

    I'm sure we had discussed this. Naming policies are one way operations. When you convert pascal case "ID" to camel case "id" there is no way to reliably convert back again. Was the original string "ID" or "Id"?

    That is why there is only one convert method on the type rather than ConvertFrom/ConvertTo.

  10. transferred this issue fromdotnet/corefxon Feb 1, 2020
  11. added this to the 3.0 milestone on Feb 1, 2020
  12. 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

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions