New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Camel-cased property names + dictionary-as-object serialization = lossy conversion #1023
Comments
Yes changing the dictionary key on serialization is lossy. That is by design. That is why I defaulted it to off with NamingStrategy. I believe it is also off by default with MVC Core. |
I thought so as well - I read through the source and indeed found the naming strategy bits. However, here is .NET Fiddle with a reproduction (using Newtonsoft.Json 9.0.1) Am I misinterpreting the results?
Results:
|
It appears that I found this out from @khellang over on this thread, where he points out that MVC uses Given the unexpected effect of |
@bwatts |
tl;dr; use CamelCaseNamingStrategy if you don't want dictionary keys touched. |
This issue started as a comment on the ASP.NET MVC issue concerning camel-casing of property names by default.
Below is a copy of the comment I left over there. Is this an issue that should be solved at the level of JSON.NET?
I just encountered an interesting scenario with camel-cased names and wanted to ensure we account for it.
The default serialization for a
Dictionary<,>
is to a JSON object:{ "foo": "...", "bar": "..." }
However, if we serialize an upper-cased key
Foo
, it will become lower-cased as a property, then deserialize back with the lower-cased name, creating a lossy conversion.We worked around this by creating a
DictionaryConverter
that treats dictionaries asList<KeyValuePair<K, V>>
:We wanted to simply tell JSON.NET to use
JsonArrayContract
for dictionaries, but had to account for existing data that uses the object-not-array format.The text was updated successfully, but these errors were encountered: