Tuesday, July 18th, 2006
Json.NET 1.1: Converting between XML and JSON
<p>James Newton-King has updated his Json.NET library to version 1.1.The major new feature is that you can now almost seamlessly convert between XML and JSON:
-
-
XmlDocument doc = new XmlDocument();
-
doc.LoadXml(@"<?xml version=""1.0"" standalone=""no""?>
-
<root>
-
<person id=""1"">
-
<name>Alan</name>
-
<url>http://www.google.com</url>
-
</person>
-
<person id=""2"">
-
<name>Louis</name>
-
<url>http://www.yahoo.com</url>
-
</person>
-
</root>");
-
-
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);
-
Related Content:











There is another XML JSON converter:
http://weblogs.asp.net/mschwarz/archive/2006/06/19/XML-_2D003E00_-JSON-Serialization.aspx
There is already another one here:
http://weblogs.asp.net/mschwarz/archive/2006/06/19/XML-_2D003E00_-JSON-Serialization.aspx
These are the rules I came up with when converting XML to JSON. You can see some in the example above.
-
* 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 ?.
* Character 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.
-
Unfortunately you lose some ordering information when multiple nodes of the same name are grouped together but apart from that the transition back and forth (including namespace data) is pretty good.
Interesting Finds: July 18, 2006
James’ conversion rules are pretty good, but there’s one edge case you need to stay aware of relating to the multiple-node-array thing. If you have a document which normally returns multiple nodes (say a set of tags) you might write code against the JSON conversion that expects an array – but this will break if only a single category is present in the XML as the convertor will not know to turn it in to an array. This can be quite a nasty gotcha if you aren’t expecting it.
Yes that could be a problem. The same issue also applies to single text nodes suddenly having additional content and no longer being a name/value against an element. I was thinking about having an overload to take a schema, which could then place nodes with the potential to have more than one in an array.
Link Listing – July 20, 2006
Announcing Teamprise 1.1 and Vault 3.5 [Via: ]
Creating Smaller Virtual Machines [Via: ]
Customizing…
now if only SQL Server (et al.) had a built-in “ForJSON” that worked the same (only better) than “ForXML”