In most versions of ASP.NET Web API the default JSON serializer is JSON.NET.
Tou can combine two things to achieve what you need:
- Specify the default value handling in the Web API serializer configuration, like this:
config.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore
- Use the DefaultValueAttribute to specify which is the default that shouldn't be serialized, if not the "usual default": null for objects and nullables, 0 for numbers, and so on.
For your specific case of enum, you can define what is the default value in your enum, even if it's not 0.
For example, if you want to skip the serialization of ExpiryPeriod property in a class, when its value is 30, apart from setting the DefaultValueHandling.Ignore, pdecorate the property like this
public class Voucher
{
[DefaultValue(30)]
public int ExpiryPeriod { get; set; }
}
NOTE: if you want to use JSON.NET to deserialize to the default value, you must use the DefaultValueHandling.IgnoreAndPopulate. This means that, if the property value is not found when deserializing the JSON text, the property will be created with the specified default value of 30.
Full example with Enum values:
using System;
using System.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class Program
{
public static void Main()
{
var tshirts = new TShirt[]
{
new TShirt { Model = "Hawaii 2", Size = Size.M },
new TShirt { Model = "La casa de papel", Size = Size.XL },
};
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate;
settings.Converters.Add(new StringEnumConverter());
foreach(var tshirt in tshirts)
{
Console.WriteLine("Original T-Shirt");
Console.WriteLine(tshirt);
Console.WriteLine();
var serialized = JsonConvert.SerializeObject(tshirt, Formatting.Indented, settings);
Console.WriteLine("Serialized T-Shirt");
Console.WriteLine(serialized);
Console.WriteLine();
Console.WriteLine("Deserialized T-Shirt");
var deserialized = JsonConvert.DeserializeObject<TShirt>(serialized, settings);
Console.WriteLine(deserialized);
Console.WriteLine();
Console.WriteLine();
}
}
public enum Size
{
XL,
L,
M,
X,
XS
}
public class TShirt
{
public string Model { get; set; }
[DefaultValue(Size.M)]
public Size Size { get; set; }
public override string ToString()
{
return $"· Model: {Model}\r\n· Size: {Size}";
}
}
}
which outputs:
Original T-Shirt
· Model: Hawaii 2
· Size: M
Serialized T-Shirt
{
"Model": "Hawaii 2"
}
Deserialized T-Shirt
· Model: Hawaii 2
· Size: M
Original T-Shirt
· Model: La casa de papel
· Size: XL
Serialized T-Shirt
{
"Model": "La casa de papel",
"Size": "XL"
}
Deserialized T-Shirt
· Model: La casa de papel
· Size: XL
As you can see, even if Seize.M is not the default (0) value of the enum, it's skipped from the serialization and used to deserialize the property if it's missing in the json.
Nullable<>) and explicitly set the value to null instead of using 0.DefaultValueHandling = DefaultValueHandling.Ignore. If that doesn't work, please edit your question and include a minimal reproducible example that demonstrates the problem.