Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 1.59 KB

DefaultValueAttributeIgnore.md

File metadata and controls

42 lines (34 loc) · 1.59 KB

DefaultValueAttribute

This sample uses the System.ComponentModel.DefaultValueAttribute to override the default value for a property and exclude it from serialization using Argon.DefaultValueHandling.

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [DefaultValue(" ")] public string FullName => $"{FirstName} {LastName}";
}

snippet source | anchor

var customer = new Customer();

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

Console.WriteLine(jsonIncludeDefaultValues);
// {
//   "FirstName": null,
//   "LastName": null,
//   "FullName": " "
// }

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

Console.WriteLine(jsonIgnoreDefaultValues);
// {}

snippet source | anchor