How to ignore a property in class if null, using json.net
As per James Newton King: If you create the serializer yourself rather than using JavaScriptConvert there is a NullValueHandling
property which you can set to ignore.
Here's a sample:
JsonSerializer _jsonWriter = new JsonSerializer {
NullValueHandling = NullValueHandling.Ignore
};
Alternatively, as suggested by @amit
JsonConvert.SerializeObject(myObject,
Newtonsoft.Json.Formatting.None,
new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore
});
Ignoring null fields in Json.net
Yes you need to use JsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore
.
But because structs are value types you need to mark Field2, Field3 nullable to get the expected result:
public struct structA
{
public string Field1;
public structB? Field2;
public structB? Field3;
}
Or just use classes instead of structs.
Documentation: NullValueHandling Enumeration
Ignore property when null using the new Net Core 3.0 Json
This was fixed on .Net 5
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
See the updates below
https://github.com/dotnet/runtime/issues/41313
https://github.com/dotnet/runtime/issues/30687
Json.NET Ignore null values in dictionary
I would just filter out the null
values from the original dictionary with LINQ and serialize the filtered dictionary:
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace JsonSerialize {
public static class Program {
private static Dictionary<string, string> dict = new Dictionary<string, string> {
["A"] = "Some text",
["B"] = null
};
public static void Main(string[] args) {
var filtered = dict
.Where(p => p.Value != null)
.ToDictionary(p => p.Key, p => p.Value);
var json = JsonConvert.SerializeObject(filtered, Formatting.Indented);
Console.WriteLine (json);
}
}
}
Which gives:
{
"A": "Some text"
}
Need to ignore NULL values when deserializing JSON
NullValueHandling.Ignore
will ignore null values for the relevant property of your model when serializing.
When deserialzing, you might consider deserializing to an IEnumerable<ChartData>
then using Linq to filter out the objects you don't want, based on what is, after all, custom logic: "exclude objects that have close == null".
E.g. (untested air code):
var data = JsonConvert.DeserializeObject<IEnumerable<ChartData>>(content,jsonSettings)
.Where(cd => cd.close != null)
;
var observableData = new ObservableCollection<ChartData>(data);
How to ignore null fields in json serialization (Newtonsoft.Json)?
I deleted the file and recreated it instead of overwriting it. Source object was exactly the same. I was probably not overwriting the file correctly.
Related Topics
Calling a Function from a String in C#
What Are the Differences Between Generics in C# and Java... and Templates in C++
What Are Some Good .Net Profilers
Passing Strings from C# to C++ Dll and Back - Minimal Example
How to Deserialize Json With C#
How to Call a C# Library from Native C++ (Using C++\Cli and Ijw)
Reach Control from Another Page. Asp.Net
How to Dynamically Generate HTML Code Using .Net'S Webbrowser or Mshtml.Htmldocument
Convert Webpage to Image from Asp.Net
Parsing CSV Files in C#, With Header
How to Get Started With Developing Internet Explorer Extensions
Is Is Possible to Export Functions from a C# Dll Like in VS C++