Created
April 30, 2020 09:48
-
-
Save NickNaso/7793afb4f22eadd28735353c56253474 to your computer and use it in GitHub Desktop.
Process JSON on node-addon-api (SImple example)
This file contains hidden or 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
{ | |
"targets": [ | |
{ | |
"target_name": "json", | |
"cflags!": [ "-fno-exceptions" ], | |
"cflags_cc!": [ "-fno-exceptions" ], | |
"sources": [ "json.cc" ], | |
"include_dirs": [ | |
"<!@(node -p \"require('node-addon-api').include\")" | |
], | |
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], | |
} | |
] | |
} |
This file contains hidden or 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
'use strict' | |
const assert = require('assert') | |
const addon = require('bindings')('json'); | |
const jsonObj = { a: 1 } | |
const jsonString = JSON.stringify(jsonObj) | |
assert.strictEqual(addon.jsonStringify(jsonObj), jsonString) | |
assert.deepStrictEqual(addon.jsonParse(jsonString), jsonObj) |
This file contains hidden or 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 <napi.h> | |
Napi::String JSONStringify(const Napi::CallbackInfo& info) { | |
Napi::Env env = info.Env(); | |
Napi::Object json_object = info[0].As<Napi::Object>(); | |
Napi::Object json = env.Global().Get("JSON").As<Napi::Object>(); | |
Napi::Function stringify = json.Get("stringify").As<Napi::Function>(); | |
return stringify.Call(json, { json_object }).As<Napi::String>(); | |
} | |
Napi::Object JSONParse(const Napi::CallbackInfo& info) { | |
Napi::Env env = info.Env(); | |
Napi::String json_string = info[0].As<Napi::String>(); | |
Napi::Object json = env.Global().Get("JSON").As<Napi::Object>(); | |
Napi::Function parse = json.Get("parse").As<Napi::Function>(); | |
return parse.Call(json, { json_string }).As<Napi::Object>(); | |
} | |
Napi::Object Init(Napi::Env env, Napi::Object exports) { | |
exports["jsonStringify"] = Napi::Function::New(env, JSONStringify); | |
exports["jsonParse"] = Napi::Function::New(env, JSONParse); | |
return exports; | |
} | |
NODE_API_MODULE(json, Init) |
This file contains hidden or 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
{ | |
"name": "json-example", | |
"version": "0.0.0", | |
"description": "Node.js Addons Example", | |
"main": "index.js", | |
"private": true, | |
"dependencies": { | |
"bindings": "^1.5.0", | |
"node-addon-api": "^2.0.0" | |
}, | |
"scripts": { | |
"test": "node index.js" | |
}, | |
"gypfile": true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment