How to convert a MaybeLocal String type to a std string?

1

I'm doing a addon for nodejs in c++ , I'm new mu% in c++ by the way, what I try to do is a function in javascript that receives an object and this convert it to a std::string in c++ , until now after researching in google, I have managed to convert the object that is passed from javascript to an object Nan::MaybeLocal<v8::String> using the example that is in the documentation of Nan .

My c++ function is like this:

void ParseJSONToJSONString(const Nan::FunctionCallbackInfo<v8::Value>& info){

    v8::Isolate* isolate = info.GetIsolate();

    if(info.Length() != 1){
        isolate->ThrowException(v8::Exception::TypeError(
            v8::String::NewFromUtf8(isolate, "Se esperaba 1 argumento.")));
        return;
    }

    //Usando el ejemplo

    v8::Local<v8::Object> obj = Nan::To<v8::Object>( info[0] ).ToLocalChecked();

    Nan::JSON NanJSON;
    Nan::MaybeLocal<v8::String> result = NanJSON.Stringify( obj );

    //Y ahora quisiera convertir la variable "result" que se supone que contiene el objeto que se paso desde javascript ya convertido en string, en un "std::string" 

}

Since javascript the function is called like this:

var dbm = require('./dbm');
//El modulo que estoy haciendo se llama 'dbm' por cierto.

dbm.saveJSONString({
    uno: 1,
    dos: "dos"
});

What I would like is to convert this variable from type Nan::MaybeLocal<v8::String> to% std::string to be able to treat it as std::string .

Thank you very much in advance.

    
asked by ftorres 13.09.2017 в 19:58
source

1 answer

0

In theory, what you have to do is convert the object MaybeLocal into a Local . Then you extract the UTF character sequence and, with that string, you can create the std::string object:

v8::Local<v8::String> v8_string = /* ... */;
Nan::Utf8Value nan_string(v8_string);
std::string std_string(*nan_string);
    
answered by 18.09.2017 / 12:25
source