Last active
July 14, 2021 14:51
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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