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.

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.

# JsonIgnoreAttribute and DataMemberAttribute

By default Json.NET will include all of a classes public properties and fields in the JSON it creates. Adding the T:Newtonsoft.Json.JsonIgnoreAttribute to a property tells the serializer to always skip writing it to the JSON result.

Opt-out Serialization Example

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 T:System.Runtime.Serialization.DataContractAttribute to the class and T:System.Runtime.Serialization.DataMemberAttribute 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.

Opt-in Serialization Example

# Formatting

JSON written by the serializer with an option of T:Newtonsoft.Json.Formatting set to Indented produces nicely formatted, easy to read JSON that is great for readability when you are developing. Formatting.None 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.

# NullValueHandling

T:Newtonsoft.Json.NullValueHandling 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.

NullValueHandling Class

NullValueHandling Ignore Example

NullValueHandling can also be customized on individual properties using the a T:Newtonsoft.Json.JsonPropertyAttribute. The JsonPropertyAttribute value of NullValueHandling will override the setting on the JsonSerializer for that property.

# DefaultValueHandling

T:Newtonsoft.Json.DefaultValueHandling 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.

Json.NET also allows you to customize what the default value of an individual property is using the T:System.ComponentModel.DefaultValueAttribute. 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.

DefaultValueHandling Class

DefaultValueHandling Ignore Example

DefaultValueHandling can also be customized on individual properties using the a T:Newtonsoft.Json.JsonPropertyAttribute. The JsonPropertyAttribute value of DefaultValueHandling will override the setting on the JsonSerializer for that property.

# IContractResolver

For more flexibility the T:Newtonsoft.Json.Serialization.IContractResolver provides an interface to customize almost every aspect of how a .NET object gets serialized to JSON, including changing serialization behavior at runtime.

IContractResolver Class

IContractResolver Example

T:Newtonsoft.Json.Formatting
T:Newtonsoft.Json.JsonIgnoreAttribute
T:Newtonsoft.Json.DefaultValueHandling
T:Newtonsoft.Json.NullValueHandling