Contract Resolver

Json.NET

Json.NET - Quick Starts & API Documentation Contract Resolvers

The IContractResolver interface provides a way to customize how the JsonSerializer serializes and deserializes .NET objects to JSON.

Implementing the IContractResolver interface and then assigning an instance to a JsonSerializer lets you control whether the object is serialized as a JSON object or JSON array, what object members should be serialized, how they are serialized and what they are called.

DefaultContractResolver

The DefaultContractResolver is the default resolver used by the serializer. It provides many avenues of extensibility in the form of virtual methods that can be overriden.

CamelCasePropertyNamesContractResolver

CamelCasePropertyNamesContractResolver inherits from DefaultContractResolver and simply overrides the JSON property name to be written in camelcase.

Product product = new Product
                    {
                      ExpiryDate = new DateTime(2010, 12, 20, 18, 1, 0, DateTimeKind.Utc),
                      Name = "Widget",
                      Price = 9.99m,
                      Sizes = new[] {"Small", "Medium", "Large"}
                    };
 
string json = 
  JsonConvert.SerializeObject(
    product,
    Formatting.Indented,
    new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
  );
 
//{
//  "name": "Widget",
//  "expiryDate": "\/Date(1292868060000)\/",
//  "price": 9.99,
//  "sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}