Open
Description
Consider the following console application:
Console.WriteLine(JsonSerializer.Serialize(new MyPoco()));
public class MyPoco
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public JsonElement Foo { get; set; } = JsonDocument.Parse("null").RootElement;
}
Even though JsonElement
is capable of representing null instances, the serializer will reject this configuration with the message
System.InvalidOperationException: 'The ignore condition 'JsonIgnoreCondition.WhenWritingNull' is not valid on value-type member 'Foo' on type 'MyPoco'. Consider using 'JsonIgnoreCondition.WhenWritingDefault'.'
Even though WhenWritingDefault
works, default(JsonElement)
is an invalid value that is unfortunately different than the JsonElement
null
representation. This means that there currently is no mechanism for naturally serializing optional JsonElement
properties and you're forced to use JsonElement?
instead.