[whatwg] JSON encoding

Douglas Crockford wrote:
>  > I think the hazard is very real and very serious.
>  > I don't see any additional expressiveness enabled by toJSONString.
> 
> No one is more concerned about the security of the platform than I am.
> There isn't a security issue here. The hazard, if there is one, is
> certainly not serious.
> 
> This is a convenience issue. Having toJSONString as a builtin is a 
> convenience, removing the need to load json.js.
> 

Why not override the toString method?

Object.prototype.toString(type) {
   if (type == "json") {
     // return a json serialisation
   } else {
     // return the usual string conversion
   }
};

That way legacy browsers can also support JSON serialisation without 
breaking "for in" enumeration.

-dean

Received on Friday, 3 November 2006 10:05:46 UTC