Reading Stack Overflow Makes Me Do Strange Things
When browsing Stack Overflow I occasionally find myself doing ridiculous (programming related) things just because some one says something that my insane mind interprets as a challenge.
XML to JSON
For instance, after reading "There is no "one-to-one" mapping between XML and JSON..." I though to myself "Sure there is!"
JSON has much less meta-data (or baggage...) than XML generally rendering it more compact and, most importantly, much simpler. So:
<?xml version="1.0" ?>
<eveapi version="2">
<currentTime>2009-01-25 15:03:27</currentTime>
<result>
<rowset columns="name,characterID,corporationName,corporationID" key="characterID" name="characters">
<row characterID="999999" corporationID="999999" corporationName="filler data" name="someName"/>
</rowset>
</result>
<cachedUntil>2009-01-25 15:04:55</cachedUntil>
</eveapi>
... could instead be written as:
{
"time": "2009-01-25 15:03:27",
"cachedUtil": "2009-01-25 15:04:55"
"characters": [
{
"characterID": "999999",
"corporationID", "999999",
"corporationName": "filler data",
"name": "someName"
}
]
}
... it doesn't capture everything but it has all the data, it's easier to read and it is simple. Unfortunately, to achieve a 1-to-1 mapping from XML to JSON we need to store attributes, namespaces and other nasty XML stuff, but JSON nodes don't have metadata, they just have regular data.
You can; however, represent an Element Node (think DOM) using a JSON dictionary relatively easily. So:
<ant:property name="value" />
... becomes:
{
"element": "property",
"namespace-uri": "http://ant.apache.org/",
"children" : []
"attributes": [
["name", "value"]
]
}
Which means that our previous XML snippet could be represented as:
{
"attributes": [["version", "2"]],
"children": [{"attributes": [],
"children": ["2009-01-25 15:03:27"],
"name": "currentTime",
"namespace-uri": ""},
{"attributes": [],
"children": [{"attributes": [["key", "characterID"],
["columns",
"name,characterID,corporationName,corporationID"],
["name", "characters"]],
"children": [{"attributes": [["corporationName",
"filler data"],
["corporationID",
"999999"],
["characterID",
"999999"],
["name",
"someName"]],
"children": [],
"name": "row",
"namespace-uri": ""},
""],
"name": "rowset",
"namespace-uri": ""},
""],
"name": "result",
"namespace-uri": ""},
{"attributes": [],
"children": ["2009-01-25 15:04:55"],
"name": "cachedUntil",
"namespace-uri": ""},
""],
"name": "eveapi",
"namespace-uri": ""
}
... and you though XML was verbose. So you can represent an XML document in JSON but you essentially have to store the parse tree. Welcome to Missed the Pointville, population you (or me as the case may be).
I guess the statement stands.