Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
});
}
}
@zhongyi-zhang

This comment has been minimized.

Copy link

@zhongyi-zhang zhongyi-zhang commented Mar 27, 2018

You saved me! Thanks bro!

@hjkl950217

This comment has been minimized.

Copy link

@hjkl950217 hjkl950217 commented Apr 13, 2018

You saved me! Thanks bro!

@PhillipChaffee

This comment has been minimized.

Copy link

@PhillipChaffee PhillipChaffee commented Apr 27, 2018

You saved me! Thanks bro!

@dwolfhub

This comment has been minimized.

Copy link

@dwolfhub dwolfhub commented May 14, 2018

You saved me! Thanks bro!

@celluj34

This comment has been minimized.

Copy link

@celluj34 celluj34 commented May 22, 2018

You saved me! Thanks bro!

@JoshYaxley

This comment has been minimized.

Copy link

@JoshYaxley JoshYaxley commented May 25, 2018

You saved me! Thanks bro!

@qldmw

This comment has been minimized.

Copy link

@qldmw qldmw commented May 31, 2018

You saved me! Thanks bro!

@jopincar

This comment has been minimized.

Copy link

@jopincar jopincar commented Jun 12, 2018

You saved me! Thanks bro!

@VictorioBerra

This comment has been minimized.

Copy link

@VictorioBerra VictorioBerra commented Jun 20, 2018

You saved me! Thanks bro!

@fofosudarko

This comment has been minimized.

Copy link

@fofosudarko fofosudarko commented Jun 27, 2018

You saved me! Thanks bro!

@AnthonyJW

This comment has been minimized.

Copy link

@AnthonyJW AnthonyJW commented Jul 1, 2018

You saved me! Thanks bro!

@DjSloth

This comment has been minimized.

Copy link

@DjSloth DjSloth commented Jul 3, 2018

KING!!!!!!!!!!!!!!!!!!!!

@jcadiba

This comment has been minimized.

Copy link

@jcadiba jcadiba commented Jul 3, 2018

God bless you man

@takecover328

This comment has been minimized.

Copy link

@takecover328 takecover328 commented Aug 4, 2018

You are amazing!!!

@BinaryPatrick

This comment has been minimized.

Copy link

@BinaryPatrick BinaryPatrick commented Aug 20, 2018

Amazing!

@irowbin

This comment has been minimized.

Copy link

@irowbin irowbin commented Aug 30, 2018

wait.... 🤔 found 11 lazy commenters. 🙄 Copy pasting the comment? 😯 Come on guys!!!! 😅 🤣 😆

Also produce string values:

public enum MyEnum 
{
   [EnumMember( Value = "one" )]
   One,

   [EnumMember( Value = "two" )]
   Two,
}
@mike-boddin

This comment has been minimized.

Copy link

@mike-boddin mike-boddin commented Sep 10, 2018

The EnumMemberAttribute is not sufficient for producing string-values but it is an useful addition to control the actual value of the serialized property, e.g.:

public class Startup
{
  public IServiceProvider ConfigureServices(IServiceCollection services)
  {
    services.AddMvc().AddJsonOptions(options =>
    {
      // we need this
      options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
      options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
    });
  }
}

public enum MyState 
{
    // AND we need this
    [EnumMember( Value = "OPEN" )]
    Open,

    [EnumMember( Value = "CLOSED" )]
    Closed,

    [EnumMember( Value = "NOT_SURE" )]
    NotSure
}

public class MyClazz
{
    public MyState State { get; set; }
}

... serializing of MyClazz will then produce:

{
    "state": "OPEN"
}

...

EDIT:
I forgot to say:
You saved me! Thanks bro! :-)

@mkhatuntcov-korewireless

This comment has been minimized.

Copy link

@mkhatuntcov-korewireless mkhatuntcov-korewireless commented Sep 13, 2018

Bro! You saved me! Thanks! :-P

@fr8tra1n

This comment has been minimized.

Copy link

@fr8tra1n fr8tra1n commented Sep 20, 2018

You saved me! Thanks bro!

@daanl

This comment has been minimized.

Copy link

@daanl daanl commented Oct 4, 2018

thanks bro

@rickythefox

This comment has been minimized.

Copy link

@rickythefox rickythefox commented Oct 4, 2018

Awesome, thanks!

@dougrday

This comment has been minimized.

Copy link

@dougrday dougrday commented Dec 18, 2018

