Using stringify from the v8 shell

I found this way of doing the reverse (JSON to v8 object), using v8s built in JSON.parse function. http://www.mail-archive.com/[email protected]/msg04430.html

Adjusting this to use JSON.stringify instead would look kind of like this (untested):

Handle<String> toJson(Handle<Value> object)
{
    HandleScope scope;

    Handle<Context> context = Context::GetCurrent();
    Handle<Object> global = context->Global();

    Handle<Object> JSON = global->Get(String::New("JSON"))->ToObject();
    Handle<Function> JSON_stringify = Handle<Function>::Cast(JSON->Get(String::New("stringify")));

    return scope.Close(JSON_stringify->Call(JSON, 1, object));
}