Skip to content

feat: add opt-in bigint option to parse - #408

Merged
jordanbtucker merged 2 commits into
json5:mainfrom
bybraveHQ:bigint-parse
Oct 24, 2026
Merged

jordanbtucker merged 2 commits into
json5:mainfrom
bybraveHQ:bigint-parse

Conversation

@bybraveHQ

Copy link
Copy Markdown
Collaborator

Closes #292. Follow-up to #370 as discussed there; the stringify side was settled in #372.

parse accepts an options object, either as the third argument after the reviver or as the second argument in its place:

JSON5.parse('12345678901234567890')                  // 12345678901234567000
JSON5.parse('12345678901234567890', {bigint: true})  // 12345678901234567890n
JSON5.parse(text, reviver, {bigint: true})

With bigint: true, integer literals outside the safe integer range (decimal and hexadecimal, with or without a sign) are returned as BigInts. Safe integers and non-integer literals (fractions, exponents, Infinity, NaN) are still numbers, so existing documents keep parsing to the same values and only the lossy cases change type. Without the option nothing changes.

Option handling follows stringify: null/undefined means the default, a non-boolean value throws a TypeError, and an array in the reviver position is not treated as options.

stringify throws on BigInts since #372, so the README points to a replacer (typeof value === 'bigint' ? String(value) : value) for the way back.

Includes types (ParseOptions, two parse overloads), tests for the boundaries at ±2^53, hex, signs, nested values, reviver interplay and option validation, README and CHANGELOG entries. npm test passes with 100% coverage, npm run test-types and npm run lint pass.

One design choice worth a look: the type of a parsed value depends on its magnitude ([1, 9007199254740993] gives [number, bigint]). That keeps the option non-breaking for safe integers, which is what #292 asks for. If you would rather have every integer literal become a BigInt when the option is on, that is a one-line change in integerLiteral() and I will adjust.

@jordanbtucker jordanbtucker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is clean and well tested. I ran the edge cases (±2^53 in decimal and hex, signs, -0/-0x0, exponents, .5, Infinity/NaN, very long literals) and the benchmark, and the default path is unchanged.

On the design question: let's keep unsafe-only. With the option on, {port: 8080} should still give you a number. Making every integer a BigInt would make the option painful for ordinary config, and it's the same trade-off json-bigint made with its opt-in alwaysParseAsBig. People who need predictable types per field will be able to use the reviver source from #386 ((k, v, {source}) => k === 'id' ? BigInt(source) : v), which matches native JSON.parse. Since a non-boolean bigint throws, we can still add something like bigint: 'all' later without a breaking change.

One wording suggestion inline. Also a heads-up that #386 touches the top of parse() and index.d.ts too, so one of the two will need a small rebase.

Comment thread README.md Resolved
Comment thread lib/index.d.ts Resolved
@bybraveHQ

Copy link
Copy Markdown
Collaborator Author

Applied both wording suggestions in 5426cdb: the README and the index.d.ts hover doc now define integer literals by syntax and spell out the default and the TypeError case.

Agreed on keeping it unsafe-only. The code is unchanged, and bigint: 'all' stays open as a non-breaking follow-up.

On the overlap with #386: happy to rebase this one once #386 lands.

npm test (100% coverage) and npm run test-types pass.

@jordanbtucker
jordanbtucker merged commit 1ecc012 into json5:main Oct 24, 2026
4 checks passed
@jordanbtucker

Copy link
Copy Markdown
Member

Thanks! Looks good. I'll merge this first, so no need to wait on #386.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Integers outside the range [-(2**53)+1, (2**53)-1]

2 participants