+1 for the all the brotherly love here.

@whizkidwwe1217

This comment has been minimized.

Copy link

@whizkidwwe1217 whizkidwwe1217 commented Jan 23, 2019

+100 You saved me too! Thanks bro. Love ya!

@kevingermain

This comment has been minimized.

Copy link

@kevingermain kevingermain commented Jan 30, 2019

You saved me! Thanks bro!

@JackMorrissey

This comment has been minimized.

Copy link

@JackMorrissey JackMorrissey commented Feb 18, 2019

You saved me! Thanks bro!

@bjartekh

This comment has been minimized.

Copy link

@bjartekh bjartekh commented Feb 21, 2019

You saved me! Thanks bro!

@danielpaulasbanken

This comment has been minimized.

Copy link

@danielpaulasbanken danielpaulasbanken commented Feb 21, 2019

You saved me! Thanks bro!

@AndreRochaDev

This comment has been minimized.

Copy link

@AndreRochaDev AndreRochaDev commented Feb 21, 2019

You saved me! Thanks bro!

@kerumirembora

This comment has been minimized.

Copy link

@kerumirembora kerumirembora commented Feb 21, 2019

After 20 saved people you kind of qualify as the enumerator Jesus Christ.

@pcontra

This comment has been minimized.

Copy link

@pcontra pcontra commented Feb 25, 2019

You saved me! Thanks bro!

@sgarshol

This comment has been minimized.

Copy link

@sgarshol sgarshol commented Feb 26, 2019

You saved me! Thanks bro!

@morrisond91

This comment has been minimized.

Copy link

@morrisond91 morrisond91 commented Mar 1, 2019

You saved me! Thanks bro!

@hhundiwala

This comment has been minimized.

Copy link

@hhundiwala hhundiwala commented Mar 6, 2019

You saved me! Thanks bro!

@NeilCross

This comment has been minimized.

Copy link

@NeilCross NeilCross commented Mar 15, 2019

You saved me! Thanks bro!

@kcootedinh

This comment has been minimized.

Copy link

@kcootedinh kcootedinh commented Mar 15, 2019

You saved me! Thanks bro!

@LMKTech

This comment has been minimized.

Copy link

@LMKTech LMKTech commented Mar 21, 2019

You saved me! Thanks bro!

@Mithgroth

This comment has been minimized.

Copy link

@Mithgroth Mithgroth commented Mar 24, 2019

You saved me! Thanks bro!

@stephengeorgewest-navitaire

This comment has been minimized.

Copy link

@stephengeorgewest-navitaire stephengeorgewest-navitaire commented Mar 25, 2019

You saved me! Thanks bro!

@AlcantaraRW

This comment has been minimized.

Copy link

@AlcantaraRW AlcantaraRW commented Apr 1, 2019

You saved me! Thanks bro!
And thanks @mike-boddin!

@busbina

This comment has been minimized.

Copy link

@busbina busbina commented Apr 1, 2019

Thanks!

@mikemikhaylov

This comment has been minimized.

Copy link

@mikemikhaylov mikemikhaylov commented Apr 12, 2019

Thanks bro!

@dima117

This comment has been minimized.

Copy link

@dima117 dima117 commented Apr 13, 2019

You saved me! Thanks bro!

@dillinzser

This comment has been minimized.

Copy link

@dillinzser dillinzser commented Apr 24, 2019

You saved me! Thanks bro!

@vmandic

This comment has been minimized.

Copy link

@vmandic vmandic commented Apr 24, 2019

You saved me! Thanks bro! (this absolutely the best comments list on a gist ever)

@oshimish

This comment has been minimized.

Copy link

@oshimish oshimish commented May 3, 2019

You saved me! Thanks bro!

@AndrewDessin

This comment has been minimized.

Copy link

@AndrewDessin AndrewDessin commented May 3, 2019

You saved me! Thanks bro!

@arogers1

This comment has been minimized.

Copy link

@arogers1 arogers1 commented May 3, 2019

You saved me! Thanks bro!

@rodrigolima7

This comment has been minimized.

Copy link

@rodrigolima7 rodrigolima7 commented May 14, 2019

You saved me! Thanks bro!

@inex0rable

This comment has been minimized.

Copy link

@inex0rable inex0rable commented May 16, 2019

Saving souls since 2017!

You saved me! Thanks bro!

@Micteu

This comment has been minimized.

Copy link

