It's not that hard to add, though.

I did something like this:

Object.prototype.inspect = function(json) {
    var kv = this.map_with_key(function(k, v) { return k.toString() + ": " +
v.inspect(json) });
    return "{" + kv.join(", ") + "}";
};

String.prototype.inspect = function(json) { return "\"" +
this.replace(/\"/g, '\\"') + "\""; };
Function.prototype.inspect = function(json) { return json ? this.toString()
: "(function)"; };
Number.prototype.inspect = function(json) { return this.toString(); }
Array.prototype.inspect = function(json) { return "[" + this.map(function(v)
{ return v == undefined ? "undefined" : v.inspect(json); }).join(", ") +
"]"}
RegExp.prototype.inspect = function(json) { return this.toString(); }

Then you get the JSON by calling myObject.inspect(true).

You probably need specific inspect-functions for more prototypes than I have
provided here, but I hope the idea is clear. :-)

Oh, by the way, the above depends on the following convenience functions
(many JS libraries provide something similar):

Object.prototype.map_with_key = function(f) {
    var result = new Array();
    for (var key in this) {
        result[result.length] = f(key, this[key]);
    };
    return result;
};

Array.prototype.map = function(f) {
    var result = new Array();
    for (var i = 0; i < this.length; ++i)
    {
        result[i] = f(this[i]);
    }
    return result;
};

- Simon

2008/9/10 Christian Plesner Hansen <[EMAIL PROTECTED]>

>
> No, there is no built-in support for JSON.  The debugger does use it
> internally but ordinary scripts don't have access the debugger
> internals.
>
>
> -- Christian
>
> On Wed, Sep 10, 2008 at 3:51 PM, Bryan White <[EMAIL PROTECTED]> wrote:
> >
> > Does V8 have any built-in support for JSON encode/decode?  There are a
> > log of mentions of JSON in these source files;
> >    debug.cc
> >    debug-delay.js
> >    mirror-delay.js
> >
> > But I don't seen any clear indication of if this stuff is intended to
> > be exposed or how it should be used from either C++ or JavaScript
> > code.
> >
> > I know I can compile and run to decode but that has security
> implications.
> >
> > I don't have a problem adding JSON support via my own code or finding
> > some JavaScript to do the job.  I just want to know If I am missing
> > the obvious.
> >
> > --
> > Bryan White
> >
> > >
> >
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to