Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CamelCase breaking change in Json.NET version 4

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?

like image 463
mikaelnet Avatar asked Mar 13 '12 13:03

mikaelnet


People also ask

What is Jsonproperty annotation C#?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

Does .NET support Json?

Text. Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON).

How does Newtonsoft Json work?

Newtonsoft. Json uses reflection to get constructor parameters and then tries to find closest match by name of these constructor parameters to object's properties. It also checks type of property and parameters to match. If there is no match found, then default value will be passed to this parameterized constructor.

Why do we use Newtonsoft Json?

The Newtonsoft. JSON namespace provides classes that are used to implement the core services of the framework. It provides methods for converting between . NET types and JSON types.


1 Answers

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.

like image 180
mikaelnet Avatar answered Sep 28 '22 09:09

mikaelnet