1
<?xml version="1.0" encoding="utf-8"?>
2
<topic id="ReducingSerializedJSONSize" revisionNumber="1">
3
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
6
<para>Optional summary abstract</para>
10
<!-- Uncomment this to generate an outline of the section and sub-section
11
titles. Specify a numeric value as the inner text to limit it to
12
a specific number of sub-topics when creating the outline. Specify
13
zero (0) to limit it to top-level sections only. -->
14
<!-- <autoOutline /> -->
15
<para>One of the common problems encountered when serializing .NET objects to
16
JSON is that the JSON ends up containing a lot of unwanted properties and values.
17
This can be especially important when returning JSON to the client. More JSON
18
means more bandwidth and a slower website.</para>
19
<para>To solve the issue of unwanted JSON Json.NET has a range of built in
20
options to fine tune what gets written from a serialized object.</para>
22
<!-- Add one or more top-level section elements. These are collapsible.
23
If using <autoOutline />, add an address attribute to identify it
24
and specify a title so that it can be jumped to with a hyperlink. -->
26
<title>JsonIgnoreAttribute and DataMemberAttribute</title>
28
<!-- Uncomment this to create a sub-section outline
30
<para>By default Json.NET will include all of a classes public properties and fields
31
in the JSON it creates. Adding the
32
<codeEntityReference>T:Newtonsoft.Json.JsonIgnoreAttribute</codeEntityReference>
33
to a property tells the serializer to always skip writing it to the JSON result.</para>
35
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeOptOut" title="Opt-out Serialization Example" />
37
<para>If a class has many properties and you only want to serialize a small subset
38
of them then adding JsonIgnore to all the others will be tedious and error prone.
39
The way to tackle this scenario is to add the
40
<codeEntityReference>T:System.Runtime.Serialization.DataContractAttribute</codeEntityReference>
42
<codeEntityReference>T:System.Runtime.Serialization.DataMemberAttribute</codeEntityReference>
43
to the properties to serialize. This is opt-in
44
serialization, only the properties you mark up with be serialized, compared to
45
opt-out serialization using JsonIgnoreAttribute.</para>
47
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeOptIn" title="Opt-in Serialization Example" />
51
<title>Formatting</title>
53
<para>JSON written by the serializer with an option of
54
<codeEntityReference>T:Newtonsoft.Json.Formatting</codeEntityReference>
55
set to Indented produces
56
nicely formatted, easy to read JSON that is great for readability when you are
57
developing. Formatting.None on the other hand keeps the JSON result small, skipping
58
all unnecessary spaces and line breaks to produce the most compact and efficient
63
<title>NullValueHandling</title>
65
<para><codeEntityReference>T:Newtonsoft.Json.NullValueHandling</codeEntityReference>
66
is an option on the JsonSerializer and controls how the
67
serializer handles properties with a null value. By setting a value of
68
NullValueHandling.Ignore the JsonSerializer skips writing any properties that have
69
a value of null.</para>
71
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeNullValueHandlingObject" title="NullValueHandling Class" />
72
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeNullValueHandlingExample" title="NullValueHandling Ignore Example" />
74
<para>NullValueHandling can also be customized on individual properties
76
<codeEntityReference>T:Newtonsoft.Json.JsonPropertyAttribute</codeEntityReference>.
77
The JsonPropertyAttribute value of
78
NullValueHandling will override the setting on the JsonSerializer for that
83
<title>DefaultValueHandling</title>
85
<para><codeEntityReference>T:Newtonsoft.Json.DefaultValueHandling</codeEntityReference>
86
is an option on the JsonSerializer and controls how the serializer handles
87
properties with a default value. Setting a value of DefaultValueHandling.Ignore
88
will make the JsonSerializer skip writing any properties that have a default
89
value to the JSON result. For object references this will be null. For value
90
types like int and DateTime the serializer will skip the default uninitialized
91
value for that value type.</para>
93
<para>Json.NET also allows you to customize what the default value of an individual
95
<codeEntityReference>T:System.ComponentModel.DefaultValueAttribute</codeEntityReference>.
96
For example if a string property called
97
Department always returns an empty string in its default state and you didn't want
98
that empty string in your JSON then placing the DefaultValueAttribute on Department
99
with that value will mean Department is no longer written to JSON unless it has a
102
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeDefaultValueHandlingObject" title="DefaultValueHandling Class" />
103
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeDefaultValueHandlingExample" title="DefaultValueHandling Ignore Example" />
105
<para>DefaultValueHandling can also be customized on individual properties using
107
<codeEntityReference>T:Newtonsoft.Json.JsonPropertyAttribute</codeEntityReference>.
108
The JsonPropertyAttribute value of DefaultValueHandling
109
will override the setting on the JsonSerializer for that property.</para>
113
<title>IContractResolver</title>
115
<para>For more flexibility the
116
<codeEntityReference>T:Newtonsoft.Json.Serialization.IContractResolver</codeEntityReference>
117
provides an interface to customize
118
almost every aspect of how a .NET object gets serialized to JSON, including changing
119
serialization behavior at runtime.</para>
121
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeContractResolverObject" title="IContractResolver Class" />
122
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeContractResolverExample" title="IContractResolver Example" />
127
<codeEntityReference>T:Newtonsoft.Json.Formatting</codeEntityReference>
128
<codeEntityReference>T:Newtonsoft.Json.JsonIgnoreAttribute</codeEntityReference>
129
<codeEntityReference>T:Newtonsoft.Json.DefaultValueHandling</codeEntityReference>
130
<codeEntityReference>T:Newtonsoft.Json.NullValueHandling</codeEntityReference>
132
</developerConceptualDocument>
b'\\ No newline at end of file'