Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# - Newtonsoft Json.Net serialize JObject doesn't ignore nulls, even with the right settings

I'm trying to serialize an object using Newtonsoft Json.Net.

This object is an anonymous type filled with a lot of heterogenous things, mainly regular POCOs, but also some JObjects or JArrays.

The thing is that when adding the NullValueHandling property to NullValueHandling.Ignore, every null property gets ignored, but only if it's part of a "regular" .Net object. Every null property inside a JObject or JArray remains.

Here's a minimal example:

var jobj = JObject.FromObject(new Anything{
    x = 1,
    y = "bla",
    z = null
});

var poco = new Foo {
   foo1 = "bar",
   foo2 = null
};

var serialized = JsonConvert.SerializeObject(new {
    source1 = poco,
    source2 = jobj
}, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

Is there a simple way to ignore those null values as well ? Did I miss some setting option ? Or do I have to deal with it manually ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A "null" value in a JObject is actually a non-null JValue with JValue.Type equal to JTokenType.Null. It represents a JSON value of null when such a value actually appears in the JSON. I believe it exists to capture the difference between the following two JSON objects:

  "source2": {
    "z": null
  }

  "source2": {
  }

In the first case, the property "z" is present with a null JSON value. In the second case, the property "z" is not present. Linq-to-JSON represents the first case with a null-type JValue rather than having JProperty.Value actually be null.

To prevent null tokens from creeping into your JObject's values, use the appropriate serializer setting when creating the JObject from some POCO:

var jobj = JObject.FromObject(new
{
    x = 1,
    y = "bla",
    z = (int?)null
}, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore } );

(Note the POCO must not itself already be a JObject. The untyped method(s) JsonConvert.DeserializeObject(jsonString) or JsonConvert.DeserializeObject<dynamic>(jsonString) will by default return a JObject when root JSON container in jsonString is a JSON object.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...