linux-packaging-mono/external/Newtonsoft.Json/Doc/ReducingSerializedJSONSize.html
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

280 lines
26 KiB
HTML
Raw Blame History

<html>
<head>
<title>Reducing Serialized JSON Size</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">Reducing Serialized JSON Size</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>One of the common problems encountered when serializing .NET objects to JSON is that the JSON ends up containing a lot of unwanted properties and values. This can be especially important when returning JSON to the client. More JSON means more bandwidth and a slower website.</p>
<p>To solve the issue of unwanted JSON Json.NET has a range of built in options to fine tune what gets written from a serialized object.</p>
<h3>JsonIgnoreAttribute and DataMemberAttribute </h3>
<p>By default Json.NET will include all of a classes public properties and fields in
the JSON it creates. Adding the <a href="./html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm">JsonIgnoreAttribute</a> to a property tells the serializer
to always skip writing it to the JSON result. </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: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Car</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// included in JSON</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Model { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span> Year { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">string</span>&gt; Features { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// ignored</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">JsonIgnore</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span> LastModified { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<p>If a class has many properties and you only want to serialize a small subset of
them then adding JsonIgnore to all the others will be tedious and error prone.
The way to tackle this scenario is to add the
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx"
target="_blank">DataContractAttribute</a> to the class and
DataMemberAttributes to the properties to serialize. This is opt-in
serialization, only the properties you mark up with be serialized, compared to
opt-out serialization using JsonIgnoreAttribute.</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);">DataContract</span>]</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Computer</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// included in JSON</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">DataMember</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">DataMember</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> SalePrice { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// ignored</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Manufacture { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> StockCount { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> WholeSalePrice { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span> NextShipmentDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<h3>Formatting </h3>
<p>JSON written by the serializer with an option of <a href="./html/T_Newtonsoft_Json_Formatting.htm">Formatting.Indented</a> produces nicely formatted, easy to read JSON <20> great when you are developing. <a href="./html/T_Newtonsoft_Json_Formatting.htm">Formatting.None</a> on the other hand keeps the JSON result small, skipping all unnecessary spaces and line breaks to produce the most compact and efficient JSON possible. </p>
<p><strong>NullValueHandling </strong></p>
<p><a href="./html/T_Newtonsoft_Json_NullValueHandling.htm">NullValueHandling</a> is an option on the JsonSerializer and controls how the serializer handles properties with a null value. By setting a value of NullValueHandling.Ignore the JsonSerializer skips writing any properties that have a value of null.</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: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Movie</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Description { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Classification { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Studio { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span>? ReleaseDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">string</span>&gt; ReleaseCountries { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<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);">Movie</span> movie = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Movie</span>();</pre>
<pre style="margin: 0px;">movie.Name = <span style="color: rgb(163, 21, 21);">"Bad Boys III"</span>;</pre>
<pre style="margin: 0px;">movie.Description = <span style="color: rgb(163, 21, 21);">"It's no Bad Boys"</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> included = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(movie,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { });</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Name": "Bad Boys III",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Description": "It's no Bad Boys",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Classification": null,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Studio": null,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "ReleaseDate": null,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "ReleaseCountries": null</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> ignored = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(movie,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { NullValueHandling = <span style="color: rgb(43, 145, 175);">NullValueHandling</span>.Ignore });</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Name": "Bad Boys III",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Description": "It's no Bad Boys"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
</div>
</div>
</div>
<p>NullValueHandling can also be customized on individual properties using the a <a href="./html/T_Newtonsoft_Json_JsonPropertyAttribute.htm">JsonPropertyAttribute</a>. The JsonPropertyAttribute value of NullValueHandling will override the setting on the JsonSerializer for that property. </p>
<h3>DefaultValueHandling </h3>
<p><a href="./html/T_Newtonsoft_Json_NullValueHandling.htm">DefaultValueHandling</a> is an option on the JsonSerializer and controls how the serializer handles properties with a default value. Setting a value of DefaultValueHandling.Ignore will make the JsonSerializer skip writing any properties that have a default value to the JSON result. For object references this will be null. For value types like int and DateTime the serializer will skip the default unitialized value for that value type. </p>
<p>Json.NET also allows you to customize what the default value of an individual property is using the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx" target="_blank">DefaultValueAttribute</a>. For example if a string property called Department always returns an empty string in its default state and you didn't want that empty string in your JSON then placing the DefaultValueAttribute on Department with that value will mean Department is no longer written to JSON unless it has a value. </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: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Invoice</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Company { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> Amount { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// false is default value of bool</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> Paid { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// null is default value of nullable</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span>? PaidDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// customize default values</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">DefaultValue</span>(30)]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> FollowUpDays { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">DefaultValue</span>(<span style="color: rgb(163, 21, 21);">""</span>)]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> FollowUpEmailAddress { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<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);">Invoice</span> invoice = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Invoice</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; Company = <span style="color: rgb(163, 21, 21);">"Acme Ltd."</span>,</pre>
<pre style="margin: 0px;">&nbsp; Amount = 50.0m,</pre>
<pre style="margin: 0px;">&nbsp; Paid = <span style="color: blue;">false</span>,</pre>
<pre style="margin: 0px;">&nbsp; FollowUpDays = 30,</pre>
<pre style="margin: 0px;">&nbsp; FollowUpEmailAddress = <span style="color: blue;">string</span>.Empty,</pre>
<pre style="margin: 0px;">&nbsp; PaidDate = <span style="color: blue;">null</span></pre>
<pre style="margin: 0px;">};</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> included = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(invoice,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { });</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Company": "Acme Ltd.",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Amount": 50.0,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Paid": false,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "PaidDate": null,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "FollowUpDays": 30,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "FollowUpEmailAddress": ""</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> ignored = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(invoice,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { DefaultValueHandling = <span style="color: rgb(43, 145, 175);">DefaultValueHandling</span>.Ignore });</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Company": "Acme Ltd.",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Amount": 50.0</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
</div>
</div>
</div>
<p>DefaultValueHandling can also be customized on individual properties using the a <a href="./html/T_Newtonsoft_Json_JsonPropertyAttribute.htm">JsonPropertyAttribute</a>. The JsonPropertyAttribute value of DefaultValueHandling will override the setting on the JsonSerializer for that property.</p>
<h3>IContractResolver</h3>
<p>For more flexibility the <a href="./html/T_Newtonsoft_Json_Serialization_IContractResolver.htm">IContractResolver</a> provides an interface to customize almost every aspect of how a .NET object gets serialized to JSON, including changing serialization behavior at runtime.</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: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">DynamicContractResolver</span> : <span style="color: rgb(43, 145, 175);">DefaultContractResolver</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: blue;">char</span> _startingWithChar;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> DynamicContractResolver(<span style="color: blue;">char</span> startingWithChar)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; _startingWithChar = startingWithChar;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: rgb(43, 145, 175);">IList</span>&lt;<span style="color: rgb(43, 145, 175);">JsonProperty</span>&gt; CreateProperties(<span style="color: rgb(43, 145, 175);">JsonObjectContract</span> contract)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">IList</span>&lt;<span style="color: rgb(43, 145, 175);">JsonProperty</span>&gt; properties = <span style="color: blue;">base</span>.CreateProperties(contract);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">// only serializer properties that start with the specified character</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; properties = </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; properties.Where(p =&gt; p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> properties;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Book</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> BookName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> BookPrice { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> AuthorName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> AuthorAge { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> AuthorCountry { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<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);">Book</span> book = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Book</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BookName = <span style="color: rgb(163, 21, 21);">"The Gathering Storm"</span>,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BookPrice = 16.19m,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthorName = <span style="color: rgb(163, 21, 21);">"Brandon Sanderson"</span>,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthorAge = 34,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthorCountry = <span style="color: rgb(163, 21, 21);">"United States of America"</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> startingWithA = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(book, <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <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);">DynamicContractResolver</span>(<span style="color: rgb(163, 21, 21);">'A'</span>) });</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "AuthorName": "Brandon Sanderson",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "AuthorAge": 34,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "AuthorCountry": "United States of America"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> startingWithB = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(book, <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <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);">DynamicContractResolver</span>(<span style="color: rgb(163, 21, 21);">'B'</span>) });</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "BookName": "The Gathering Storm",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "BookPrice": 16.19</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>