JSON In MinXML

Why?

JSON is a straightforward and useful transport (external) syntax that can be parsed into a variety of structures. MinXML is designed to be a common internal target for a variety parser, so it is quite natural to write a JSON parser that generates MinXML. Sharing the same internal format means that a system can have a shared back end for a variety of front-end files it supports.

Ginger, for example, uses MinXML as the common internal format for its auto-convert-and-load system; this allows the programmer to write complicated constants in a variety of external syntaxes such as JSON and have them converted, compiled and loaded on demand by the compiler and interpreter.

Translation Rules

Parsing JSON as MinXML boils down to providing a set of simple translation rules. There are many sensible ways to do this, potentially, without any best answer. So we re-used the rules provided in Ginger.

  • Integers such as -123 are translated into <constant type="integer" value="-123"/>.
  • Floating point numbers such as 1.3 are translated as <constant type="float" value="1.3"/>.
  • Strings such as "Hello, world!\n" are translated as <constant type="string" value="Hello, world!&#10;"/>.
  • Null is translated as <constant type="null" value="null"/>.
  • Booleans are translated as <constant type="boolean" value="true"/> or <constant type="boolean" value="false"/>.
  • Arrays [ … ] are translated as <array> … </array>
  • Objects such as { "foo" : "bar" } are translated as <object><constant field="foo" type="string" value="bar"/></object>

The thinking behind these rules is relatively simple. Expressions which encode their values entirely in an attribute are called constants. Arrays and objects are really collections whose members are either indexed positionally or indexed by a keyword. In addition, we anticipate the extensions represented by MinXSON.

Example

Original JSON:

{ "alpha": 42, "beta": [ null, -7.2, false ] }

Becomes this MinXML:

<object>
    <constant field="alpha" type="integer" value="42"/>
    <array field="beta">
        <constant type="null" value="null"/>
        <constant type="float" value="=7.2"/>
        <constant type="boolean" value="false"/>
    </array>
</object>

Resources