Wednesday, June 10, 2015

JSON/R

JSON is well and good, but it has one shortcoming: the management of object references. When you have to serialize an object graph, with object cycles, JSON is lost.

My client needed to serialize such a graph. So I wrote a JSON extension, dubbed "JSON/R" (JSON with references).

I wanted the parser to stay as pretty as the standard JSON's (see a previous article), with the same property: a parser with no backtrack.

I added two concepts: "heap" and "reference".

A "heap" is just an array with different brackets (< and >); its usage is that the objects in it are "referenced" by their position in the array. By convention, the root object of the graph is the first one, in other words, $0.

A "reference" is an index into the heap: noted $<integer>.

I should write a free parser too. The concept is basic enough to be quite useful.