@Micteu Micteu commented May 17, 2019

You saved me! Thanks bro!

@davidrevoledo

This comment has been minimized.

Copy link

@davidrevoledo davidrevoledo commented May 24, 2019

Thanks.

@meareindel

This comment has been minimized.

Copy link

@meareindel meareindel commented May 28, 2019

You saved me! Thanks bro!

@krullgor

This comment has been minimized.

Copy link

@krullgor krullgor commented Jun 3, 2019

You saved me! Thanks bro!

@regisdiogo

This comment has been minimized.

Copy link
Owner Author

@regisdiogo regisdiogo commented Jun 3, 2019

You saved me! Thanks bro!

@marqustd

This comment has been minimized.

Copy link

@marqustd marqustd commented Jun 11, 2019

You saved me! Thanks bro!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jun 12, 2019

You saved me! Thanks bro!

@mvock

This comment has been minimized.

Copy link

@mvock mvock commented Jun 19, 2019

You saved me! Thanks bro!

@cconcise

This comment has been minimized.

Copy link

@cconcise cconcise commented Jun 19, 2019

You saved me! Thanks bro!

@rynarce

This comment has been minimized.

Copy link

@rynarce rynarce commented Jul 25, 2019

You saved me! Thanks bro!

@mattfrear

This comment has been minimized.

Copy link

@mattfrear mattfrear commented Jul 28, 2019

You saved me! Thanks bro!

@sayfulloev

This comment has been minimized.

Copy link

@sayfulloev sayfulloev commented Aug 7, 2019

You saved me! Thanks bro!

@memetolsen

This comment has been minimized.

Copy link

@memetolsen memetolsen commented Aug 8, 2019

You saved me! Thanks bro!

@Euler-KB

This comment has been minimized.

Copy link

@Euler-KB Euler-KB commented Aug 12, 2019

You saved me! Thanks bro!

@hallco978

This comment has been minimized.

Copy link

@hallco978 hallco978 commented Aug 14, 2019

You saved me! Thanks bro!

@stailer

This comment has been minimized.

Copy link

@stailer stailer commented Aug 24, 2019

You saved me! Thanks bro!

@federico-paolillo

This comment has been minimized.

Copy link

@federico-paolillo federico-paolillo commented Aug 29, 2019

You saved me! Thanks bro!

@lukaszciastko

This comment has been minimized.

Copy link

@lukaszciastko lukaszciastko commented Aug 31, 2019

Any idea how to use this for .Net Core 3.0?

@lyndon-rocks

This comment has been minimized.

Copy link

@lyndon-rocks lyndon-rocks commented Sep 4, 2019

@lukaszciastko Try something like this to ignore nulls:
services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.IgnoreNullValues = true);

@xemicalas

This comment has been minimized.

Copy link

@xemicalas xemicalas commented Sep 6, 2019

You saved me! Thanks bro!

@spencerkittleson

This comment has been minimized.

Copy link

@spencerkittleson spencerkittleson commented Sep 18, 2019

You saved me! Thanks bro!

@FrenchGrandpa

This comment has been minimized.

Copy link

@FrenchGrandpa FrenchGrandpa commented Sep 22, 2019

You saved me! Thanks bro!

@wolffish44

This comment has been minimized.

Copy link

@wolffish44 wolffish44 commented Sep 22, 2019

You saved me! Thanks bro!

@vasua

This comment has been minimized.

Copy link

@vasua vasua commented Sep 24, 2019

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;
    });
@remiskaune

This comment has been minimized.

Copy link

@remiskaune remiskaune commented Sep 27, 2019

Thanks its helped after update to .net core 3.0

@golflima

This comment has been minimized.

Copy link

@golflima golflima commented Sep 27, 2019

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;
    });

Just add: using System.Text.Json.Serialization;. Unfortunately, it seems Swagger (Swashbuckle.AspNetCore 5.0.0-rc3) is not ready yet to handle this properly as in the generated doc the enum types are marked as int even if they are properly serialized as strings.

You saved me! Thanks bro! ;)

@paillave

This comment has been minimized.

Copy link

@paillave paillave commented Oct 3, 2019

thanks @vasua

@aramean

This comment has been minimized.

Copy link

@aramean aramean commented Oct 14, 2019

@vasua. Thank you bro!

@Brandon2255p

This comment has been minimized.

Copy link

@Brandon2255p Brandon2255p commented Oct 16, 2019

