8

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?

mikaelnet
  • 101
  • 1
  • 4

2 Answers2

2

Hmm - discovered that this change was done between 4.0R1 and 4.0R2. Here's the issue.

I can see that it's correct from a json-perspective, but I'm not sure if I really agree to the actual change. At least not making such a change between two minor versions.

A workaround is posted there as well.

mikaelnet
  • 101
  • 1
  • 4
1

Uhm.. Downgrade to a version that works the way you want it to.

Then file a bug report with Json.NET.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89