Skip to content

Instantly share code, notes, and snippets.

@rgueldenpfennig
Last active July 14, 2021 14:51
Show Gist options
  • Select an option

  • Save rgueldenpfennig/995243acf612482815bc283b71545ae8 to your computer and use it in GitHub Desktop.

Select an option

Save rgueldenpfennig/995243acf612482815bc283b71545ae8 to your computer and use it in GitHub Desktop.
Configure JSON serializer settings to convert Enums to strings and ignorre null values in an ASP.NET Core application
public void ConfigureServices(IServiceCollection services)
{
// when using Newtonsoft JSON
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.Converters.Add(new StringEnumConverter());
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
// when using System.Text.Json.Serialization;
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment