Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 1.51 KB

DefaultValueHandlingIgnore.md

File metadata and controls

43 lines (36 loc) · 1.51 KB

DefaultValueHandling setting

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

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person Partner { get; set; }
    public decimal? Salary { get; set; }
}

snippet source | anchor

var person = new Person();

var jsonIncludeDefaultValues = JsonConvert.SerializeObject(person, Formatting.Indented);

Console.WriteLine(jsonIncludeDefaultValues);
// {
//   "Name": null,
//   "Age": 0,
//   "Partner": null,
//   "Salary": null
// }

var jsonIgnoreDefaultValues = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
{
    DefaultValueHandling = DefaultValueHandling.Ignore
});

Console.WriteLine(jsonIgnoreDefaultValues);
// {}

snippet source | anchor