JSON decoding

Bob Ippolito bob at redivi.com
Tue Oct 24 19:57:27 PDT 2006


On 10/24/06, Brendan Eich <brendan at mozilla.com> wrote:
> On Oct 24, 2006, at 7:09 PM, Bob Ippolito wrote:
>
> > On 10/24/06, zwetan <zwetan at gmail.com> wrote:
> >> 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.
>
> What exactly is the difference between a function that can be wrapped/
> overridden (or class that can be subclassed) and a method that can be
> overridden?  Can you show an example of how this breaks?  Sorry if
> this is obvious.
>

There is no meaningful difference, but that's not what I'm talking about.

toJSONString is the wrong level of abstraction. The return value
should NOT be a string that inserts arbitrary text into the encoding
stream. Injecting arbitrary text into the stream is an absolute
guarantee that (most) people will do it incorrectly and produce
invalid documents. Worst case they will use it to intermittently
produce invalid documents. Maybe there's even something

The return value should be any JSON-encodable object that gets
properly encoded into the string (called "toJSON" perhaps). If this
approach is taken then it will be *impossible* to produce invalid
documents, much like it's impossible to produce an invalid XML
document by serializing a DOM tree.

-bob



More information about the Es4-discuss mailing list