DictionaryKeyPolicy only works for deserialization but not serialization
DictionaryKeyPolicy only works for deserialization but not serialization. When serializing ValidationProblemDetails the keys aren't converting to camelCase
Can you share a sample repro of the issue you are seeing?
cc @steveharter, @pranavkm, @layomia
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"
}
}
[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);
}
@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.
We should also verify that PropertyNamingPolicy is working correctly for serialization.
Why is DictionaryKeyPolicy being used for deserialization? It should only be used for serialization.
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?
cc @pranavkm @rynowak
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.