.NET Core 3.0 Web Api Dictionary Key CamelCase Issue
Asked Answered
K

1

6

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

  1. Issue 1
  2. Issue 2

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.

Kingcup answered 20/10, 2019 at 23:5 Comment(6)
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
You should go back to Newtonsoft Json if you need the old behavior, but imho its wrong behaving for the reasons named above. When deserializing, its impossible to restore the original casing if it gets turned into camelCase on serializing, because dictionary keys are values too in Json, not properties) --> "dictionary": [{"key": "MyKey", "value": "MyValue"}, ...]Evetta
Thank you for the response. So from what you are saying is that it is never a good idea to serialize the dictionary keys to be different then what they originally were when returning a response from the api (In my case transforming from Pascal to Camel). If I want then to be camelcase then I should transform it as I am adding them to the dictionary? I wonder why Newtonsoft Json did that automatically.Kingcup
Not if you want to serialize it back again, as pointed in this issue: JamesNK: 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"?Evetta
No, I am not interested in serializing it back again. I use these keys for permissions for my angular front end application. So I have these keys hardcoded in the app as camelcase. I could always update the hardcoded keys to be Pascal but not sure what exactly is correct approach. I would like to follow best practices. I would rather not add back Newtonsoft since .net core team doesn't recommend thatKingcup
I have just decided to use Newtonsoft just for the purpose of serializing the Dictionary Keys on an api response until .NET Core 3 supports the same functionality. Thank you for the assistanceKingcup
F
0

This is now possible in .Net 7 (possibly earlier). In the program.cs file:

services.AddControllers()
.AddJsonOptions(o =>
{
    o.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
});
Fascinate answered 10/5, 2023 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.