How to get LINQ to JSON to ignore null content like LINQ to XML does?

Dec 21, 2011 at 3:45 PM
Edited Dec 24, 2011 at 7:13 AM

Consider this LINQ to XML code...

 

    var doc = new XDocument (decl,
                new XElement (ns + "service",
                  GetXmlLink (ApiResource.Service),
                  new XElement (ns + "eula",
                    new XElement (ns + "accepted", service.EulaAccepted.ToString ().ToLowerInvariant ()),
                    GetXmlLink (ApiResource.Eula)
                  ),
                  !service.EulaAccepted ? null :
                  new XElement (ns + "portfolios",
                    new XElement (ns + "total", service.PortfoliosCount.ToString (CultureInfo.InvariantCulture)),
                    GetXmlLink (ApiResource.Portfolios)
                  )
                )
              );

 

If service.EulaAccepted is false, then the null ensures that no further content is added to the "service" element.  XDoc's ignoring of null content is by design.

Working with 4.0.5.0 of JSON.NET (the latest NuGet package), the following code doesn't do the same thing:-

 

    var doc = new JObject (
                new JProperty ("service",
                  new JObject (
                    new JProperty ("eula",
                      new JObject (
                        new JProperty ("accepted", service.EulaAccepted.ToString ().ToLowerInvariant ())
                      )
                    )
                  ),
                  !service.EulaAccepted ? null :
                  new JObject (
                    new JProperty ("portfolios",
                      new JObject (
                        new JProperty ("total", service.PortfoliosCount.ToString (CultureInfo.InvariantCulture))
                      )
                    )
                  )
                )
              );

 

What the latter outputs is this:-

{
  "service": [
    {
      "eula": {
        "accepted": "false"
      }
    },
    null
  ]
}

i.e. it outputs "null" instead of ignoring it.  [IGNORE: Have I done something wrong?  Is there a way to get what I want?  Would JSON.NET have to be changed to ignore null content, as LINQ to XML does?]

Thanks.

Andrew

Dec 24, 2011 at 7:24 AM

Ok, having thought about it for a couple of days, I've come to the conclusion that the output "null" is fine.  At first I wanted my JSON output to be as similar as possible to my XML output, but now I see that what LINQ to JSON has output is actually the best thing.  "null" is a valid JSON value, it indicates that something that's normally there is missing, and it means that the "service" item is always an array, rather than changing from being an array or an object.  The dynamic nature of JavaScript means that the code that processes the "service" item could easily cope with it being either an array or an object, but it's less work and more consistent to have the item always be an array.  So it's all good!

Like another recent poster to this discussion board, I'm also using JSON.NET in a WCF Web API service.  My media type formatters output resource representations in both XML and JSON formats.  The XML code uses LINQ to XML, and the JSON code uses LINQ to JSON... affording a pleasing symmetry and consistency.  I picked up JSON.NET in the last hour of work this year (via NuGet), and got immediately pleasing results.  Now I've resolved this null issue, LINQ to JSON is just perfect... thank you!  Keep up the good work.  And I've just made a donation to the cause via PayPal.

Merry Christmas!

Coordinator
Dec 24, 2011 at 10:49 PM

Thanks. I sent you a thank you for your donation but I was on my phone and your spam filter defeated me. Glad you're finding Json.NET useful.