jsonjson.netasp.net-core-3.0.net-core-3.0system.text.json

Ignore property when null using the new Net Core 3.0 Json


When using JSON.Net in ASP.Net Core 2.2 I was able to ignore a property when its value was null when serializing to JSON:

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? Created { get; set; }

But when using the new ASP.Net Core 3.0 built in JSON (System.Text.Json) I can’t find an equivalent attribute to ignore a property if its value is null.

I could only find JsonIgnore.

Am I missing something?


Solution

  • This was fixed on .Net 5

    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    

    See the updates below

    https://github.com/dotnet/runtime/issues/41313

    https://github.com/dotnet/runtime/issues/30687