Recently JsonIgnoreCondition have been changed to have WhenWritingDefault rather than WhenNull.
See:

When serving JSON for frontends, then default values of value types are extremely important. It's not like null.
You could argue that undefined and null have "similar" semantic in Javascript, so not having the null field serialized makes sense. It will be undefined, and checks you would do against null such as if (!value) will match the undefined behavior too.
While for value types you don't expect that it can become undefined, so you'll end up with subtle bugs after deserialization.
That's why I believe that WhenWritingNull makes a lot of sense.
JSON.NET has a similar feature, see: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm
Remarks:
WhenWritingNull is the new enum entry.
```c#
public enum JsonIgnoreCondition
{
///
/// Property is never ignored during serialization or deserialization.
///
Never = 0,
///
/// Property is always ignored during serialization and deserialization.
///
Always = 1,
///
/// If the value is the default, the property is ignored during serialization.
///
WhenWritingDefault = 2,
///
/// If the value is null, the property is ignored during serialization.
///
WhenWritingNull = 3,
}
While working on a regression for https://github.com/dotnet/runtime/issues/38800 I wrote a PR that fixed both the regression and proposed the change, see here: https://github.com/dotnet/runtime/pull/38811.
That should have been separate PRs, so the PR got rejected, but you can see a baseline there.
## Usage Examples
In global options:
```c#
public class MyClass
{
public int[] ReferenceType { get; set; }
public int ValueType { get; set; }
}
[Fact]
public static void Serialize_value_type_but_not_reference_type()
{
var options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenNull
};
string json = @"{""ReferenceType"":null, ""ValueType"":18}";
var obj = JsonSerializer.Deserialize<MyClass>(json, options);
// Deserialize
Assert.Null(obj.ReferenceType);
Assert.Equal(18, obj.ValueType);
// Deserialize
obj = new MyClass();
json = JsonSerializer.Serialize(obj, options);
Assert.Equal(@"{""ValueType"":0}", json);
}
On top of properties:
```c#
public class MyClass
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int[] ReferenceType { get; set; }
public int ValueType { get; set; }
}
[Fact]
public static void Serialize_value_type_but_not_reference_type()
{
string json = @"{""ReferenceType"":null, ""ValueType"":18}";
var obj = JsonSerializer.Deserialize<MyClass>(json);
// Deserialize
Assert.Null(obj.ReferenceType);
Assert.Equal(18, obj.ValueType);
// Deserialize
obj = new MyClass();
json = JsonSerializer.Serialize(obj);
Assert.Equal(@"{""ValueType"":0}", json);
}
```
For reference https://github.com/dotnet/runtime/issues/38800
From @rubo in https://github.com/dotnet/runtime/issues/779:
Currently, with
WhenWritingDefault, it doesn't serialize, for instance,inttype with the default value. There's should be an option to skip nullable types but still serialize non-nullable types. I don't need to receive anything in JSON that isnullbut I do want to receive fields with values of0. In JavaScript apps, having somethingundefinedor0makes a big difference. In a nutshell,WhenWritingDefaultmakesintfields of0to becomeundefinedin JavaScript client. For now, I useint?instead ofintas a workaround which is not a good solution.
From @loic-sharma in https://github.com/dotnet/runtime/issues/779:
I ran into the same issue as @rubo when updating my app to .NET 5. The current
JsonIgnoreCondition.WhenWritingDefaultis a breaking change that feels a little too strong. Would it be possible to add aJsonIgnoreCondition.WhenNullthat skipsnullvalues but writes defaults like0andfalse? This comment indicates thatJsonIgnoreCondition.WhenNullwas removed - why is that?For context, my app implements the NuGet protocol. This protocol requires some integer and boolean properties even if they have default values (for example, the search API must return a
downloadsvalue even if a package has 0 downloads). However, this protocol also has optional properties that should be omitted if they have a value ofnull(for example, the package metadata API should omit thedeprecationproperty if a package is not deprecated). AddingJsonIgnoreAttributes everywhere as needed is rather inconvenient.
C#
public enum JsonIgnoreCondition
{
// Never = 0,
// Always = 1,
// WhenWritingDefault = 2,
WhenWritingNull = 3
}
Most helpful comment
Video
C# public enum JsonIgnoreCondition { // Never = 0, // Always = 1, // WhenWritingDefault = 2, WhenWritingNull = 3 }