1.0.1 • Published 3 years ago
true-json-bigint v1.0.1
true-json-bigint
This is a module for parsing JSON strings to JSON objects and stringifying JSON objects to JSON strings. It is a fork of json-bigint 0.3.0, but enhances the original module in the following aspects.
- It is possible to parse JSON strings containing numeric values larger than 1.797693134862315E+308. The module json-bigint 0.3.0 will throw the error "Bad number" in such cases.
- This module has a proper support for scientific notation. The original module will parse values like 1e+100 to numeric values but not to big number objects as its string representation has less than 15 characters. This handling results in an inapproriate casting of integer values that are larger than 253 - 1 and written in scientific notation. With true-json-bigint the length of the floating point representation will be used to determine the length of the number. This change ensures that all numeric values written in scientific notation will be parsed to big number objects when necessary.
The module generally works in the same way as the original one. Use the following command to add true-json-bigint into the node_moduels directory of your application:
$ npm install true-json-bigint
Examples
Parse JSON strings as described in the next example:
const JSONbig = require('true-json-bigint');
const json = '{"smallNumber": 1, "bigNumber" : 987654321123456789987654321}';
const result = JSONbig.parse(json);
console.log(result);
// { smallNumber: 1, bigNumber: BigNumber { s: 1, e: 26, c: [ 9876543211234, 56789987654321 ] } }
As can be seen in the following example, stringifying a JSON object that contains a big number can easily be done by using the stringify function of this module.
const JSONbig = require('true-json-bigint');
const BigNumber = require('bignumber.js');
const json = {}
json.smallNumber = 1
json.bigNumber = BigNumber('987654321123456789987654321');
const result = JSONbig.stringify(json);
console.log(result);
// {"smallNumber":1,"bigNumber":9.87654321123456789987654321e+26}
See the README of json-bigint 0.3.0 for further details on how to use this module.