Newtonsoft.json: JsonExtensionData and camel case serialization

Created on 10 Nov 2016  路  10Comments  路  Source: JamesNK/Newtonsoft.Json

It seems that, when using JsonExtensionData on a parent class, the child class's additional attributes are not converted to camel case. The shared ones are.

It seems like maybe the camel case isn't being applied to the extended data dictionary keys?

So returning a base enumeration I get something like the following

ParentClass {
       Id;
       SharedField;
        [JsonExtensionData]
        Dictionary<string, JToken>  _extendedData;
}
SubClass extends ParentClass {
        AdditionalField;
}

"subClass": {
      "id": 1,
      "sharedField": "somevalue",
      "AdditionalField": "somevalue"
    }

whereas I would expect

"subClass": {
      "id": 1,
      "sharedField": "somevalue",
      "additionalField": "somevalue"
    }

Most helpful comment

One more linked: Names of properties from JsonExtensionData dictionay are serialized not in Camel Case while JsonSerializerSettings.ContractResolver is CamelCasePropertyNamesContractResolver

For example,

class Person
{
    public String Name { get; set; }

    [JsonExtensionData]
    public Dictionary<String, JToken> AdditionalProperties { get; set; }
}
var person = new Person
{
    Name = "John",
    AdditionalProperties = new Dictionary<String, JToken> {{"FormOfAddress", "Mr"}}
};

var serializerSettings = new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
};

var personJson = JsonConvert.SerializeObject(person, serializerSettings);

Console.WriteLine(personJson);

And in output I have
{"name":"John","FormOfAddress":"Mr"}
But expected
{"name":"John","formOfAddress":"Mr"}

All 10 comments

One more linked: Names of properties from JsonExtensionData dictionay are serialized not in Camel Case while JsonSerializerSettings.ContractResolver is CamelCasePropertyNamesContractResolver

For example,

class Person
{
    public String Name { get; set; }

    [JsonExtensionData]
    public Dictionary<String, JToken> AdditionalProperties { get; set; }
}
var person = new Person
{
    Name = "John",
    AdditionalProperties = new Dictionary<String, JToken> {{"FormOfAddress", "Mr"}}
};

var serializerSettings = new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
};

var personJson = JsonConvert.SerializeObject(person, serializerSettings);

Console.WriteLine(personJson);

And in output I have
{"name":"John","FormOfAddress":"Mr"}
But expected
{"name":"John","formOfAddress":"Mr"}

I am having a similar issue.

I got the same problem today and don't have to open an issue for it :)
@JamesNK when will this be released ? or is there any nightly releases for this project ?

@JamesNK im using version 9.0.1 and i still have a problem, related issue https://github.com/JamesNK/Newtonsoft.Json/issues/868

[JsonExtensionData]
public Dictionary<string, object> Values { get; set; }

in this i have properties named TestCar1 = "anyvalue", TestCar2="val2val"
and i serialize it like this:

var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
var json = JsonConvert.SerializeObject(data, Formatting.Indented, jsonSerializerSettings);

however thay dont get camel cased.

Because it isn't released yet. No I don't have a date for you.

@JamesNK this is still not released from 2 years ago? Because it looks like this is still a bug

image

For anyone else struggling with this, I can save you a few hours :).

Make sure you're setting ProcessExtensionDataNames to true in your NamingStrategy

https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_Serialization_NamingStrategy_ProcessExtensionDataNames.htm

e.g.

var formatter = new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver
    {
        NamingStrategy = new CamelCaseNamingStrategy
        {
            ProcessExtensionDataNames = true
        }
    }
};

@brianwebb I've had that enabled the whole time; doesnt actually fix it

@onionhammer I think the issue you're seeing is because of this line:
var obj = JsonConvert.DeserializeObject<Data>(data);

Have a look here - https://stackoverflow.com/questions/35777561/get-a-dynamic-object-for-jsonconvert-deserializeobject-making-properties-upperca/35784914#35784914

Fiddle showing the issue here - https://dotnetfiddle.net/Jr90D2

Deserialising a nested object creates a JToken heirarchy rather than a dynamic object. This means that it will keep the original tag name - in your case "Toes" with a capital T.

Was this page helpful?
0 / 5 - 0 ratings