CamelCase breaks changes in Json.NET version 4

I just upgraded Json.NET from version 3.5 Release 7 to 4.0 Release 8 and realized that serialization is not doing the same. When serializing an object that contains the standard dictionary version 3.5, the dictionary keys remained unchanged, but when using 4.0, the contract recognizer is also applied to the keys.

For example, 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:

 [JsonObject(MemberSerialization.OptOut)] public class ProductFilter { public int Id { get; set; } public int NodeId { get; set; } public IDictionary<string, ProductFilterAttribute> Attributes { get; set; } } 

keys in the Attributes dictionary also become camelCase. In version 3.5R7, those that remain unchanged, and I think the right way.

An example of a fragment from output 3.5R7:

 { "id": 98659, "nodeId": 317970, "attributes": { "FULL_TIME_USE": { values: [ { "1" } ], formattedValue: "... 

Sample 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 solution and adding [JsonProperty("id")] , [JsonProperty("nodeId")] , etc. is not an option here)

Any ideas on how to solve this?

+8
json c # camelcasing
source share
2 answers

Hmm - found that this change was made between 4.0R1 and 4.0R2. Here is the problem.

I see this is correct from a json perspective, but I'm not sure if I agree to the actual change. At least do not make such a change between the two minor versions.

A workaround also goes there.

+2
source share

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

Then write the error report using Json.NET.

+1
source share

All Articles