An open API service indexing awesome lists of open source software.

https://github.com/mkrufky/node-native-json

Access the v8::JSON object methods Parse & Stringify from native c++ addons across all versions of node.js
https://github.com/mkrufky/node-native-json

Last synced: 4 months ago
JSON representation

Access the v8::JSON object methods Parse & Stringify from native c++ addons across all versions of node.js

Awesome Lists containing this project

README

          

This project has been merged into `NAN` and is now deprecated. Use `Nan::JSON` from `NAN` v2.6.2 or later. See [Nan::JSON](https://github.com/nodejs/nan/blob/master/doc/json.md)

# native-json

Access the `v8::JSON` object methods `Parse` & `Stringify` from native c++ addons across all versions of node.js

[![NPM](https://nodei.co/npm/native-json.png?downloads=true&downloadRank=true)](https://nodei.co/npm/native-json/)

[![Build Status](https://api.travis-ci.org/mkrufky/node-native-json.svg?branch=master)](http://travis-ci.org/mkrufky/node-native-json)
[![Build status](https://ci.appveyor.com/api/projects/status/p5cgmixxpftd8hxa?svg=true)](https://ci.appveyor.com/project/mkrufky/node-native-json)

native-json is a header package for building c++ native addons for node.js that is meant to complement **[nan](https://github.com/nodejs/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:

``` bash
$ 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 ` in your *.cpp* files:

``` python
"include_dirs" : [
"` when compiling your addon.

## API

``` c++
Nan::MaybeLocal Parse(v8::Local jsonString);

Nan::MaybeLocal Stringify(v8::Local jsonObject);
Nan::MaybeLocal Stringify(v8::Local jsonObject, v8::Local 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

``` c++
v8::Local jsonString =
Nan::New("{ \"JSON\": \"object\" }").ToLocalChecked();

v8::Local 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

``` c++
v8::Local object = Nan::To(parsedValue);

v8::Local stringified =
Native::JSON::Stringify(object).ToLocalChecked();
```

### Tests

To run the native-json tests do:

``` sh
npm install
npm run-script rebuild-tests
npm test
```

Or just:

``` sh
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.