You saved me! Thanks bro!

@fean

This comment has been minimized.

Copy link

@fean fean commented Oct 18, 2019

You saved me! Thanks bro!

@RetsamSoft

This comment has been minimized.

Copy link

@RetsamSoft RetsamSoft commented Oct 21, 2019

Sorry for my bad English, for only one property I use

    public class SomeClass{
        public enum _Status{ Active= 1, Inactive= -1 }
        //[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
        [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
        public _Status Status{ get; set; }    
    }
@ilaborde

This comment has been minimized.

Copy link

@ilaborde ilaborde commented Oct 22, 2019

In my case I've added the camelCase strategy

options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter(new CamelCaseNamingStrategy()));

@rue-vmom

This comment has been minimized.

Copy link

@rue-vmom rue-vmom commented Oct 24, 2019

You saved me! Thanks bro!

@erikbarke

This comment has been minimized.

Copy link

@erikbarke erikbarke commented Oct 27, 2019

@vasua, thanks!!

@simplerjiang

This comment has been minimized.

Copy link

@simplerjiang simplerjiang commented Nov 5, 2019

amazing you save me

@philipblaquiere

This comment has been minimized.

Copy link

@philipblaquiere philipblaquiere commented Nov 6, 2019

For ASP.NET Core 3

services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; options.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter()); });

@kerumirembora

This comment has been minimized.

Copy link

@kerumirembora kerumirembora commented Nov 27, 2019

It's the second time I came here to be saved, and it saved me again.

You saved me twice! Thanks bro!

@p-rc-andrade

This comment has been minimized.

Copy link

@p-rc-andrade p-rc-andrade commented Nov 28, 2019

You saved me! Thanks bro!

@star-thoughts

This comment has been minimized.

Copy link

@star-thoughts star-thoughts commented Nov 29, 2019

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;
    });

Just add: using System.Text.Json.Serialization;. Unfortunately, it seems Swagger (Swashbuckle.AspNetCore 5.0.0-rc3) is not ready yet to handle this properly as in the generated doc the enum types are marked as int even if they are properly serialized as strings.

You saved me! Thanks bro! ;)

In AddSwaggerGen you can call DescribeAllEnumsAsStrings as the following to get Swagger to use enums. Note that this method is deprecated so you'll have to pragma around it like:

services.AddSwaggerGen(c =>
{
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
#pragma warning disable 618
                c.DescribeAllEnumsAsStrings();
#pragma warning restore 618
}
@MauricioLudwig

This comment has been minimized.

Copy link

@MauricioLudwig MauricioLudwig commented Dec 3, 2019

You saved me! Thanks bro!

@speccaN

This comment has been minimized.

Copy link

@speccaN speccaN commented Dec 3, 2019

You saved me! Thanks bro!

@tsardaryan

This comment has been minimized.

Copy link

@tsardaryan tsardaryan commented Dec 10, 2019

You saved me! Thanks bro!

@yhvicey

This comment has been minimized.

Copy link

@yhvicey yhvicey commented Dec 14, 2019

You saved me! Thanks bro!

@Lordinaire

This comment has been minimized.

Copy link

@Lordinaire Lordinaire commented Dec 19, 2019

You saved me! Thanks bro!

@bernatgy

This comment has been minimized.

Copy link

@bernatgy bernatgy commented Dec 28, 2019

You saved me! Thanks bro!

@pvictorlv

This comment has been minimized.

Copy link

@pvictorlv pvictorlv commented Dec 29, 2019

For swagger with Json.Net:

                Services.AddMvc().AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.Converters.Add(new StringEnumConverter());
                })
@zimmermanw84

This comment has been minimized.

Copy link

@zimmermanw84 zimmermanw84 commented Dec 30, 2019

You saved me! Thanks bro!

@marblekirby

This comment has been minimized.

Copy link

@marblekirby marblekirby commented Jan 11, 2020

You saved me! Thanks bro!

@jacamera

This comment has been minimized.

Copy link

@jacamera jacamera commented Jan 15, 2020

You saved me! Thanks bro!

Seriously though: What could possibly be the rationale for this not being the default behavior for application/json binding when it is the default for application/x-www-form-urlencoded binding?

Edit to answer myself: Maybe because there's one JSON policy for both serialization and deserialization. If you add the StringEnumConverter to the global converter list then enums will be serialized to their string representation instead of their numerical value.

