This project has been merged into NAN
and is now deprecated. Use Nan::JSON
from NAN
v2.6.2 or later. See Nan::JSON
native-json
Access the v8::JSON
object methods Parse
& Stringify
from native c++ addons across all versions of node.js
native-json is a header package for building c++ native addons for node.js that is meant to complement nan by adding an interface to the v8::JSON
object's Parse
and Stringify
methods that can be relied upon regardless of the version of node.js that the addon is being built against.
Usage
Simply add nan and native-json as dependencies in the package.json of your Node addon:
$ npm install --save nan
$ npm install --save native-json
Pull in the paths to nan and native-json in your binding.gyp so that you can use #include <native-json.h>
in your .cpp files:
"include_dirs" : [
"<!(node -e \"require('nan')\")",
"<!(node -e \"require('native-json')\")"
]
This works like a -I<path-to-native-json>
when compiling your addon.
API
Nan::MaybeLocal<v8::Value> Parse(v8::Local<v8::String> jsonString);
Nan::MaybeLocal<v8::String> Stringify(v8::Local<v8::Object> jsonObject);
Nan::MaybeLocal<v8::String> Stringify(v8::Local<v8::Object> jsonObject, v8::Local<v8::String> gap);
To access the javascript function JSON.parse()
from your c++ code, call Native::JSON::Parse()
where you would normally call v8::JSON::Parse()
, which would otherwise not be available in versions of node.js older than 0.12.x
v8::Local<v8::String> jsonString =
Nan::New("{ \"JSON\": \"object\" }").ToLocalChecked();
v8::Local<v8::Value> parsedValue =
Native::JSON::Parse(jsonString).ToLocalChecked();
To access the javascript function JSON.stringify()
from your c++ code, call Native::JSON::Stringify()
where you would normally call v8::JSON::Stringify()
, which would otherwise not be available in versions of node.js older than 7.x
v8::Local<v8::Object> object = Nan::To<v8::Object>(parsedValue);
v8::Local<v8::String> stringified =
Native::JSON::Stringify(object).ToLocalChecked();
Tests
To run the native-json tests do:
npm install
npm run-script rebuild-tests
npm test
Or just:
npm install
make test
Licence & copyright
Copyright (c) 2017 Michael Ira Krufky
native-json is licensed under an MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.