I've just upgraded our Json.NET from version 3.5 Release 7 to 4.0 Release 8, and realized that seralization isn't done in the same way. When serializing an object that contains a standard Dictionary the 3.5 version left the dictionary keys unchanged, but when using 4.0, the contract resolver applies to the keys as well.
For example, when using the following JsonSerializerSettings:
jsonSerializerSettings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> { new JavaScriptDateTimeConverter() },
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
and when serializing an object like this one:
[JsonObject(MemberSerialization.OptOut)]
public class ProductFilter
{
public int Id { get; set; }
public int NodeId { get; set; }
public IDictionary<string, ProductFilterAttribute> Attributes { get; set; }
}
the keys in the Attributes
dictionary becomes camelCase as well. In version 3.5R7 those where left unchanged and I think that's the correct way.
Example snippet from 3.5R7 output:
{
"id": 98659,
"nodeId": 317970,
"attributes": {
"FULL_TIME_USE": {
values: [ { "1" } ],
formattedValue: "...
Example snippet from 4.0R8 output:
{
"id": 98659,
"nodeId": 317970,
"attributes": {
"fULL_TIME_USE": {
values: [ { "1" } ],
formattedValue: "...
(We have a lot of similar code, so removing the camelCase resolving and adding [JsonProperty("id")]
, [JsonProperty("nodeId")]
etc isn't really an option here)
Any ideas of how to solve this?