You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JSON.parse/stringify with bigints support. Based on Douglas Crockford [JSON.js](https://github.com/douglascrockford/JSON-js) package and [bignumber.js](https://github.com/MikeMcl/bignumber.js) library.
6
+
JSON.parse/stringify with bigints support. Based on Douglas Crockford [JSON.js](https://github.com/douglascrockford/JSON-js) package and [bignumber.js](https://github.com/MikeMcl/bignumber.js) library.
8
7
9
8
Native `Bigint` was added to JS recently, so we added an option to leverage it instead of `bignumber.js`. However, the parsing with native `BigInt` is kept an option for backward compability.
The behaviour of the parser is somewhat configurable through 'options'
51
52
52
53
#### options.strict, boolean, default false
54
+
53
55
Specifies the parsing should be "strict" towards reporting duplicate-keys in the parsed string.
54
56
The default follows what is allowed in standard json and resembles the behavior of JSON.parse, but overwrites any previous values with the last one assigned to the duplicate-key.
55
57
56
58
Setting options.strict = true will fail-fast on such duplicate-key occurances and thus warn you upfront of possible lost information.
57
59
58
60
example:
61
+
59
62
```js
60
63
var JSONbig =require('json-bigint');
61
-
var JSONstrict =require('json-bigint')({"strict":true});
64
+
var JSONstrict =require('json-bigint')({strict:true});
62
65
63
66
var dupkeys ='{ "dupkey": "value 1", "dupkey": "value 2"}';
64
67
console.log('\n\nDuplicate Key test with both lenient and strict JSON parsing');
Specifies if BigInts should be stored in the object as a string, rather than the default BigNumber.
88
96
89
97
Note that this is a dangerous behavior as it breaks the default functionality of being able to convert back-and-forth without data type changes (as this will convert all BigInts to be-and-stay strings).
90
98
91
99
example:
100
+
92
101
```js
93
102
var JSONbig =require('json-bigint');
94
-
var JSONbigString =require('json-bigint')({"storeAsString":true});
103
+
var JSONbigString =require('json-bigint')({storeAsString:true});
95
104
var key ='{ "key": 1234567890123456789 }';
96
105
console.log('\n\nStoring the BigInt as a string, instead of a BigNumber');
97
106
console.log('Input:', key);
98
107
var withInt =JSONbig.parse(key);
99
108
var withString =JSONbigString.parse(key);
100
-
console.log('Default type: %s, With option type: %s', typeofwithInt.key, typeofwithString.key);
101
-
109
+
console.log(
110
+
'Default type: %s, With option type: %s',
111
+
typeofwithInt.key,
112
+
typeofwithString.key
113
+
);
102
114
```
103
115
104
116
Output
117
+
105
118
```
106
119
Storing the BigInt as a string, instead of a BigNumber
Specifies if all numbers should be stored as BigNumber.
138
158
139
159
Note that this is a dangerous behavior as it breaks the default functionality of being able to convert back-and-forth without data type changes (as this will convert all Number to be-and-stay BigNumber)
140
160
141
161
example:
162
+
142
163
```js
143
164
var JSONbig =require('json-bigint');
144
-
var JSONbigAlways =require('json-bigint')({"alwaysParseAsBig":true});
165
+
var JSONbigAlways =require('json-bigint')({alwaysParseAsBig:true});
145
166
var key ='{ "key": 123 }'; // there is no need for BigNumber by default, but we're forcing it
146
167
console.log(`\n\nStoring the Number as a BigNumber, instead of a Number`);
147
168
console.log('Input:', key);
148
169
var normal =JSONbig.parse(key);
149
170
var always =JSONbigAlways.parse(key);
150
-
console.log('Default type: %s, With option type: %s', typeofnormal.key, typeofalways.key);
151
-
171
+
console.log(
172
+
'Default type: %s, With option type: %s',
173
+
typeofnormal.key,
174
+
typeofalways.key
175
+
);
152
176
```
153
177
154
178
Output
179
+
155
180
```
156
181
Storing the Number as a BigNumber, instead of a Number
-[RFC4627: The application/json Media Type for JavaScript Object Notation (JSON)](http://www.ietf.org/rfc/rfc4627.txt)
170
217
-[Re: \[Json\] Limitations on number size?](http://www.ietf.org/mail-archive/web/json/current/msg00297.html)
171
218
-[Is there any proper way to parse JSON with large numbers? (long, bigint, int64)](http://stackoverflow.com/questions/18755125/node-js-is-there-any-proper-way-to-parse-json-with-large-numbers-long-bigint)
@@ -175,17 +222,19 @@ var JSONbig = require('json-bigint')({"alwaysParseAsBig": true, "useNativeBigInt
175
222
### Note on native BigInt support
176
223
177
224
#### Stringifying
225
+
178
226
Full support out-of-the-box, stringifies BigInts as pure numbers (no quotes, no `n`)
179
227
180
228
#### Limitations
229
+
181
230
- Roundtrip operations
182
231
183
232
`s === JSONbig.stringify(JSONbig.parse(s))` but
184
233
185
-
`o !== JSONbig.parse(JSONbig.stringify(o))`
234
+
`o !== JSONbig.parse(JSONbig.stringify(o))`
186
235
187
-
when `o` has a value with something like `123n`.
236
+
when `o` has a value with something like `123n`.
188
237
189
-
`JSONbig` stringify `123n` as `123`, which becomes `number` (aka `123` not `123n`) by default when being reparsed.
238
+
`JSONbig` stringify `123n` as `123`, which becomes `number` (aka `123` not `123n`) by default when being reparsed.
190
239
191
-
There is currently no consistent way to deal with this issue, so we decided to leave it, handling this specific case is then up to users.
240
+
There is currently no consistent way to deal with this issue, so we decided to leave it, handling this specific case is then up to users.
0 commit comments