Python’s simpleparse module


I’m working on a project that required a bit more from the JSON parser than the stock JSON parser with Python allowed for. After doing some hunting around, I came to the unfortunate conclusion that I’d probably need to write my own.

Thankfully, Python’s simpleparse module lived up to its billing (thanks in large part to JSON having such trivial syntax) Here’s the working BNF suitable for passing to simpleparse:

<ws> := [ tnr]*

>string_json< := string_single_quote / string_double_quote
key := string_json
array := "[", ws, ( value, (ws, ",", ws, value)* )?, ws, "]"

member := key, ws, ":", ws, value
object := "{", ws, ( member, ( ws, ",", ws, member )* )?, ws, "}"

true := "true"
false := "false"
null := "null"
>value< := false / null / true / object / array / float / int / string_json
>document< := ws, value, ws

This is also available in source code form here: jsonorder.py. jsonorder.py is part of a larger project, but it also works as a standalone library (albeit not a terribly useful one for most uses)

One thought on “Python’s simpleparse module

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s