Click or drag to resize
Json.NET

Configure NamingStrategy property name serialization

 

This sample configures a CamelCaseNamingStrategy to not camel case properties that already have a name specified with an attribute.

Sample
Types
public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [JsonProperty(PropertyName = "UPN")]
    public string Upn { get; set; }
}
Usage
User user = new User
{
    FirstName = "John",
    LastName = "Smith",
    Upn = "john.smith@acme.com"
};

DefaultContractResolver contractResolver = new DefaultContractResolver
{
    NamingStrategy = new CamelCaseNamingStrategy
    {
        OverrideSpecifiedNames = false
    }
};

string json = JsonConvert.SerializeObject(user, new JsonSerializerSettings
{
    ContractResolver = contractResolver,
    Formatting = Formatting.Indented
});

Console.WriteLine(json);
// {
//   "firstName": "John",
//   "lastName": "Smith",
//   "UPN": "john.smith@acme.com"
// }