forked from JamesNK/Newtonsoft.Json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReducingSerializedJSONSize.aml
132 lines (122 loc) · 7.75 KB
/
ReducingSerializedJSONSize.aml
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
<?xml version="1.0" encoding="utf-8"?>
<topic id="ReducingSerializedJSONSize" revisionNumber="1">
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
<!--
<summary>
<para>Optional summary abstract</para>
</summary>
-->
<introduction>
<!-- Uncomment this to generate an outline of the section and sub-section
titles. Specify a numeric value as the inner text to limit it to
a specific number of sub-topics when creating the outline. Specify
zero (0) to limit it to top-level sections only. -->
<!-- <autoOutline /> -->
<para>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 significant when returning JSON to the client. More JSON
means more bandwidth and a slower website.</para>
<para>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.</para>
</introduction>
<!-- Add one or more top-level section elements. These are collapsible.
If using <autoOutline />, add an address attribute to identify it
and specify a title so that it can be jumped to with a hyperlink. -->
<section>
<title>JsonIgnoreAttribute and DataMemberAttribute</title>
<content>
<!-- Uncomment this to create a sub-section outline
<autoOutline /> -->
<para>By default Json.NET will include all of a class's public properties and fields
in the JSON it creates. Adding the
<codeEntityReference>T:Newtonsoft.Json.JsonIgnoreAttribute</codeEntityReference>
to a property tells the serializer to always skip writing it to the JSON result.</para>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeOptOut" title="Opt-out Serialization Example" />
<para>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
<codeEntityReference>T:System.Runtime.Serialization.DataContractAttribute</codeEntityReference>
to the class and
<codeEntityReference>T:System.Runtime.Serialization.DataMemberAttribute</codeEntityReference>
to the properties to serialize. This is opt-in
serialization - only the properties you mark up will be serialized, unlike
opt-out serialization using JsonIgnoreAttribute.</para>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeOptIn" title="Opt-in Serialization Example" />
</content>
</section>
<section>
<title>Formatting</title>
<content>
<para>JSON written by the serializer with an option of
<codeEntityReference>T:Newtonsoft.Json.Formatting</codeEntityReference>
set to Indented produces
nicely formatted, easy-to-read JSON that is great for readability when you are
developing. <codeInline>Formatting.None</codeInline> 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.</para>
</content>
</section>
<section>
<title>NullValueHandling</title>
<content>
<para><codeEntityReference>T:Newtonsoft.Json.NullValueHandling</codeEntityReference>
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.</para>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeNullValueHandlingObject" title="NullValueHandling Class" />
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeNullValueHandlingExample" title="NullValueHandling Ignore Example" />
<para>NullValueHandling can also be customized on individual properties
using the
<codeEntityReference>T:Newtonsoft.Json.JsonPropertyAttribute</codeEntityReference>.
The JsonPropertyAttribute value of
NullValueHandling will override the setting on the JsonSerializer for that
property.</para>
</content>
</section>
<section>
<title>DefaultValueHandling</title>
<content>
<para><codeEntityReference>T:Newtonsoft.Json.DefaultValueHandling</codeEntityReference>
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 uninitialized
value for that value type.</para>
<para>Json.NET also allows you to customize what the default value of an individual
property is using the
<codeEntityReference>T:System.ComponentModel.DefaultValueAttribute</codeEntityReference>.
For example, if a string property called
Department always returns an empty string in its default state and you don'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.</para>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeDefaultValueHandlingObject" title="DefaultValueHandling Class" />
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeDefaultValueHandlingExample" title="DefaultValueHandling Ignore Example" />
<para>DefaultValueHandling can also be customized on individual properties using
the <codeEntityReference>T:Newtonsoft.Json.JsonPropertyAttribute</codeEntityReference>.
The JsonPropertyAttribute value of DefaultValueHandling
will override the setting on the JsonSerializer for that property.</para>
</content>
</section>
<section>
<title>IContractResolver</title>
<content>
<para>For more flexibility, the
<codeEntityReference>T:Newtonsoft.Json.Serialization.IContractResolver</codeEntityReference>
provides an interface to customize
almost every aspect of how a .NET object gets serialized to JSON, including changing
serialization behavior at runtime.</para>
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeContractResolverObject" title="IContractResolver Class" />
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeContractResolverExample" title="IContractResolver Example" />
</content>
</section>
<relatedTopics>
<codeEntityReference>T:Newtonsoft.Json.Formatting</codeEntityReference>
<codeEntityReference>T:Newtonsoft.Json.JsonIgnoreAttribute</codeEntityReference>
<codeEntityReference>T:Newtonsoft.Json.DefaultValueHandling</codeEntityReference>
<codeEntityReference>T:Newtonsoft.Json.NullValueHandling</codeEntityReference>
</relatedTopics>
</developerConceptualDocument>
</topic>