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.
7
+
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
+
9
+
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.
8
10
9
11
While most JSON parsers assume numeric values have same precision restrictions as IEEE 754 double, JSON specification _does not_ say anything about number precision. Any floating point number in decimal (optionally scientific) notation is valid JSON value. It's a good idea to serialize values which might fall out of IEEE 754 integer precision as strings in your JSON api, but `{ "value" : 9223372036854775807}`, for example, is still a valid RFC4627 JSON string, and in most JS runtimes the result of `JSON.parse` is this object: `{ value: 9223372036854776000 }`
Specifies if all numbers should be stored as BigNumber.
138
+
139
+
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
+
141
+
example:
142
+
```js
143
+
var JSONbig =require('json-bigint');
144
+
var JSONbigAlways =require('json-bigint')({"alwaysParseAsBig":true});
145
+
var key ='{ "key": 123 }'; // there is no need for BigNumber by default, but we're forcing it
146
+
console.log(`\n\nStoring the Number as a BigNumber, instead of a Number`);
147
+
console.log('Input:', key);
148
+
var normal =JSONbig.parse(key);
149
+
var always =JSONbigAlways.parse(key);
150
+
console.log('Default type: %s, With option type: %s', typeofnormal.key, typeofalways.key);
151
+
152
+
```
153
+
154
+
Output
155
+
```
156
+
Storing the Number as a BigNumber, instead of a Number
157
+
Input: { "key": 123 }
158
+
Default type: number, With option type: object
159
+
160
+
```
161
+
162
+
If you want to force all numbers to be parsed as native `BigInt`
163
+
(you probably do! Otherwise any calulations become a real headache):
164
+
```js
165
+
var JSONbig =require('json-bigint')({"alwaysParseAsBig":true, "useNativeBigInt":true});
166
+
```
110
167
111
168
### Links:
112
169
-[RFC4627: The application/json Media Type for JavaScript Object Notation (JSON)](http://www.ietf.org/rfc/rfc4627.txt)
-[What is JavaScript's Max Int? What's the highest Integer value a Number can go to without losing precision?](http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t)
116
173
-[Large numbers erroneously rounded in Javascript](http://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript)
117
174
175
+
### Note on native BigInt support
176
+
177
+
#### Stringifying
178
+
Full support out-of-the-box, stringifies BigInts as pure numbers (no quotes, no `n`)
179
+
180
+
#### Limitations
181
+
- Roundtrip operations
182
+
183
+
`s === JSONbig.stringify(JSONbig.parse(s))` but
184
+
185
+
`o !== JSONbig.parse(JSONbig.stringify(o))`
186
+
187
+
when `o` has a value with something like `123n`.
188
+
189
+
`JSONbig` stringify `123n` as `123`, which becomes `number` (aka `123` not `123n`) by default when being reparsed.
190
+
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.
0 commit comments