json-bigint
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
bignumber.js | 9.1.2 | 77.37 kB | MIT | prod |
json-bigint is a powerful JavaScript library that provides a JSON.parse/stringify alternative with bigints support. While standard JSON parsers assume numeric values have the same precision restrictions as IEEE 754 double, json-bigint defies this assumption, recognizing that the JSON specification does not place any restrictions on number precision. It is particularly useful when dealing with large numeric values that would exceed the precision of an IEEE 754 integer, such as { "value" : 9223372036854775807}
. With json-bigint, these large values can be parsed accurately, effectively avoiding precision loss which is common in most JavaScript runtimes.
To use the json-bigint package, you first need to import the library using the require
function. The sample code to parse and stringify JSON using json-bigint looks as follows:
var JSONbig = require('json-bigint');
var json = '{ "value" : 9223372036854775807, "v2": 123 }';
console.log('Input:', json);
console.log('');
console.log('node.js built-in JSON:');
var r = JSON.parse(json);
console.log('JSON.parse(input).value : ', r.value.toString());
console.log('JSON.stringify(JSON.parse(input)):', JSON.stringify(r));
console.log('\n\nbig number JSON:');
var r1 = JSONbig.parse(json);
console.log('JSONbig.parse(input).value : ', r1.value.toString());
console.log('JSONbig.stringify(JSONbig.parse(input)):', JSONbig.stringify(r1));
The library also offers several options to customize its behavior. These include settings for strict parsing, storing BigInts as a string, using native BigInt in place of bignumber.js library, always parsing numbers as BigInt, and more. The usage of options is illustrated clearly in the example
sections providing code snippets with different options set.
For comprehensive information on json-bigint, refer to the README available on its GitHub repository: json-bigint on GitHub. It contains descriptions of the package's functionality and usage, along with numerous examples. The README also includes links to related resources such as the RFC4627 JSON specification and discussions on the handling of large numbers in JavaScript.
The package doesn't appear to have separate documentation apart from the README, but considering the precise explanations and examples provided, the README should serve effectively as its documentation.