29
votes

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?

6
There is a thread on the corefx github suggesting this get implemented but it looks like it's an all or nothing thing at the moment using JsonSerializerOptions.IgnoreNullValuesSimply Ged
System.Text.Json is meant for simple scenarios for now. It's main focus is speed and low allocations. You may have to use custom formatters or use JSON.NET fro more advanced scenariosPanagiotis Kanavos
@PanagiotisKanavos Are you talking about JsonConverter? I have been looking for an example of how to do this but I can't find any ...Miguel Moura
@SimplyGed Any idea how to accomplish this for now using System.Text.Json even if extra code is needed?Miguel Moura

6 Answers

8
votes

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

18
votes

I'm looking at .Net Core 3.1, where this should ignore null values

    services.AddControllers().AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.IgnoreNullValues = true;
    });

Note, the above isn't per property/attribute, although there is an attribute which may be helpful JsonIgnoreAttribute

An alternative, to solve your problem might be a JsonConverterAttribute, information on how to write your own converter is here

11
votes

If you want property level control of ignoring null values during JSON serialization, for Net Core 3.1 you'll have to write a custom converter. There are examples described in the Newtonsoft.Json migration documentation.

That is a big hassle for functionality that was declarative using Newtonsoft.Json. You can specify to use Newtonsoft.Json by specifying as much in Startup.ConfigureServices().

services.AddControllers()
    .AddNewtonsoftJson();

As the documentation notes, you'll need to add the Microsoft.AspNetCore.Mvc.NewtonsoftJson package.

10
votes

Adding this to your startup should help although it's not per property and it's not an attribute.

services.AddMvc()
        .AddJsonOptions(options =>{ options.JsonSerializerOptions.IgnoreNullValues = true; });
6
votes

If you are still using Newtonsoft.Json in .net core 3.1, you want to have configuration like below.

services
   .AddControllers()
   .AddJsonOptions(options =>
   {
       options.JsonSerializerOptions.IgnoreNullValues = true;
   })
   .AddNewtonsoftJson(options =>
   {
       options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
   });
-3
votes

See the official migration guide Migrate from ASP.NET Core 2.2 to 3.0

Your service codes should look like this:

services.AddMvc(c =>
{

})
.AddNewtonsoftJson(
    options =>
        {
            options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            options.SerializerSettings.StringEscapeHandling = StringEscapeHandling.EscapeHtml;                      

            options.SerializerSettings.Error = (object sender, ErrorEventArgs args) =>
            {
               // handle error
            };
        }
    );