Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey !

I'm making a Node.js extension, and I would like to return a json format object, instead of a json-formatted string.

C++
#include <node.h>
#include <node_object_wrap.h>
using namespace v8;

void ListDevices(const FunctionCallbackInfo<Value>& args) {
	Isolate* isolate = Isolate::GetCurrent();
	HandleScope scope(isolate);

	std::string json = "[\"test\", \"test2\"]";
	args.GetReturnValue().Set(String::NewFromUtf8(isolate, json.c_str()));
}

void InitAll(Handle<Object> exports) {
	NODE_SET_METHOD(exports, "listDevices", ListDevices);
}

NODE_MODULE(addon, InitAll)


How can it be done ?
C#
var addon = require("./ADDON");

var jsonvar = JSON.parse(addon.listDevices());
console.log(jsonvar);

Actually, in this part, I would like to remove the JSON.parse

By the way, is it me, or it is really difficult to find documentation ? I tried on google, but a lot of content was out of date, and in v8.h, interesting functions weren't documented.

Thank you ;)
Posted
Updated 11-Jun-14 5:42am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900