Skip to content

Instantly share code, notes, and snippets.

@regisdiogo
Last active June 13, 2022 11:17
Show Gist options
  • Star 92 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save regisdiogo/27f62ef83a804668eb0d9d0f63989e3e to your computer and use it in GitHub Desktop.
Save regisdiogo/27f62ef83a804668eb0d9d0f63989e3e to your computer and use it in GitHub Desktop.
ASP.NET Core - Json serializer settings Enum as string and ignore null values
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
}
}
@ansxor
Copy link

ansxor commented Sep 19, 2020

You saved me! Thanks bro!

@azsdaja
Copy link

azsdaja commented Oct 1, 2020

Seems to not be working for enums in response.

@onionhammer
Copy link

Limitation here: There doesn't seem to be a way to simply allow strings as enums but still prefer integers. I want to still allow input JSON bodies to accept strings for enums but dont want to serialize results with strings

@graknol
Copy link

graknol commented Oct 14, 2020

You saved me! Thanks bro!

@tomaszFijalkowski
Copy link

You saved me! Thanks bro!

@ivantcc
Copy link

ivantcc commented Nov 5, 2020

You saved me! Thanks bro!

@erijonhson
Copy link

You saved me! Thanks bro!

@MartinAmsinck
Copy link

You saved me! Thanks bro!

@MilkyAomiki
Copy link

You saved me! Thanks bro!

@MIGMLG
Copy link

MIGMLG commented Nov 25, 2020

You saved me! Thanks bro!

@jack0fshad0ws
Copy link

2021.. still rocks! )

@OlegLviv
Copy link

OlegLviv commented Feb 8, 2021

YOU SAVED MEEE BRRRROOOOOO

@DanielGilbert
Copy link

You saved me! Thanks bro!

@CodyBatt
Copy link

Didn't work for me. I ended up adding a schema filter and then...

You saved me! Thanks bro!

@DevPhak
Copy link

DevPhak commented Feb 27, 2021

You saved me! many thanks bro.

@GregDaure
Copy link

GregDaure commented Mar 4, 2021

You saved me! Thanks bro!

@muphblu
Copy link

muphblu commented Mar 19, 2021

You saved me! Thanks bro!

@SmRiley
Copy link

SmRiley commented Mar 22, 2021

Maybe this would help somebody. The same code for .net core 3.0 will look like this:

services.AddMvc()
    .AddJsonOptions(options => {
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
        options.JsonSerializerOptions.IgnoreNullValues = true;
    });

You saved me! Thanks bro!

@pferreirafabricio
Copy link

You saved me! Thanks bro!

@jonasby
Copy link

jonasby commented Apr 21, 2021

You saved me! Thanks bro!

@iwillrod
Copy link

Nice!

using System.Text.Json.Serialization;
...

services
    .AddControllers()
    .AddNewtonsoftJson()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
        options.JsonSerializerOptions.IgnoreNullValues = true;
    });

Working in .NET core 5.0

Make sure to include AddNewtonsoftJson() if you're using JSON PATCH. Otherwise it wont work.

@elenalash
Copy link

Nice!

using System.Text.Json.Serialization;
...

services
    .AddControllers()
    .AddNewtonsoftJson()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
        options.JsonSerializerOptions.IgnoreNullValues = true;
    });

Working in .NET core 5.0

Make sure to include AddNewtonsoftJson() if you're using JSON PATCH. Otherwise it wont work.

thank you!

@mjgoeke
Copy link

mjgoeke commented Aug 12, 2021

You saved me! Thanks bro!

@kerumirembora
Copy link

Saving bros since 2018.

You saved me for the 3rd time! Thanks bro!

@Zenuka
Copy link

Zenuka commented Oct 11, 2021

You saved me! Thanks bro!

@jurilents
Copy link

You saved me! Thanks bro!

@martikyan
Copy link

You saved me! Thanks bro! 🤣

@guimar86
Copy link

You saved me! Thanks bro!

@darkness-reveur
Copy link

You saved me! Thanks bro!

@edcsu
Copy link

edcsu commented Jun 13, 2022

For dotnet 6 a few options were deprecated

        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.WriteIndented = true;

            // serialize enums as strings in api responses (e.g. Role)
            options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());

            // ignore omitted parameters on models to enable optional params (e.g. User update)
            options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
        });```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment