none
SerializerSettings.NullValueHandling is not working in .NET Core 3.0 RRS feed

  • Question

  • The below is not working in .Net Core 3.0

    The below is the code in startup.cs

    public void ConfigureServices(IServiceCollection services)
            {

    services.AddControllers().AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                });

    }

    If I post an order with null value in string, its not ignored in [FromBody]

    below is my swagger example:

    {
      "date": "2020-02-27T15:49:24.189Z",
      "temperatureC": 0,
      "summary": null
    }

    here Summary is null. When I post an order I expected the pyaload shouldn't have the Summary proerty in this.

    here is my Controller code:

    [HttpPost]
            public ActionResult Post([FromBody] WeatherForecast body)
            {
                var rng = new Random();

                var b = body.Date;

                return null;
            }

    When I click on Post and  debugged, summary field is present with value is null, instead of ignoring this string its sending null value. 

    

    • Moved by CoolDadTx Friday, February 28, 2020 2:49 PM ASP.NET related
    Thursday, February 27, 2020 4:01 PM

All replies

  •  

    Newtonsoft.Json.NullValueHandling.Ignore is not working since it is for serializing object instead of your  deserializing/model binding.Besides, a type must have all specifc fileds.

    It will be ignored when using like

    [HttpPost]
    public void PostData([FromBody] WeatherForecast body)
            {
                string ignored = JsonConvert.SerializeObject(body);//work like this
               
            }

    I do not know what are you expected to do that but it is not likely  to remove the null fields from a type on model binding. 

    Friday, February 28, 2020 3:09 AM
  • hello friend try the bellow code may help

    JsonConvert.SerializeObject(myObject, 
                                Newtonsoft.Json.Formatting.None, 
                                new JsonSerializerSettings { 
                                    NullValueHandling = NullValueHandling.Ignore
                                });
    	OR
    
    JsonSerializerSettings settings = new JsonSerializerSettings();
    settings.NullValueHandling = NullValueHandling.Ignore;
    var myJson = JsonConvert.SerializeObject(myObject, settings);
    
    OR
    		var settings = new JsonSerializerSettings();
    		
    		settings.NullValueHandling = NullValueHandling.Ignore;
    		settings.DefaultValueHandling = DefaultValueHandling.Ignore;
    		
    		
    		Console.WriteLine(JsonConvert.SerializeObject(user, settings));

    Friday, February 28, 2020 9:38 AM
  • Thank you but I dont want to do as part of Each controller method. Is there any way I can do as part of startup.cs.

    in 2_2 version it was working fine when we ignore the values in statup.cs, not sure why its not in core3.0


    • Edited by Krishna90 Friday, February 28, 2020 11:20 AM
    Friday, February 28, 2020 11:20 AM
  • Thank you. Looks like this will work if I write the code for each controller but I want to enforce this from startup.cs, which was working in .Net core 2.2 but not in 3.0
    Friday, February 28, 2020 11:22 AM
  • NewtonSoft is a third party library. We do not provide support for third party libraries in the C# forums as this forum is specifically for C# questions. Please post questions related to using NewtonSoft in your code in their forums.

    Questions related to ASP.NET should be posted in the ASP.NET forums.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, February 28, 2020 2:49 PM