There's some talk on the net about how JSON can be parsed by YAML parsers. However, that still doesn't quite make JSON as suitable as YAML, only YAML as JSON.

I've written before about my preference to YAML over XML for writing configuration files. YAML has a much cleaner syntax as is IMHO easier to write an work with. So I've picked YAML as the configuration file format for specifying the AST structure for my C parser in Python project. Here's a smallish sample:

ArrayDecl: [type*, dim*]

ArrayRef: [name*, subscript*]

# op: =, +=, /= etc.
#
Assignment: [op, lvalue*, rvalue*]

BinaryOp: [op, left*, right*]

I find this very readable, the minimal syntax required to convey a meaning.

Anyway, today I wanted to check if I can use a JSON parser to parse this file (after all, the json module is standard in Python 2.6 and 3), but very quickly realized it's impossible.

JSON is a subset of YAML, but not the other way around. While YAML is used for configuration files, JSON is strictly a data exchange format, and all its data has to be correctly qualified as dicts, strings, lists, etc. Like this:

'["foo", {"bar":["baz", "kwa", 1.0, 2]}]'

The above is the mandatory JSON for what could've been written in YAML as:

- foo
- bar:
    - baz
    - kwa
    - 1.0
    - 2

While the JSON version (which is, as I said, valid YAML) is suitable for terse data exchange, the YAML one is more readable as a configuration specification.

So, no JSON for me - I'll stick with YAML. It's a shame YAML won't make it into the Python standard (while things like Windows .ini - like configuration files are in), but there are good libraries available for download.

Anyhow, lately I'm leaning more towards internal DSLs. With the with statement fully standard in Python 2.6/3, internal DSLs in Python will become more common and easier to write. I guess I'll leave this topic for another post, though.