Page 1 of 1

JSON support

Posted: Sat Aug 03, 2013 4:53 am
by pkoppstein
This is an enhancement request regarding support for JSON. In brief, I would like newlisp to support a variant of json-parse that preserves the JSON/Javascript distinction between objects and arrays.

Apart from the intrinsic value of preserving the duality, another consideration is invertibility. In other programming languages that have a "fromJSON" function, there is typically a converse (quasi-invertible) function for mapping back to JSON.

(For example: Javascript has JSON.stringify and JSON.parse; PHP has json_encode and json_decode; python has json.loads and json.dumps; R has fromJSON and toJSON, etc. )

Currently, json-parse cannot have such a quasi-invertible counterpart because it maps both JSON objects and arrays to newlisp lists. For example:

> (set 'a (json-parse [text][["a",2]][/text] ))
(("a" 2))
> (set 'o (json-parse [text]{"a":2}[/text] ))
(("a" 2))

> (= a o)

Since newlisp does have arrays, one option would be to map JSON arrays to newlisp arrays. Perhaps there are better options, but this is the most obvious one to me.

In any case, one important point is that maintaining the JSON duality can be accomplished without causing backward-compatibility issues, since json-parse does not currently take a second argument.

That is, (json-parse STRING true) could preserve the duality.