@bybrave/json-bigint2
v2.0.0
Published
Maintained fork of json-bigint — JSON.parse/stringify with big-number support, native BigInt, TypeScript types
Maintainers
Readme
@bybrave/json-bigint2
Maintained fork of json-bigint — JSON.parse / JSON.stringify that preserve numbers beyond Number.MAX_SAFE_INTEGER instead of silently corrupting them.
JSON.parse('{"id":9223372036854775807}').id
// 9223372036854775806 ← corrupted
require('@bybrave/json-bigint2')({ useNativeBigInt: true }).parse('{"id":9223372036854775807}').id
// 9223372036854775807n ← exactWhy this fork
The original json-bigint has not been released since 2020, and the code published on npm is older than its master branch — several fixes were merged upstream but never shipped. @bybrave/json-bigint2 is a drop-in replacement that ships the fixed code plus:
| Change | Upstream issue |
|---|---|
| TypeScript type definitions included | #34 |
| Floats no longer crash useNativeBigInt mode (shipped, was unreleased on npm) | #49, #88 |
| Numbers beyond the double range (3e+500, 400-digit integers) parse instead of throwing Bad number | #25, #26 |
| objectProto option: parsed objects get a normal prototype (hasOwnProperty, toString work) | #39, #64 |
| Strict JSON number grammar: 01, -01, 5., 1e are rejected like JSON.parse does | #19 |
| Parse errors are real SyntaxError instances (stack trace, instanceof Error) | — |
| Zero dev-dependency test suite on node:test, CI on Node 18/20/22 | — |
Install
npm install @bybrave/json-bigint2Usage
const JSONbig = require('@bybrave/json-bigint2')({ useNativeBigInt: true });
const payload = '{"id":9223372036854775807,"name":"deep"}';
const data = JSONbig.parse(payload);
data.id; // 9223372036854775807n
JSONbig.stringify(data); // '{"id":9223372036854775807,"name":"deep"}'Without options, unsafe integers become bignumber.js instances (original behaviour):
const JSONbig = require('@bybrave/json-bigint2');
JSONbig.parse('{"id":9223372036854775807}').id.toFixed(); // '9223372036854775807'TypeScript:
import JSONbig = require('@bybrave/json-bigint2');
const parser = JSONbig({ useNativeBigInt: true, objectProto: true });
const data = parser.parse(payload);Options
| Option | Default | Description |
|---|---|---|
| useNativeBigInt | false | Use native BigInt instead of bignumber.js |
| alwaysParseAsBig | false | Parse every integer as big, not only unsafe ones |
| storeAsString | false | Store big numbers as plain strings |
| strict | false | Throw on duplicate object keys |
| objectProto | false | Parsed objects get Object.prototype instead of a null prototype |
| protoAction | 'error' | "__proto__" key handling: 'error' | 'ignore' | 'preserve' |
| constructorAction | 'error' | "constructor" key handling: 'error' | 'ignore' | 'preserve' |
Notes:
objectProto: trueis safe against prototype pollution: suspicious keys are still guarded byprotoAction/constructorAction, and member assignment never touches the actual prototype.- In
useNativeBigIntmode a number that overflows the double range and keeps a fractional part (e.g.1.55…5e+309) parses asInfinity, mirroringJSON.parse.
Migrating from json-bigint
It is a drop-in replacement:
- const JSONbig = require('json-bigint')({ useNativeBigInt: true });
+ const JSONbig = require('@bybrave/json-bigint2')({ useNativeBigInt: true });The only behavioural differences are the bug fixes listed above (strict number grammar may reject inputs that were never valid JSON, and parse errors are now SyntaxError instances).
Support
If this package saves you time, you can support maintenance:
Bitcoin (BTC): bc1q37557q5jpeaxqydzwvf3jgj7zhnfpn2td3q40q
Credits & license
MIT. Based on json-bigint by Andrey Sidorov, which builds on Douglas Crockford's reference JSON parser.
