Repository navigation
Description
Background and Motivation
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:
- I cannot think of deserialization usages that make sense, that's why I proposed WhenWritingNull.
- Options.IgnoreNullValues is not a solution, since it recently became obsolete.
Proposed API
WhenWritingNull is the new enum entry.
public enum JsonIgnoreCondition
{
/// <summary>
/// Property is never ignored during serialization or deserialization.
/// </summary>
Never = 0,
/// <summary>
/// Property is always ignored during serialization and deserialization.
/// </summary>
Always = 1,
/// <summary>
/// If the value is the default, the property is ignored during serialization.
/// </summary>
WhenWritingDefault = 2,
/// <summary>
/// If the value is null, the property is ignored during serialization.
/// </summary>
WhenWritingNull = 3,
}While working on a regression for #38800 I wrote a PR that fixed both the regression and proposed the change, see here: #38811.
That should have been separate PRs, so the PR got rejected, but you can see a baseline there.
Usage Examples
In global options:
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:
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);
}
Activity
molinch commentedon Jul 12, 2020
For reference #38800
layomia commentedon Jul 14, 2020
From @rubo in #779:
layomia commentedon Jul 14, 2020
From @loic-sharma in #779:
terrajobst commentedon Jul 14, 2020
Video
more on Upgrade_20201110_DotNet5