1.0.1 • Published 3 years ago

true-json-bigint v1.0.1

Weekly downloads
1,108
License
MIT
Repository
github
Last release
3 years ago

true-json-bigint

Build Status NPM

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.

  1. 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.
  2. 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.

1.0.1

3 years ago

0.4.9

3 years ago

1.0.0

3 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago