Joseph Anderson
Joseph Anderson

Reputation: 4144

Newtonsoft.JSON Serialization Array, Object, or Null

I have some JSON that can be a List or null. How do I create a POCO for this JSON?

Here is an example array: http://pastebin.com/qAZF2Ug9

Here is my POCO: http://pastebin.com/hUtgyytc

How can I tell Newtonsoft.JSON to ignore the SalesLine object, if it is null?

Upvotes: 0

Views: 1119

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063358

You can specify the settings:

var settings = new Newtonsoft.Json.JsonSerializerSettings {
    NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore};

and use that in various serializer constructors and serialize calls.

Alternatively, IIRC it supports conditional serialization, i.e.

public bool ShouldSerializeFoo() { return Foo != null; }
// pairs to property Foo

Upvotes: 1

Max Brodin
Max Brodin

Reputation: 3938

Try to mark this property with JsonProperty attribute

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public SaleLines SaleLines { get; set; }

Upvotes: 1

Related Questions