NullValueHandling.Ignore doesn't ignore nulls
See original GitHub issueSource/destination types
System.Collections.Generic.Dictionary<string, object>
Source/destination JSON
{"exists":1,"doesnt":null}
Expected behavior
That the null is not included.
Actual behavior
The null is included.
Steps to reproduce
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Sample
{
public class Program
{
public static void Main(string[] args)
{
var obj = new Dictionary<string, object>
{
["exists"] = 1,
["doesnt"] = null,
};
var json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
System.Console.WriteLine(json);
}
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
JSON.NET NullValueHandling in JsonPropertyAttribute not ...
Try passing new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore } as third parameter in JsonConvert.
Read more >Newtonsoft.Json NullValueHandling.Ignor doesn't ...
I have this class with one optional field which I want to hide from JSON when it's null, but it's there even with...
Read more >Newtonsoft Json.Net serialize JObject doesn't ignore nulls ...
The thing is that when adding the NullValueHandling property to NullValueHandling.Ignore , every null property gets ignored, but only if it's part of...
Read more >NullValueHandling setting
This sample serializes an object to JSON with NullValueHandling set to Ignore so that properties with a default value aren't included in the...
Read more >How to ignore properties with System.Text.Json
In this article, you learn how to ignore properties based on various criteria: Individual properties; All read-only properties; All null-value ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Same for me.
This example from documentation doesn’t work: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm
I tried it here: https://dotnetfiddle.net/5683os
Would be helpful to know when this feature stopped to work. I mean what the last version it worked
This setting is only for properties on objects. Not dictionaries.