19

I'm using Json.Net for my website. I want the serializer to serialize property names in camelcase by default. I don't want it to change property names that I manually assign. I have the following code:

public class TestClass
{
    public string NormalProperty { get; set; }

    [JsonProperty(PropertyName = "CustomName")]
    public string ConfiguredProperty { get; set; }
}

public void Experiment()
{
    var data = new TestClass { NormalProperty = null, 
        ConfiguredProperty = null };

    var result = JsonConvert.SerializeObject(data,
        Formatting.None,
        new JsonSerializerSettings {ContractResolver
            = new CamelCasePropertyNamesContractResolver()}
        );
    Console.Write(result);
}

The output from Experiment is:

{"normalProperty":null,"customName":null}

However, I want the output to be:

{"normalProperty":null,"CustomName":null}

Is this possible to achieve?

5
  • don't use CamelCasePropertyNamesContractResolver and use JsonProperty only. Commented Oct 5, 2012 at 15:26
  • @L.B If I only use JsonProperty, the default naming will be PascalCase, so normalProperty will instead be NormalProperty in the JSON. Commented Oct 5, 2012 at 16:53
  • Oliver No, It is serialized exactly as what you give in JsonProperty. Commented Oct 5, 2012 at 16:58
  • 1
    @L.B If there is no JsonProperty, it is serialized as NormalProperty. If I need to serialize a class that has 20 PascalCase properties, I don't want to have to write a JsonProperty for each of them to meet javascript naming conventions. It is needless effort. Commented Oct 5, 2012 at 19:43
  • 1
    The code above works for me without any modifications using version 7.0.1 of Newtonsoft.Json, so I think this may have been a bug that was fixed. Commented Oct 16, 2015 at 18:41

2 Answers 2

21

You can override the CamelCasePropertyNamesContractResolver class like this:

class CamelCase : CamelCasePropertyNamesContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member,
        MemberSerialization memberSerialization)
    {
        var res = base.CreateProperty(member, memberSerialization);

        var attrs = member
            .GetCustomAttributes(typeof(JsonPropertyAttribute),true);
        if (attrs.Any())
        {
            var attr = (attrs[0] as JsonPropertyAttribute);
            if (res.PropertyName != null)
                res.PropertyName = attr.PropertyName;
        }

        return res;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

One slight improvement if (res.PropertyName != null && attr.PropertyName != null) Doing this allows you to set a JsonProperty attribute on a field without a name and still have it handled with regular camel casing. Useful if you want to just set something like [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2

With the introduction of NamingStrategy it's easier.
As a bonus you can get it to not modify dictionary keys.

class MyContractResolver : CamelCasePropertyNamesContractResolver
{
    public MyContractResolver()
    {
        NamingStrategy.OverrideSpecifiedNames = false;    //Overriden
        NamingStrategy.ProcessDictionaryKeys = false;     //Overriden
        NamingStrategy.ProcessExtensionDataNames = false; //default
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.