Now let's have a look at two examples with the insight we gained up to here. Microformats are well suited, as they are an open standard and short enough for a brief discussion.
XOXO as a simple XHTML based outline format is one of several microformats. The slightly modified sample from the Draft Specification reads:
<ol class="xoxo">
<li>Subject 1
<ol>
<li>subpoint a</li>
<li>subpoint b</li>
</ol>
</li>
<li><span>Subject 2</span>
<ol compact="compact">
<li>subpoint c</li>
<li>subpoint d</li>
</ol>
</li>
</ol>
Now we apply the patterns above to convert this XML document fragment to a JSON structure.
- The outer list with two list items is converted using pattern 6.
- The first list item contains a single textual content "Subject 1" and an inner list element. So it can be treated according to pattern 7.
- The first inner list is converted with pattern 6 again.
- Pattern 5 is applied to the second item of the outer list.
- The second inner list is converted using a combination of pattern 3 and 6.
Here is the resulting JSON structure, which is reversible without loss of information.
"ol": {
"li": [
{
"#text": "Subject 1",
"ol": {
"li": ["subpoint a", "subpoint b"]
}
},
{
"span": "Subject 2",
"ol": {
"@compact": "compact",
"li": ["subpoint c", "subpoint d"]
}
}
]
}
hCalender is another microformat based on the iCalender standard. We just ignore the fact, that the iCalender format could be more easily converted to JSON and look at a hCalender event example, which is also slightly modified so, that it is a structured rather than a mixed semi-structured document fragment.
<span class="vevent">
<a class="url" href="http://www.web2con.com/">
<span class="summary">Web 2.0 Conference</span>
<abbr class="dtstart" title="2005-10-05">October 5</abbr>
<abbr class="dtend" title="2005-10-08">7</abbr>
<span class="location">Argent Hotel, San Francisco, CA</span>
</a>
</span>
Here patterns 2, 3, 4, 5 and 6 are used to generate the following JSON structure:
"span": {
"a": {
"@class": "url",
"@href": "http://www.web2con.com/",
"span": [
{ "@class": "summery", "#text": "Web 2.0 Conference" },
{ "@class": "location", "#text": "Argent Hotel, San Francisco, CA" }
},
"abbr": [
{ "@class": "dtstart", "title": "2005-10-05", "#text": "October 5" },
{ "@class": "dtend", "title": "2005-10-08", "#text": "7" }
}
}
}
This example demonstrates a conversion which does not preserve the original element order. Even if this may not change semantics here, we generally can
- state, that a conversion isn't sufficiently possible.
- tolerate the result, if order doesn't matter.
- try to make our XML document more JSON-friendly.
In many cases the last point may be not acceptable, at least when the XML document is based on existing standards. But in other cases it may be worth the effort to consider some subtle XML changes, which can make XML and JSON play nicely together. Changing the <abbr>
elements to <span>
elements in the hCalender example would be such an improvement.