<html> <head> <title>Contract Resolver</title> <link href="styles.css" rel="stylesheet" type="text/css" /> <link href="custom.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="control"> <span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br /> <span class="topicTitle">Contract Resolvers</span></div> <div id="content"> <span style="color: DarkGray"> </span> <p>The <a href="./html/T_Newtonsoft_Json_Serialization_IContractResolver.htm">IContractResolver</a> interface provides a way to customize how the JsonSerializer serializes and deserializes .NET objects to JSON.</p> <p>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.</p> <h4>DefaultContractResolver</h4> <p>The <a href="./html/T_Newtonsoft_Json_Serialization_DefaultContractResolver.htm">DefaultContractResolver</a> is the default resolver used by the serializer. It provides many avenues of extensibility in the form of virtual methods that can be overriden.</p> <h4>CamelCasePropertyNamesContractResolver</h4> <p><a href="./html/T_Newtonsoft_Json_Serialization_CamelCasePropertyNamesContractResolver.htm">CamelCasePropertyNamesContractResolver</a> inherits from DefaultContractResolver and simply overrides the JSON property name to be written in <a href="http://en.wikipedia.org/wiki/CamelCase" target="_blank">camelcase</a>.</p> <div class="overflowpanel"> <div class="code"> <div style="font-family: courier new; color: black; font-size: 10pt;"> <pre style="margin: 0px;"><span style="color: rgb(43, 145, 175);">Product</span> product = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Product</span></pre> <pre style="margin: 0px;"> {</pre> <pre style="margin: 0px;"> ExpiryDate = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DateTime</span>(2010, 12, 20, 18, 1, 0, <span style="color: rgb(43, 145, 175);">DateTimeKind</span>.Utc),</pre> <pre style="margin: 0px;"> Name = <span style="color: rgb(163, 21, 21);">"Widget"</span>,</pre> <pre style="margin: 0px;"> Price = 9.99m,</pre> <pre style="margin: 0px;"> Sizes = <span style="color: blue;">new</span>[] {<span style="color: rgb(163, 21, 21);">"Small"</span>, <span style="color: rgb(163, 21, 21);">"Medium"</span>, <span style="color: rgb(163, 21, 21);">"Large"</span>}</pre> <pre style="margin: 0px;"> };</pre> <pre style="margin: 0px;"> </pre> <pre style="margin: 0px;"><span style="color: blue;">string</span> json = </pre> <pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(</pre> <pre style="margin: 0px;"> product,</pre> <pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre> <pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { ContractResolver = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">CamelCasePropertyNamesContractResolver</span>() }</pre> <pre style="margin: 0px;"> );</pre> <pre style="margin: 0px;"> </pre> <pre style="margin: 0px;"><span style="color: green;">//{</span></pre> <pre style="margin: 0px;"><span style="color: green;">// "name": "Widget",</span></pre> <pre style="margin: 0px;"><span style="color: green;">// "expiryDate": "\/Date(1292868060000)\/",</span></pre> <pre style="margin: 0px;"><span style="color: green;">// "price": 9.99,</span></pre> <pre style="margin: 0px;"><span style="color: green;">// "sizes": [</span></pre> <pre style="margin: 0px;"><span style="color: green;">// "Small",</span></pre> <pre style="margin: 0px;"><span style="color: green;">// "Medium",</span></pre> <pre style="margin: 0px;"><span style="color: green;">// "Large"</span></pre> <pre style="margin: 0px;"><span style="color: green;">// ]</span></pre> <pre style="margin: 0px;"><span style="color: green;">//}</span></pre> </div> </div> </div> <div id="footer"> </div> </div> </body> </html>