Last active
December 30, 2015 21:59
-
-
Save QQBoxy/7891082 to your computer and use it in GitHub Desktop.
Node.js Addon parseJson
Ref: http://www.mail-archive.com/v8-users@googlegroups.com/msg04430.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <node.h> | |
using namespace v8; | |
Handle<Value> parseJson(Handle<Value> jsonString) { | |
HandleScope scope; | |
Handle<Context> context = Context::GetCurrent(); | |
Handle<Object> global = context->Global(); | |
Handle<Object> JSON = global->Get(String::New("JSON"))->ToObject(); | |
Handle<Function> JSON_parse = Handle<Function>::Cast(JSON->Get(String::New("parse"))); | |
// return JSON.parse.apply(JSON, jsonString); | |
return scope.Close(JSON_parse->Call(JSON, 1, &jsonString)); | |
} | |
void Initialize(Handle<Object> exports) { | |
exports->Set(String::NewSymbol("parseJson"), FunctionTemplate::New(parseJson)->GetFunction()); | |
//IntelliSense: argument of type "v8::Handle<v8::Value> (*)(v8::Handle<v8::Value> jsonString)" is incompatible with parameter of type "v8::InvocationCallback" | |
} | |
NODE_MODULE(hello, Initialize) | |
/* | |
//test.js | |
var hello = require('./build/Release/hello'); | |
console.log( hello.parseJson( "{\"a\":1, \"b\":2}" ) ); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment