JSON in MinXML Tutorial

JSON is a popular notation for representing hierarchical data. It has a deliberately limited palette of data types - arrays, records, numbers, strings, booleans and null - that is surprisingly useful. However JSON is an external grammar that doesn't specify what the internal representation should be. In some languages, like Python, there's such a strong natural internal representation that it is easy to overlook the alternatives.

In this section we look at parsing JSON source into an MinXML tree. This can be a neat approach when working in a strongly typed-language such as Java. And it can also be an elegant way of merging JSON data into a richer system of types. It is also a stepping stone to the more powerful hybrid language MinXSON.

The class com.steelypip.powerups.json.MinXMLJSONParser is used to parse JSON as MinXML from a java.io.Reader source. For example:

import com.steelypip.powerups.json.MinXMLJSONParser;
import java.io.StringReader;
import java.io.OutputStreamWriter;
...
MinXML json_as_minxml = new MinXMLJSONParser( new StringReader( "[ 1, true ]" ) ).read();
json_as_minxml.prettyPrint( new OutputStreamWriter( System.out ) );

This generates the following MinXML output:

<array>
    <constant type="integer" value="1"/>
    <constant type="boolean" value="true"/>
</array>