JSON decoding

Brendan Eich brendan at mozilla.com
Tue Oct 24 14:31:31 PDT 2006


On Oct 24, 2006, at 5:24 PM, zwetan wrote:

> On 10/24/06, Brendan Eich <brendan at mozilla.com> wrote:
>> An application can replace the toJSONString method on an instance or
>> a class to change its JSON serialization.
> <snip>
>> 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.
>>
>
> Does ES4 will also provide functions for JSON deserialization ?
> and if yes, are those function will also be replaceable ?
> something in the line of Object.prototype.fromJSONString ?

I realize the formatting wasn't ideal (copy-paste from dokuwiki) but  
the answer was in the message to which you replied:

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.
-----

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.

/be




More information about the Es4-discuss mailing list