When serializing a dictionary with JSON.NET, it seems like the NullValueHandling setting is ignored.
var dict = new Dictionary<string, string>
{
["A"] = "Some text",
["B"] = null
};
var json = JsonConvert.SerializeObject(dict, Formatting.Indented,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
Console.WriteLine(json);
Output:
{
"A": "Some text",
"B": null
}
I expected that only the KVP with key "A" is present in the json output and KVP "B" is omitted.
How can I tell Json.NET to only serialize the entries that do not contain null values?