JSON decoding

Brendan Eich brendan at mozilla.com
Tue Oct 24 12:46:23 PDT 2006

Here's the latest wiki'd proposal (thanks to Doug for updating it)  
for a standard JSON codec ([pretty] means optional argument, not  
array initialiser):

JSON encoding and decoding

The following built-in functions facilitate handling JSON data, as  
specified in Douglas Crockford's RFC 4627.

     * Object.prototype.toJSONString( [pretty] )

Returns a String containing the JSON representation of an object,  
array, date, string, number, boolean, or null . The [[Prototype]]  
internal property is not used when finding members. A Date object is  
serialized as an ISO date string in double quotes. An Array object is  
serialized as a sequence of comma separated values wrapped in square  
brackets. Otherwise, an object is serialized as a sequenced of comma  
separated pairs wrapped in curly braces. A string is serialized as a  
quoted string with backslash escapement. A finite number is  
serialized by toString. true , false , and null are encoded an  
unquoted strings. See http://www.ietf.org/rfc/rfc4627.txt?number=4627

An application can replace the toJSONString method on an instance or  
a class to change its JSON serialization. So, for example, to  
customize the serialization of dates, add a custom toJSONString  
method to either the individual date object or Date.prototype .

Members in objects whose valus are functions will be skipped.

A new EncodingError instance will be thrown if the encoder reaches a  
value that

     * cannot be encoded as valid JSON (such as a function in an  
array, undefined, and NaN) OR
     * represents a cycle (as happens when an object has a property  
that refers to itself)

Recurring objects that do not cause cycles are allowed, but will  
produce a complete text for each occurrence.

If the optional pretty parameter is true, then linefeeds are inserted  
after each { , [ , and , and before } and ] , and multiples of 4  
spaces are inserted to indicate the level of nesting, and one space  
will be inserted after : . Otherwise, no whitespace is inserted  
between the tokens.

     * EncodingError is a new exception type. It contains a name  
property with value “EncodingError” and a message (an implementation- 
defined string).


     * String.prototype.parseJSON( hook )

Returns the value represented by the string. A syntaxError is thrown  
if the string was not a strict JSON text.

The optional hook function will be called for each object value  
found. It is passed each object. Its return value will be used  
instead of the object in the final structure.

This is pretty minimal.  It reflects Bob's helpful comments.  It  
still gives Date default encoding better than what toString produces,  
to wit toISOString's result.  Without evolving JSON to talk about ISO  
date literals, this is as far as we want to go.  We could try to  
invent a string date convention that can round-trip, but it could  
conflict with JSON schemas in use now or in the future.  So we leave  
it to anyone who wants that to hook up Date.prototype.toJSONString.

Comments welcome as always.


More information about the Es4-discuss mailing list