0

I have an object and I want to serialize it to a custom serialization format. e.g

     class MyObj
     {
        public string Name { get;set;}
        public Dictionary<string, string> KeyValues {get;set; }
     }

I want to camelcase Name but not KeyValues. Is this possible in Newtonsoft? I know how to do this for the entire object but not for specific properties.

So, the output should look like this:

    {
      "name" : "Mike", 
      "keyValues": 
       {
           "Abc": "x",
           "Prv": "y"
       }
    }
BSMP
  • 4,596
  • 8
  • 33
  • 44
Abhay
  • 564
  • 6
  • 14
  • I used short example. but I have used camelcase JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), }; this is for other objects in class. I need non camel case for only KeyValues inside it. – Abhay May 15 '17 at 22:11

1 Answers1

0

You just have to attribute the fields with JsonProperty and set the label that you want to use:

public class MyObj
{
    [JsonProperty("name")]
    public string Name { get;set;}

    [JsonProperty("KeyValues")]
    public Dictionary<string, string> KeyValues {get;set; }
}

JsonProperty is included in Newtonsoft.Json library.

javier_el_bene
  • 450
  • 2
  • 10
  • 25
  • this is not useful. as I have used camelcase serializer for other properties, and I want to only customize several properties without camelcasing – Abhay May 15 '17 at 22:11
  • Could you explain why is not useful, so I can help you? Using this attribute you can set any label you want, so I think you can achieve what you want. – javier_el_bene May 15 '17 at 22:13
  • I have used CamelCasePropertyNamesContractResolver as my class contains several nested classes which needs this. I want to control only few members not to have camel case – Abhay May 15 '17 at 22:15