Initial prototype parser in subversion

7 views
Skip to first unread message

Patrick Maupin

unread,
Mar 6, 2010, 2:12:06 AM3/6/10
to rson-discuss
A parser has been constructed for what I call "enhanced JSON." I
have not yet added the indentation-based parsing on top of JSON; this
just handles a much-relaxed JSON syntax.

Even so, it is pretty good. In addition to parsing stock JSON, and
not needing quotes in most places, and allowing things like hex/octal/
binary numbers and trailing commas, it will parse some subset of
Python, so it is possible to have a Pythonish configuration file that
is not passed through eval():

---

# Comment goes here

x = 27
y = [1, 2, 3, {"z": 42}]


# Although comments cannot go after other data,
# they can be arbitrarily indented.

m = """
Now is the time for all good men
to come to the aid of their country.
"""

w = ["""test""",
"""triple""",
{"""quotes""":"ok?"}]

num1 = 0x100
num2 = 0b101010
num3 = 0o455
num5 = 0.1
num6 = 01.2
num7 = .2

---

It also parses some other things quite easily. Today, a coworker gave
me a file that looked something like this:

data1 = [
-1.05
0.00
+2.3];

data2 = [
-2.7
0.29
+4.2];

It was quite easy and natural to extend the parser to handle this:
- If a new element is encountered in a list without an intervening
comma, accept the newline as a delimiter.
- Ignore a colon when looking for a comma or newline delimiter.

So, these are the sorts of rules I would like to think about when
adding the indentation and other things -- simple things that make
sense that can be easily documented and help cope with other
preexisting file formats.

The code can be checked out here:

http://rson.googlecode.com/svn/trunk/py2x

Currently, you can invoke it in two different ways:

from rson.ejson import loads
from rson.cjson import loads

"cjson" is "compatible json" -- basically ejson restricted until it
plays nicely with the simplejson testsuite (plus Infinity and NaN
added). It passes all the tests except the speedups test and a test
which checks that you can't have control characters in a string.

Reply all
Reply to author
Forward
0 new messages