UNPKG

1.94 kBMarkdownView Raw
1## JSON
2
3The _JSON_ object provides the c++ versions of the methods offered by the `JSON` object in javascript. V8 exposes these methods via the `v8::JSON` object.
4
5 - <a href="#api_nan_json_parse"><b><code>Nan::JSON.Parse</code></b></a>
6 - <a href="#api_nan_json_stringify"><b><code>Nan::JSON.Stringify</code></b></a>
7
8Refer to the V8 JSON object in the [V8 documentation](https://v8docs.nodesource.com/node-7.4/da/d6f/classv8_1_1_j_s_o_n.html) for more information about these methods and their arguments.
9
10<a name="api_nan_json_parse"></a>
11
12### Nan::JSON.Parse
13
14A simple wrapper around [`v8::JSON::Parse`](https://v8docs.nodesource.com/node-7.4/da/d6f/classv8_1_1_j_s_o_n.html#a936310d2540fb630ed37d3ee3ffe4504).
15
16Definition:
17
18```c++
19Nan::MaybeLocal<v8::Value> Nan::JSON::Parse(v8::Local<v8::String> json_string);
20```
21
22Use `JSON.Parse(json_string)` to parse a string into a `v8::Value`.
23
24Example:
25
26```c++
27v8::Local<v8::String> json_string = Nan::New("{ \"JSON\": \"object\" }").ToLocalChecked();
28
29Nan::JSON NanJSON;
30Nan::MaybeLocal<v8::Value> result = NanJSON.Parse(json_string);
31if (!result.IsEmpty()) {
32 v8::Local<v8::Value> val = result.ToLocalChecked();
33}
34```
35
36<a name="api_nan_json_stringify"></a>
37
38### Nan::JSON.Stringify
39
40A simple wrapper around [`v8::JSON::Stringify`](https://v8docs.nodesource.com/node-7.4/da/d6f/classv8_1_1_j_s_o_n.html#a44b255c3531489ce43f6110209138860).
41
42Definition:
43
44```c++
45Nan::MaybeLocal<v8::String> Nan::JSON::Stringify(v8::Local<v8::Object> json_object, v8::Local<v8::String> gap = v8::Local<v8::String>());
46```
47
48Use `JSON.Stringify(value)` to stringify a `v8::Object`.
49
50Example:
51
52```c++
53// using `v8::Local<v8::Value> val` from the `JSON::Parse` example
54v8::Local<v8::Object> obj = Nan::To<v8::Object>(val).ToLocalChecked();
55
56Nan::JSON NanJSON;
57Nan::MaybeLocal<v8::String> result = NanJSON.Stringify(obj);
58if (!result.IsEmpty()) {
59 v8::Local<v8::String> stringified = result.ToLocalChecked();
60}
61```
62