I have a .NET Core Web Api which I recently migrated from version 2.2 to version 3.0. I have a route/method in one of my controllers that returns a Dictionary. The issue I am now having is that when the response is returned from the api, the Dictionary Keys are in PascalCase and not in CamelCase like they used to be before I updgraded.
Prior to my update to .NET Core 3.0 I had the following code in my startup
services.AddMvcCore()
.AddJsonOptions(opts =>
{
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
Now, since the removal of the Newtonsoft Json.Net package I removed the above block of code and replaced it with the following -
services.AddControllers(config =>
{
// Some configuration in here
})
.AddJsonOptions(options => options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase)
For some reason I am still getting back the Dictionary Keys as Pascal Case when I want the dictionary keys to be in CamelCase. I have tried a few other ways to no avail. I read through the migration documentation and could not find any information about this particular subject. I also ran across a few issues on GitHub that are related, but did not help
Maybe I missed an extra configuration somewhere in order to get this to work or I need to go back to the Newtonsoft package, I am just not sure.
JsonNamingPolicy.CamelCase
is for properties. Dictionary keys are no properties. The correct behavior is indeed, that keys are not serialized. Otherwise serializing and deserializing a string the same collection would yield two different results (the deserialized collection then would have lower case keys, which changes the expected behavior) – Evetta"dictionary": [{"key": "MyKey", "value": "MyValue"}, ...]
– Evetta