Click or drag to resize
Json.NETDefaultValueHandling setting
 

This sample uses the DefaultValueHandling setting to not serialize properties with a default value.

Sample
Types
1public class Person
2{
3    public string Name { get; set; }
4    public int Age { get; set; }
5    public Person Partner { get; set; }
6    public decimal? Salary { get; set; }
7}
Usage
 1Person person = new Person();
 2
 3string jsonIncludeDefaultValues = JsonConvert.SerializeObject(person, Formatting.Indented);
 4
 5Console.WriteLine(jsonIncludeDefaultValues);
 6// {
 7//   "Name": null,
 8//   "Age": 0,
 9//   "Partner": null,
10//   "Salary": null
11// }
12
13string jsonIgnoreDefaultValues = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
14{
15    DefaultValueHandling = DefaultValueHandling.Ignore
16});
17
18Console.WriteLine(jsonIgnoreDefaultValues);
19// {}