If you only want to allow string conversion during deserialization you can just apply the following attribute to a specific model property:

[JsonConverter(typeof(JsonStringEnumConverter))]
@gaetanvdberge

This comment has been minimized.

Copy link

@gaetanvdberge gaetanvdberge commented Jan 15, 2020

Thanks! Just what I needed!

@danger250

This comment has been minimized.

Copy link

@danger250 danger250 commented Jan 21, 2020

You saved me! Thanks bro!

@romanimm

This comment has been minimized.

Copy link

@romanimm romanimm commented Jan 21, 2020

You saved me! Thanks bro!

@TheYves

This comment has been minimized.

Copy link

@TheYves TheYves commented Jan 21, 2020

You saved me! Thanks bro!

@victorvhpg

This comment has been minimized.

Copy link

@victorvhpg victorvhpg commented Jan 27, 2020

You saved me! Thanks bro!

@wouterattoptal

This comment has been minimized.

Copy link

@wouterattoptal wouterattoptal commented Feb 6, 2020

You saved me! Thanks bro!

@stuarthallows

This comment has been minimized.

Copy link

@stuarthallows stuarthallows commented Feb 13, 2020

You saved me! Thanks bro!

@virtes

This comment has been minimized.

Copy link

@virtes virtes commented Feb 25, 2020

You saved me! Thanks bro!

@merschformann

This comment has been minimized.

Copy link

@merschformann merschformann commented Feb 25, 2020

You saved me! Thanks bro!

@dairaE

This comment has been minimized.

Copy link

@dairaE dairaE commented Mar 3, 2020

You saved me! Thanks bro!

@MarkEngelsman

This comment has been minimized.

Copy link

@MarkEngelsman MarkEngelsman commented Mar 5, 2020

You saved me! Thanks bro!

@harlitad

This comment has been minimized.

Copy link

@harlitad harlitad commented Mar 13, 2020

This is really cool. You saved me! thank you so much!

@veuncent

This comment has been minimized.

Copy link

@veuncent veuncent commented Mar 31, 2020

You saved me! Thanks bro!

@JavidShoaei

This comment has been minimized.

Copy link

@JavidShoaei JavidShoaei commented Apr 2, 2020

You saved me! Thanks bro!

@DarioDomiDE

This comment has been minimized.

Copy link

@DarioDomiDE DarioDomiDE commented Apr 23, 2020

You saved me! Thanks bro!

@conficient

This comment has been minimized.

Copy link

@conficient conficient commented Apr 28, 2020

Many thanks to all the contributors-- just ran into this little landmine in .NET Core 3.1 while converting an app.

Also note you can turn off the camelCasing for serialized webapi output using

options.JsonSerializerOptions.PropertyNamingPolicy = null;

This also caused conversion issues for me!

@masleshov

This comment has been minimized.

Copy link

@masleshov masleshov commented May 7, 2020

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!

@erik-neumann

This comment has been minimized.

Copy link

@erik-neumann erik-neumann commented May 8, 2020

You saved me! Thanks bro!

@kivix221

This comment has been minimized.

Copy link

@kivix221 kivix221 commented May 13, 2020

Thanks bro!

@mbj-netip

This comment has been minimized.

Copy link

@mbj-netip mbj-netip commented May 20, 2020

Thanks! :)

@olakusibe

This comment has been minimized.

Copy link

@olakusibe olakusibe commented Jun 2, 2020

Thanks!!!

@bolton2

This comment has been minimized.

Copy link

@bolton2 bolton2 commented Jun 6, 2020

you saved me! Thanx bro!

@fevaldez

This comment has been minimized.

Copy link

@fevaldez fevaldez commented Jun 7, 2020

Thanks a lot, another one being saved here with net.core 3.1

@Andersoft

This comment has been minimized.

Copy link

@Andersoft Andersoft commented Jun 7, 2020

You saved me! Thanks bro!

@csj

This comment has been minimized.

Copy link

@csj csj commented Jun 7, 2020

I have been rescued by your valiant contribution. Fraternal appreciations!

@jaganathanb

This comment has been minimized.

Copy link

@jaganathanb jaganathanb commented Jun 19, 2020

For ASP.NET Core 3

services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; options.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter()); });

This helped with .Net Core 3.

@kumaheiyama

This comment has been minimized.

Copy link

@kumaheiyama kumaheiyama commented Jun 20, 2020

You saved me! Thanks bro!

