File: ReducingSerializedJSONSize.html

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (280 lines) | stat: -rw-r--r-- 26,766 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<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 � 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>