I'm doing a addon
for nodejs
in c++
, I'm very new to c++
by the way, what I'm trying to do is a function in javascript
that receives an object and converts it to an std::string
in c++
, so far after doing some research on google, I've managed to convert the object passed from javascript
to an object Nan::MaybeLocal<v8::String>
using the example in the Nan
.
My function c++
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"
}
From javascript
the function it 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 cast that type variable Nan::MaybeLocal<v8::String>
to a std::string
so that I can treat it as std::string
.
Thank you very much in advance.
In theory what you have to do is convert the object
MaybeLocal
to aLocal
. Then you extract the UTF character sequence, and with that string, you can now create the objectstd::string
: