Skip to content

Latest commit

 

History

History
50 lines (43 loc) · 1.63 KB

NullValueHandlingIgnore.md

File metadata and controls

50 lines (43 loc) · 1.63 KB

NullValueHandling setting

This sample serializes an object to JSON with Argon.NullValueHandling set to Ignore so that properties with a default value of null aren't included in the JSON result.

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
{
    Name = "Nigal Newborn",
    Age = 1
};

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

Console.WriteLine(jsonIncludeNullValues);
// {
//   "Name": "Nigal Newborn",
//   "Age": 1,
//   "Partner": null,
//   "Salary": null
// }

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

Console.WriteLine(jsonIgnoreNullValues);
// {
//   "Name": "Nigal Newborn",
//   "Age": 1
// }

snippet source | anchor