Json.NET has the ability to conditionally serialize properties by placing a ShouldSerialize method on a class. This funtionality is similar to the XmlSerializer ShouldSerialize feature http://msdn.microsoft.com/en-us/library/53b8022e.aspx _blank .

# ShouldSerialize

To conditionally serialize a property add a boolean method with the same name as the property and then prefixed the method name with ShouldSerialize. The result of the method determines whether the property is serialized. If the method returns true then the property will be serialized, if it returns false and the property will be skipped.

Employee class with a ShouldSerialize method

ShouldSerialize output

# IContractResolver

ShouldSerialize can also be set using an T:Newtonsoft.Json.Serialization.IContractResolver. Conditionally serializing a property using an IContractResolver is useful if you don't want to place a ShouldSerialize method on a class or you didn't declare the class and are unable to.

Conditional properties with IContractResolver

T:Newtonsoft.Json.JsonSerializer
T:Newtonsoft.Json.Serialization.IContractResolver
P:Newtonsoft.Json.Serialization.JsonProperty.ShouldSerialize