Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

true-json-bigint

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

true-json-bigint

Enhanced fork of json-bigint that allows parsing all big number values in JSON strings.

  • 0.4.9
  • update_0.x.x
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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.

Keywords

FAQs

Package last updated on 26 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc