JSON decoding

Bob Ippolito bob at redivi.com
Fri Oct 20 12:38:23 PDT 2006

On 10/20/06, Brendan Eich <brendan at mozilla.org> wrote:
> On Oct 11, 2006, at 8:06 PM, Brendan Eich wrote:
> > On Oct 11, 2006, at 3:06 PM, Robert Sayre wrote:
> >
> >> I'm pretty sure the group accepted some form of Crockford's parseJSON
> >> proposal, but I think it would be handy to add something analogous to
> >> simplejson's object_hook argument. One weakness JSON has is
> >> annotating
> >> literals (like strings with custom attributes), and this facility can
> >> make that smoother.
> >>
> >> <http://svn.red-bean.com/bob/simplejson/trunk/docs/module-
> >> simplejson.html#load>
> >>
> >> See "Specializing JSON object decoding" above as well.
> >
> > Thanks, the wiki will be re-exported after the next face-to-face
> > TG1 meeting (which is the end of next week).  We should have
> > finalized the JSON APIs by then.  We were already considering an
> > optional filter funarg, but Bob's object_hook is more powerful.
> > I've added a comment to the wiki with that link.
> Sneak preview (wiki re-export coming soon) of Doug Crockford's latest
> rev of the proposal:
> JSON encoding and decoding
> The following built-in functions facilitate handling JSON data, as
> specified in Douglas Crockford's RFC 4627.
> Encoding
>      * Object.prototype.toJSONString( filter , pretty )
> Returns a String containing the JSON representation of an object,
> array, date, string, number, boolean, or null . The proto link 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 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 encodingError will be thrown if the encoder reache a value which
>      * cannot be encoded as valid JSON (such as a function,
> undefined, and NaN) OR
>      * represents a cycle (as happens when an object has a property
> that refers to itself)
> Recurring objects which do not cause cycles are allowed, but will
> produce a complete text for each occurrence.
> A filter is an optional function which takes two parameters: a name
> and a value. The function will be called for each candidate pair in
> each object being serialized. If the function returns true, then the
> pair will be included. If the function returns false, then the pair
> will be excluded. If the function returns any other value, or if it
> throws, then an encodingError exception is thrown.
> If the 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.
> Decoding
>      * 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.
> -----
> I like this proposal a lot; it avoids too many bells and whistles,
> and captures the desired (different) hooks for encoding and decoding,
> as well as pretty-encoding.  Comments?

Encoding seems incredibly weak. You can put Date objects in, but
you'll never get a Date object back out. The filter function seems
really bizarre and a little useless. Why key:value pair? Why no
context? No opportunity for object replacement?

Decoding looks great, though.


More information about the Es4-discuss mailing list