@dungimon

This comment has been minimized.

Copy link

@dungimon dungimon commented Jun 24, 2020

You saved me! Thanks bro!

@MARF-TECH

This comment has been minimized.

Copy link

@MARF-TECH MARF-TECH commented Jun 26, 2020

bro! saved me ! Thanks! You!

@isurupera

This comment has been minimized.

Copy link

@isurupera isurupera commented Jul 2, 2020

For ASP.NET Core 3
services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; options.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter()); });

This helped with .Net Core 3.

can it use for specific Enum? this is work properly, it rerun all Enum string values, but i need some of Enum as int

@huserben

This comment has been minimized.

Copy link

@huserben huserben commented Jul 11, 2020

You saved me! Thanks bro!

@thomasio101

This comment has been minimized.

Copy link

@thomasio101 thomasio101 commented Jul 15, 2020

For ASP.NET Core 3
services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; options.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter()); });

This helped with .Net Core 3.

can it use for specific Enum? this is work properly, it rerun all Enum string values, but i need some of Enum as int

I have the same question as @isurupera.

@nwtnwrkshp

This comment has been minimized.

Copy link

@nwtnwrkshp nwtnwrkshp commented Jul 20, 2020

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!

for .NET Core 3.0 web api:

services.AddControllers().AddJsonOptions(o =>
      {
        o.JsonSerializerOptions.IgnoreNullValues = true;
        o.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
      });
@omair-sajid-confiz

This comment has been minimized.

Copy link

@omair-sajid-confiz omair-sajid-confiz commented Jul 22, 2020

You all saved me! Thanks all bros!

@pazcaldj

This comment has been minimized.

Copy link

@pazcaldj pazcaldj commented Jul 29, 2020

You saved me! Thanks bro!

@mjnorman

This comment has been minimized.

Copy link

@mjnorman mjnorman commented Aug 4, 2020

You know why I'm here

@byteshadow

This comment has been minimized.

Copy link

@byteshadow byteshadow commented Aug 20, 2020

You saved me! Thanks bro!

@abzarak

This comment has been minimized.

Copy link

@abzarak abzarak commented Aug 23, 2020

You saved me! Thanks bro!

@ruifrvaz

This comment has been minimized.

Copy link

@ruifrvaz ruifrvaz commented Aug 26, 2020

i'd love to keep the chain alive, but this doesnt seem to work for openapi 3.0. in the document the schema is an enum of type integer.

any way of getting this to work as an enum of type string?

update

i was using newtonsoftjson as converter, which does not work. use AddJsonOptions instead.

you saved me bro.

@NinovanderMark

This comment has been minimized.

Copy link

@NinovanderMark NinovanderMark commented Sep 2, 2020

You saved me! Thanks bro!

@MercedeX

This comment has been minimized.

Copy link

@MercedeX MercedeX commented Sep 7, 2020

You saved me! Thanks bro!

@ansxor

This comment has been minimized.

Copy link

@ansxor ansxor commented Sep 19, 2020

You saved me! Thanks bro!

@azsdaja

This comment has been minimized.

Copy link

@azsdaja azsdaja commented Oct 1, 2020

Seems to not be working for enums in response.

@onionhammer

This comment has been minimized.

Copy link

@onionhammer onionhammer commented Oct 8, 2020

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

This comment has been minimized.

Copy link

@graknol graknol commented Oct 14, 2020

You saved me! Thanks bro!

@tomaszFijalkowski

This comment has been minimized.

Copy link

@tomaszFijalkowski tomaszFijalkowski commented Oct 31, 2020

You saved me! Thanks bro!

@ivantcc

This comment has been minimized.

Copy link

@ivantcc ivantcc commented Nov 5, 2020

You saved me! Thanks bro!

@erijonhson

This comment has been minimized.

Copy link

@erijonhson erijonhson commented Nov 6, 2020

You saved me! Thanks bro!

@MartinAmsinck

This comment has been minimized.

Copy link

@MartinAmsinck MartinAmsinck commented Nov 8, 2020

You saved me! Thanks bro!

@MilkyAomiki

This comment has been minimized.

Copy link

@MilkyAomiki MilkyAomiki commented Nov 17, 2020

You saved me! Thanks bro!

@MIGMLG

This comment has been minimized.

Copy link

@MIGMLG MIGMLG commented Nov 25, 2020

You saved me! Thanks bro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.