1 |
3a515b92
|
cagy
|
## JSON
|
2 |
|
|
|
3 |
|
|
The _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 |
|
|
|
8 |
|
|
Refer to the V8 JSON object in the [V8 documentation](https://v8docs.nodesource.com/node-8.11/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 |
|
|
|
14 |
|
|
A simple wrapper around [`v8::JSON::Parse`](https://v8docs.nodesource.com/node-8.11/da/d6f/classv8_1_1_j_s_o_n.html#a936310d2540fb630ed37d3ee3ffe4504).
|
15 |
|
|
|
16 |
|
|
Definition:
|
17 |
|
|
|
18 |
|
|
```c++
|
19 |
|
|
Nan::MaybeLocal<v8::Value> Nan::JSON::Parse(v8::Local<v8::String> json_string);
|
20 |
|
|
```
|
21 |
|
|
|
22 |
|
|
Use `JSON.Parse(json_string)` to parse a string into a `v8::Value`.
|
23 |
|
|
|
24 |
|
|
Example:
|
25 |
|
|
|
26 |
|
|
```c++
|
27 |
|
|
v8::Local<v8::String> json_string = Nan::New("{ \"JSON\": \"object\" }").ToLocalChecked();
|
28 |
|
|
|
29 |
|
|
Nan::JSON NanJSON;
|
30 |
|
|
Nan::MaybeLocal<v8::Value> result = NanJSON.Parse(json_string);
|
31 |
|
|
if (!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 |
|
|
|
40 |
|
|
A simple wrapper around [`v8::JSON::Stringify`](https://v8docs.nodesource.com/node-8.11/da/d6f/classv8_1_1_j_s_o_n.html#a44b255c3531489ce43f6110209138860).
|
41 |
|
|
|
42 |
|
|
Definition:
|
43 |
|
|
|
44 |
|
|
```c++
|
45 |
|
|
Nan::MaybeLocal<v8::String> Nan::JSON::Stringify(v8::Local<v8::Object> json_object, v8::Local<v8::String> gap = v8::Local<v8::String>());
|
46 |
|
|
```
|
47 |
|
|
|
48 |
|
|
Use `JSON.Stringify(value)` to stringify a `v8::Object`.
|
49 |
|
|
|
50 |
|
|
Example:
|
51 |
|
|
|
52 |
|
|
```c++
|
53 |
|
|
// using `v8::Local<v8::Value> val` from the `JSON::Parse` example
|
54 |
|
|
v8::Local<v8::Object> obj = Nan::To<v8::Object>(val).ToLocalChecked();
|
55 |
|
|
|
56 |
|
|
Nan::JSON NanJSON;
|
57 |
|
|
Nan::MaybeLocal<v8::String> result = NanJSON.Stringify(obj);
|
58 |
|
|
if (!result.IsEmpty()) {
|
59 |
|
|
v8::Local<v8::String> stringified = result.ToLocalChecked();
|
60 |
|
|
}
|
61 |
|
|
```
|