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}";
}
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);
// {}