how to convert v8::Value to json string

1,357 views
Skip to first unread message

Yong Wang

unread,
Mar 2, 2016, 3:00:48 AM3/2/16
to v8-users
Hi all,

    I do not find any api can convert v8::Value to json string. 
Could someone can give me a help?

   thanks in advance.
    

Ben Noordhuis

unread,
Mar 2, 2016, 6:08:32 AM3/2/16
to v8-users
There is no API method but what you can do is look up the 'JSON'
property on the global object, then call the 'stringify' method with
your v8::Value as the argument.

Yong Wang

unread,
Mar 2, 2016, 7:56:24 PM3/2/16
to v8-users
I see, thanks

在 2016年3月2日星期三 UTC+8下午7:08:32,Ben Noordhuis写道:

Pavel Medvedev

unread,
Mar 4, 2016, 6:39:27 AM3/4/16
to v8-users
I use built-in JSON.stringify() like this:


/// Stringify V8 value to JSON
/// return empty string for empty value
std
::string json_str(v8::Isolate* isolate, v8::Handle<v8::Value> value)
{
   
if (value.IsEmpty())
    {
        return std::string();
    }
 
    v8
::HandleScope scope(isolate);
 
    v8
::Local<v8::Object> json = isolate->GetCurrentContext()->
        
Global()->Get(v8::String::NewFromUtf8(isolate, "JSON"))->ToObject();
    v8
::Local<v8::Function> stringify = json->Get(v8::String::NewFromUtf8(isolate, "stringify")).As<v8::Function>();
 
    v8
::Local<v8::Value> result = stringify->Call(json, 1, &value);
    v8
::String::Utf8Value const str(result);
 
    
return std::string(*str, str.length());
 
}


By the way, there is v8::JSON::Parse() function in V8 API, and I'm wondered why there is no v8::JSON::Stringify() counterpart.

Jochen Eisinger

unread,
Mar 7, 2016, 5:27:45 AM3/7/16
to v8-u...@googlegroups.com
On Fri, Mar 4, 2016 at 12:39 PM Pavel Medvedev <pmed...@gmail.com> wrote:
I use built-in JSON.stringify() like this:


/// Stringify V8 value to JSON
/// return empty string for empty value
std
::string json_str(v8::Isolate* isolate, v8::Handle<v8::Value> value)
{
   
if (value.IsEmpty())
    {
        return std::string();
    }
 
    v8
::HandleScope scope(isolate);
 
    v8
::Local<v8::Object> json = isolate->GetCurrentContext()->
        
Global()->Get(v8::String::NewFromUtf8(isolate, "JSON"))->ToObject();
    v8
::Local<v8::Function> stringify = json->Get(v8::String::NewFromUtf8(isolate, "stringify")).As<v8::Function>();
 
    v8
::Local<v8::Value> result = stringify->Call(json, 1, &value);
    v8
::String::Utf8Value const str(result);
 
    
return std::string(*str, str.length());
 
}


By the way, there is v8::JSON::Parse() function in V8 API, and I'm wondered why there is no v8::JSON::Stringify() counterpart.

Mainly because there's no C++ stringifier in V8. There's a basic stringifier, but it doesn't support to full glory of proxies, etc..
 

On Wednesday, March 2, 2016 at 11:00:48 AM UTC+3, Yong Wang wrote:
Hi all,

    I do not find any api can convert v8::Value to json string. 
Could someone can give me a help?

   thanks in advance.
    

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages