by Dave Herman (dherman at ccs dot neu dot edu)
|(require (planet dherman/json:4:=0))|
Because JSON distinguishes syntactically between null, array literals, and object literals, this library uses non-overlapping datatypes for the three corresponding variants of jsexpr.
Since the Scheme null value '() overlaps with lists, there is no natural choice for the jsexpr represented as null. We prefer #\null as the least objectionable option from Scheme’s host of singleton datatypes (note that the #<void> and #<undefined> constants do not have readable and writeable representations, which makes them less convenient choices).
The JSON RFC only states that object literal expressions "SHOULD" contain unique keys, but does not proscribe them entirely. Looking at existing practice, it appears that popular JSON libraries parse object literals with duplicate keys by simply picking one of the key-value pairs and discarding the others with the same key. This behavior is naturally paralleled by PLT Scheme hash tables, making them a natural analog.
Finally, the JSON RFC is almost completely silent about the order of key-value pairs. While the RFC only specifies the syntax of JSON, which of course always must represent object literals as an ordered collection, the introduction states:
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
In practice, JSON libraries discard the order of object literals in parsed JSON text and make no guarantees about the order of generated object literals. This again makes hash tables a good choice for representing as JSON object literals.