Json.NET 1.1 released

Thanks again to the people that have tried out Json.NET and have reported bugs or offered suggestions. This release addresses all the issues that you've reported and that I'm aware of. 1.1 also adds a few new features.

  • New feature - XmlNodeConverter class provides a means to easy convert between Xml and JSON text. SerializeXmlNode and DeserializeXmlNode helper methods have also been added.
  • New feature - Handles parsing JavaScript constructor calls (i.e. new Date(1234)).
  • New feature - Improved date serializing and deserializing.
  • Change - Rewritten JavaScript string escape method.
  • Bug fix - Lots of little fixes to JsonSerializer.

Converting between XML and JSON

The big new feature added in Json.NET 1.1 is a converter class for XML. By using XmlNodeConverter (or the newly added helper methods) it is now extremely simple to convert JSON to XML and vice versa in .NET.

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<?xml version=""1.0"" standalone=""no""?>
  <person id=""1"">
  <person id=""2"">
string jsonText = JavaScriptConvert.SerializeXmlNode(doc);
//  "?xml": {
//    "@version": "1.0",
//    "@standalone": "no"
//  },
//  "root": {
//    "person": [
//      {
//        "@id": "1",
//        "name": "Alan",
//        "url": "http://www.google.com"
//      },
//      {
//        "@id": "2",
//        "name": "Louis",
//        "url": "http://www.yahoo.com"
//      }
//    ]
//  }
XmlDocument newDoc = (XmlDocument)JavaScriptConvert.DeerializeXmlNode(jsonText);
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);

Elements, attributes, text, comments, character data, processing instructions, namespaces and the XML declaration are all preserved when converting between the two. The only caveat is that it is possible to lose the order of differently named nodes at the same level when they are grouped together into an array.

A Brief Guide

  • Elements remain unchanged.
  • Attributes are prefixed with an @.
  • Single child text nodes are a value directly against an element, otherwise they are accessed via #text.
  • The XML declaration and processing instructions are prefixed with ?.
  • Charater data, comments, whitespace and significate whitespace nodes are accessed via #cdata-section, #comment, #whitespace and #significate-whitespace respectively.
  • Multiple nodes with the same name at the same level are grouped together into an array.
  • Empty elements are null.

Json.NET 1.1 can be downloaded here.