Skip to content

fix!: throw on serializing bigints - #372

Merged
jordanbtucker merged 2 commits into
json5:mainfrom
jordanbtucker:bigint
Oct 19, 2026
Merged

jordanbtucker merged 2 commits into
json5:mainfrom
jordanbtucker:bigint

Conversation

@jordanbtucker

Copy link
Copy Markdown
Member

stringify silently dropped BigInts. JSON5.stringify(1n) returned undefined, and JSON5.stringify({a: 1n}) returned {}. JSON.stringify throws a TypeError in both cases, and JSON5 should do the same instead of quietly losing data.

This PR makes stringify throw TypeError: Do not know how to serialize a BigInt when it encounters a BigInt or a BigInt object (Object(1n)). toJSON, toJSON5 and replacer functions still run first, so BigInts can still be serialized by converting them, for example with a replacer that returns String(value).

This is a breaking change for v3.

@43081j

43081j commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

can't approve but have reviewed and looks good to me 👍

@jordanbtucker
jordanbtucker merged commit 0c21d87 into json5:main Oct 19, 2026
3 checks passed
@jordanbtucker
jordanbtucker deleted the bigint branch October 19, 2026 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants