JSON decoding

Brendan Eich brendan at mozilla.com
Tue Oct 24 20:37:50 PDT 2006


On Oct 24, 2006, at 10:57 PM, Bob Ippolito wrote:

> 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

Last sentence was cut off.

I thought you might mean that, but I don't see how to do it.  We  
can't declare a type for "the set of all valid JSON values".  If we  
take an object of some type, we want to rule out cycles, Date objects  
that don't have toJSON or whatever methods, etc.  How does simplejson  
do this?

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

That's circular in that a DOM implementation will build only well- 
formed DOM trees.  To express the type "valid JSON value" we would  
need recursive structural types, and that still wouldn't catch  
cycles.  We can't force this type into the nominal part of the type  
system.

Anyway, if we are missing something in simplejson, please show us the  
way.  Thanks,

/be




More information about the Es4-discuss mailing list