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

commented Mar 27, 2018

You saved me! Thanks bro!

@hjkl950217

This comment has been minimized.

Copy link

commented Apr 13, 2018

You saved me! Thanks bro!

@PhillipChaffee

This comment has been minimized.

Copy link

commented Apr 27, 2018

You saved me! Thanks bro!

@dwolfhub

This comment has been minimized.

Copy link

commented May 14, 2018

You saved me! Thanks bro!

@celluj34

This comment has been minimized.

Copy link

commented May 22, 2018

You saved me! Thanks bro!

@JoshYaxley

This comment has been minimized.

Copy link

commented May 25, 2018

You saved me! Thanks bro!

@qldmw

This comment has been minimized.

Copy link

commented May 31, 2018

You saved me! Thanks bro!

@jopincar

This comment has been minimized.

Copy link

commented Jun 12, 2018

You saved me! Thanks bro!

@VictorioBerra

This comment has been minimized.

Copy link

commented Jun 20, 2018

You saved me! Thanks bro!

@fofosudarko

This comment has been minimized.

Copy link

commented Jun 27, 2018

You saved me! Thanks bro!

@AnthonyJW

This comment has been minimized.

Copy link

commented Jul 1, 2018

You saved me! Thanks bro!

@DjSloth

This comment has been minimized.

Copy link

commented Jul 3, 2018

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

@jcadiba

This comment has been minimized.

Copy link

commented Jul 3, 2018

God bless you man ♥♥♥

@takecover328

This comment has been minimized.

Copy link

commented Aug 4, 2018

You are amazing!!!

@BinaryPatrick

This comment has been minimized.

Copy link

commented Aug 20, 2018

Amazing!

@irowbin

This comment has been minimized.

Copy link

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

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

commented Sep 13, 2018

Bro! You saved me! Thanks! :-P

@fr8tra1n

This comment has been minimized.

Copy link

commented Sep 20, 2018

You saved me! Thanks bro!

@daanl

This comment has been minimized.

Copy link

commented Oct 4, 2018

thanks bro

@rickythefox

This comment has been minimized.

Copy link

commented Oct 4, 2018

Awesome, thanks!

@dougrday

This comment has been minimized.

Copy link

commented Dec 18, 2018

+1 for the all the brotherly love here.

@whizkidwwe1217

This comment has been minimized.

Copy link

commented Jan 23, 2019

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

@kevingermain

This comment has been minimized.

Copy link

commented Jan 30, 2019

You saved me! Thanks bro!

@JackMorrissey

This comment has been minimized.

Copy link

commented Feb 18, 2019

You saved me! Thanks bro!

@bjartekh

This comment has been minimized.

Copy link

commented Feb 21, 2019

You saved me! Thanks bro!

@danielpaulasbanken

This comment has been minimized.

Copy link

commented Feb 21, 2019

You saved me! Thanks bro!

@AndreRochaDev

This comment has been minimized.

Copy link

commented Feb 21, 2019

You saved me! Thanks bro!

@kerumirembora

This comment has been minimized.

Copy link

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

commented Feb 25, 2019

You saved me! Thanks bro!

@sgarshol

This comment has been minimized.

Copy link

commented Feb 26, 2019

You saved me! Thanks bro!

@morrisond91

This comment has been minimized.

Copy link

commented Mar 1, 2019

You saved me! Thanks bro!

@hhundiwala

This comment has been minimized.

Copy link

commented Mar 6, 2019

You saved me! Thanks bro!

@NeilCross

This comment has been minimized.

Copy link

commented Mar 15, 2019

You saved me! Thanks bro!

@kcootedinh

This comment has been minimized.

Copy link

commented Mar 15, 2019

You saved me! Thanks bro!

@LMKTech

This comment has been minimized.

Copy link

commented Mar 21, 2019

You saved me! Thanks bro!

@Mithgroth

This comment has been minimized.

Copy link

commented Mar 24, 2019

You saved me! Thanks bro!

@stephengeorgewest-navitaire

This comment has been minimized.

Copy link

commented Mar 25, 2019

You saved me! Thanks bro!

@AlcantaraRW

This comment has been minimized.

Copy link

commented Apr 1, 2019

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

@busbina

This comment has been minimized.

Copy link

commented Apr 1, 2019

Thanks!

@mikhail-mikhaylov

This comment has been minimized.

Copy link

commented Apr 12, 2019

Thanks bro!

@dima117

This comment has been minimized.

Copy link

commented Apr 13, 2019

You saved me! Thanks bro!

@dillinzser

This comment has been minimized.

Copy link

commented Apr 24, 2019

You saved me! Thanks bro!

@vmandic

This comment has been minimized.

Copy link

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

commented May 3, 2019

You saved me! Thanks bro!

@AndrewDessin

This comment has been minimized.

Copy link

commented May 3, 2019

You saved me! Thanks bro!

@arogers1

This comment has been minimized.

Copy link

commented May 3, 2019

You saved me! Thanks bro!

@rodrigolima7

This comment has been minimized.

Copy link

commented May 14, 2019

You saved me! Thanks bro!

@inex0rable

This comment has been minimized.

Copy link

commented May 16, 2019

Saving souls since 2017!

You saved me! Thanks bro!

@Micteu

This comment has been minimized.

Copy link

commented May 17, 2019

You saved me! Thanks bro!

@davidrevoledo

This comment has been minimized.

Copy link

commented May 24, 2019

Thanks.

@meareindel

This comment has been minimized.

Copy link

commented May 28, 2019

You saved me! Thanks bro!

@krullgor

This comment has been minimized.

Copy link

commented Jun 3, 2019

You saved me! Thanks bro!

@regisdiogo

This comment has been minimized.

Copy link
Owner Author

commented Jun 3, 2019

You saved me! Thanks bro!

@marqustd

This comment has been minimized.

Copy link

commented Jun 11, 2019

You saved me! Thanks bro!

@Maikeaerosmith

This comment has been minimized.

Copy link

commented Jun 12, 2019

You saved me! Thanks bro!

@mvock

This comment has been minimized.

Copy link

commented Jun 19, 2019

You saved me! Thanks bro!

@cconcise

This comment has been minimized.

Copy link

commented Jun 19, 2019

You saved me! Thanks bro!

@rynarce

This comment has been minimized.

Copy link

commented Jul 25, 2019

You saved me! Thanks bro!

@mattfrear

This comment has been minimized.

Copy link

commented Jul 28, 2019

You saved me! Thanks bro!

@sayfulloev

This comment has been minimized.

Copy link

commented Aug 7, 2019

You saved me! Thanks bro!

@memetolsen

This comment has been minimized.

Copy link

commented Aug 8, 2019

You saved me! Thanks bro!

@Euler-KB

This comment has been minimized.

Copy link

commented Aug 12, 2019

You saved me! Thanks bro!

@hallco978

This comment has been minimized.

Copy link

commented Aug 14, 2019

You saved me! Thanks bro!

@stailer

This comment has been minimized.

Copy link

commented Aug 24, 2019

You saved me! Thanks bro!

@federico-paolillo

This comment has been minimized.

Copy link

commented Aug 29, 2019

You saved me! Thanks bro!

@lukaszciastko

This comment has been minimized.

Copy link

commented Aug 31, 2019

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

@lyndon-rocks

This comment has been minimized.

Copy link

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

commented Sep 6, 2019

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.