1

Hey all how do i correctly update my json SerializerSettings in my netcoreapp 3.1 application? Im trying to update this setting in the startup.cs I've tried to set this two ways, by using AddController() and also AddMvc()

        builder.Services
            .AddControllers()
            .AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; })
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            });

        builder.Services
            .AddMvc()
            .AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; })
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            });

I don't think it's being applied correctly as I've tried to create unit tests by adding some null values to my JSON file however null reference exceptions are still being thrown, I tried to create a var json = new JsonSerializer() so i could view the serializer settings and i could still see the NullValueHandling = Included.

Adam
  • 97
  • 1
  • 6
  • 1
    You're asking multiple questions here, but generally you should have [one question per post](https://meta.stackoverflow.com/q/275908). Note that `AddNewtonsoftJson()` only configures the `JsonSerializerSettings` **used by ASP.NET Core**. See [this answer](https://stackoverflow.com/a/58392090) to [How to set json serializer settings in asp.net core 3?](https://stackoverflow.com/q/58392039) and also [JsonConvert.DefaultSettings](https://www.newtonsoft.com/json/help/html/p_newtonsoft_json_jsonconvert_defaultsettings.htm). – dbc May 17 '22 at 10:04
  • apologies ill edit the question – Adam May 17 '22 at 10:40
  • Can you share your sample code? – Yiyi You Jun 07 '22 at 06:23

0 Answers0