|
4 | 4 | [](https://coveralls.io/github/tuftjs/json-stringify?branch=master) |
5 | 5 | [](https://snyk.io/test/github/tuftjs/json-stringify?targetFile=package.json) |
6 | 6 |
|
7 | | -A JSON library for Node.js that exports the following functions: |
| 7 | +A JSON library for Node.js that exports the following set of functions: |
| 8 | + |
8 | 9 | * [jsonStringify()](#jsonstringifyvalue-safe) |
9 | 10 | * [stableJsonStringify()](#stablejsonstringifyvalue-comparefunction-safe) |
10 | 11 |
|
| 12 | +These functions prioritize performance at the expense of certain features. The serialized JSON output of these functions is identical to that of `JSON.stringify()` with the following exceptions: |
| 13 | + |
| 14 | +1. When serializing strings, only quotation marks (`"`) and backslashes (`\`) are escaped. As the vast majority of strings in real world usage do not include ASCII control characters or lone surrogate code points, a significant performance boost is gained by not checking for their presence. If you require this functionality, it is best to stick with `JSON.stringify()` or another alternative. |
| 15 | +2. `toJSON()` methods are completely ignored. However, `Buffer` and `Date` objects *will* be transformed to represent the same output you would get from `JSON.stringify()`. |
| 16 | + |
| 17 | +These functions also do not have *replacer* or *space* parameters like `JSON.stringify()` does. |
| 18 | + |
| 19 | +⚠ **Note:** |
| 20 | +By default, a check will be made on all nested objects and arrays for circular references, and an `Error` will be thrown if one is encountered. Setting the *safe* parameter to `false` will disable this check and grant a considerable speed boost. If a circular reference is found when *safe* is set to `false`, the internal serialization function will be called recursively until the call stack size is exceeded. |
| 21 | + |
11 | 22 | ## Installation |
12 | 23 |
|
13 | 24 | ```bash |
14 | 25 | npm i @tuft/json-stringify |
15 | 26 | ``` |
16 | 27 |
|
17 | | -</br> |
18 | | -<p align="center"> |
19 | | - 🔹 🔹 🔹 |
20 | | -</p> |
21 | | -</br> |
| 28 | +## API |
22 | 29 |
|
23 | 30 | ### jsonStringify(*value*[, *safe*]) |
24 | 31 |
|
25 | | -A more performant version of `JSON.stringify()`. Strings produced by this function are identical (not accounting for the order of keys) to the output of `JSON.stringify()`, and it works the same with the following exceptions: |
26 | | -* There are no *replacer* or *space* parameters. |
27 | | -* There **is** a *safe* parameter (see below). |
28 | | -* `toJSON()` methods are completely ignored. However, `Buffer` and `Date` objects will be transformed to represent the same output you would get from `JSON.stringify()`. |
| 32 | +Converts a JavaScript object or value to a JSON string. The following are valid data types that can be converted to JSON: |
| 33 | + |
| 34 | +* `null` |
| 35 | +* Boolean |
| 36 | +* Number |
| 37 | +* String |
| 38 | +* Array |
| 39 | +* Object |
| 40 | +* `Date`** |
| 41 | +* `Buffer`** |
29 | 42 |
|
30 | | -⚠ **Important note:** |
31 | | -By default a check will be made on all nested objects and arrays for circular references, and an `Error` will be thrown if one is encountered. Setting the *safe* parameter to `false` will disable this check and grant a considerable speed boost, but this should not be done unless it is certain the given object contains no circular references. |
| 43 | +\*\* `Date` and `Buffer` objects are serialized in a way that matches the output of their respective `toJSON()` methods. |
32 | 44 |
|
33 | 45 | #### Parameters |
34 | 46 |
|
@@ -58,15 +70,9 @@ const obj = { |
58 | 70 | jsonStringify(obj); // '{"foo":42,"hello":"world"}' |
59 | 71 | ``` |
60 | 72 |
|
61 | | -</br> |
62 | | -<p align="center"> |
63 | | - 🔹 🔹 🔹 |
64 | | -</p> |
65 | | -</br> |
66 | | - |
67 | 73 | ### stableJsonStringify(*value*,[ *compareFunction*[, *safe*]]) |
68 | 74 |
|
69 | | -A deterministic version of `jsonStringify()`. It performs the same as `jsonStringify()` with the exception that object entries are sorted before being serialized. This ensures that consistent output is produced for the same input, at the cost of reduced performance. |
| 75 | +A deterministic version of `jsonStringify()`. It performs the same, with the exception that object entries are sorted before being serialized. This ensures that consistent output is produced for the same input, at the cost of reduced performance. |
70 | 76 |
|
71 | 77 | #### Parameters |
72 | 78 |
|
@@ -126,8 +132,3 @@ const obj = { |
126 | 132 |
|
127 | 133 | stableJsonStringify(obj); // '{"c":"bar","b":"foo","a":"baz"}' |
128 | 134 | ``` |
129 | | - |
130 | | -</br> |
131 | | -<p align="center"> |
132 | | - 🔹 🔹 🔹 |
133 | | -</p> |
|
0 commit comments