true-json-bigint
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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 and covers all features of version 1.0.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 1.0.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 1.0.0 for further details on how to use this module.

Package Sidebar

Install

npm i true-json-bigint

Weekly Downloads

1,404

Version

1.0.1

License

MIT

Unpacked Size

42.2 kB

Total Files

15

Last publish

Collaborators

  • sebastiang77