<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 <ahref="./html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm">JsonIgnoreAttribute</a> to a property tells the serializer
to always skip writing it to the JSON result. </p>
<p>JSON written by the serializer with an option of <ahref="./html/T_Newtonsoft_Json_Formatting.htm">Formatting.Indented</a> produces nicely formatted, easy to read JSON – great when you are developing. <ahref="./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><ahref="./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>
<p>NullValueHandling can also be customized on individual properties using the a <ahref="./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><ahref="./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 <ahref="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>
<p>DefaultValueHandling can also be customized on individual properties using the a <ahref="./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 <ahref="./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>
<prestyle="margin: 0px;"> <spanstyle="color: green;">// only serializer properties that start with the specified character</span></pre>
<namespaceSummaryItem name="Newtonsoft.Json" isDocumented="True">The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework.</namespaceSummaryItem>
<namespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True">The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>.</namespaceSummaryItem>
<namespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON.</namespaceSummaryItem>
<namespaceSummaryItem name="Newtonsoft.Json.Linq.ComponentModel" isDocumented="True">The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding.</namespaceSummaryItem>
<namespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True">The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema.</namespaceSummaryItem>
<namespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON.</namespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json" isDocumented="True" xmlns="">The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Linq.ComponentModel" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON.</NamespaceSummaryItem>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.