1 | json-bigint
|
2 | ===========
|
3 |
|
4 | 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.
|
5 |
|
6 | 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 }`
|
7 |
|
8 | ==========
|
9 |
|
10 | example:
|
11 |
|
12 | ```js
|
13 | var JSONbig = require('json-bigint');
|
14 |
|
15 | var json = '{ "value" : 9223372036854775807, "v2": 123 }';
|
16 | console.log('Input:', json);
|
17 | console.log('');
|
18 |
|
19 | console.log('node.js bult-in JSON:')
|
20 | var r = JSON.parse(json);
|
21 | console.log('JSON.parse(input).value : ', r.value.toString());
|
22 | console.log('JSON.stringify(JSON.parse(input)):', JSON.stringify(r));
|
23 |
|
24 | console.log('\n\nbig number JSON:');
|
25 | var r1 = JSONbig.parse(json);
|
26 | console.log('JSON.parse(input).value : ', r1.value.toString());
|
27 | console.log('JSON.stringify(JSON.parse(input)):', JSONbig.stringify(r1));
|
28 | ```
|
29 |
|
30 | Output:
|
31 |
|
32 | ```
|
33 | Input: { "value" : 9223372036854775807, "v2": 123 }
|
34 |
|
35 | node.js bult-in JSON:
|
36 | JSON.parse(input).value : 9223372036854776000
|
37 | JSON.stringify(JSON.parse(input)): {"value":9223372036854776000,"v2":123}
|
38 |
|
39 |
|
40 | big number JSON:
|
41 | JSON.parse(input).value : 9223372036854775807
|
42 | JSON.stringify(JSON.parse(input)): {"value":9223372036854775807,"v2":123}
|
43 | ```
|
44 |
|
45 | ### Links:
|
46 | - [RFC4627: The application/json Media Type for JavaScript Object Notation (JSON)](http://www.ietf.org/rfc/rfc4627.txt)
|
47 | - [Re: \[Json\] Limitations on number size?](http://www.ietf.org/mail-archive/web/json/current/msg00297.html)
|
48 | - [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)
|
49 | - [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)
|
50 | - [Large numbers erroneously rounded in Javascript](http://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript)
|
51 |
|
52 |
|
53 | [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/sidorares/json-bigint/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
|
54 |
|