JSON decoding

Bob Ippolito bob at redivi.com
Tue Oct 24 16:09:16 PDT 2006


On 10/24/06, zwetan <zwetan at gmail.com> wrote:
> On 10/24/06, Brendan Eich <brendan at mozilla.com> wrote:
> <snip>
> > I realize the formatting wasn't ideal (copy-paste from dokuwiki) but
> > the answer was in the message to which you replied:
> >
>
> and I read it too fast so it didn't helped :)
>
> > Decoding
> >
> >      * String.prototype.parseJSON( hook )
> >
> <snip>
> > Object.prototype.fromJSONString does not make sense, since the string
> > comes from |this| (if it came from a parameter, then you'd want a
> > String "static method").  So String.prototype.parseJSON is the
> > decoder, and it can be wrapped or replaced just like other
> > String.prototype method properties, although the object hook
> > (inspired directly by Bob Ippolito's simplejson) will probably
> > relieve the need to override.
> >
>
> indeed the hook is a good approach but with what I try to achieve
> overriding the method look like the only option I will have
>
> quick pseudo example:
>
> Date.prototype.toJSONString
> return something as "new Date( 2006, 9, 24, ... )"
>
> String.prototype.parseJSON
> should be able to scan the incoming string
> something as "{ a: new Date(2006, 9, 24), b:new Date( 2006, 11, 25 ), etc. }"
> to reckonize "new" keyword
> then "Date" keyword, then scan open/close parenthesis
> then dynamically create an instance of the Date object
> (thanks to the .call/.apply function)
>
> I would prefer to use the hook but I suppose using
> a "new class(...) " notation inside a JSON string
> will yeld an EncodingError.

This is exactly why toJSONString shouldn't be the API, it should deal
in objects instead... toJSONString guarantees that the
interoperability we currently enjoy will vanish.

-bob



More information about the Es4-discuss mailing list