Skip to content

Fix BigInt precision loss in parseWithBigInt for JSON with nested escaped JSON strings#1

Merged
wjfz merged 2 commits intomasterfrom
copilot/fix-json-formatting-precision
Mar 6, 2026
Merged

Fix BigInt precision loss in parseWithBigInt for JSON with nested escaped JSON strings#1
wjfz merged 2 commits intomasterfrom
copilot/fix-json-formatting-precision

Conversation

Copy link

Copilot AI commented Mar 6, 2026

parseWithBigInt in index.js blindly regex-replaces all 16+ digit integers with __BigInt__ markers, including those inside JSON string values. When the input contains nested escaped JSON (e.g. a rawBody field), this inserts unescaped quotes into string values, corrupting the JSON. JSON.parse then fails and the fallback eval path loses BigInt precision.

Input:  {"data":{"account_id":1401004938049356981},"rawBody":"{\"account_id\":1401004938049356981}"}
Output: account_id becomes 1401004938049357000
  • Add string-context awareness to the BigInt marking regex in parseWithBigInt (index.js): scan for unescaped quotes up to the match offset to skip numbers inside JSON strings. This is the same approach already used in json-worker.js and format-lib.js.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…d escaped JSON strings

The regex that marks large integers (16+ digits) for BigInt handling was blindly
replacing all matches, including numbers inside JSON string values. This corrupted
the JSON structure (inserting unescaped quotes inside strings), causing JSON.parse
to fail and fall back to eval-based parsing which loses integer precision.

Fix: Add an offset-based string context check (consistent with json-worker.js and
format-lib.js) that tracks whether the match position is inside a JSON string by
scanning for unescaped quote characters. Numbers inside strings are left untouched.

Co-authored-by: wjfz <3372236+wjfz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix precision issues in formatted JSON with escaped strings Fix BigInt precision loss in parseWithBigInt for JSON with nested escaped JSON strings Mar 6, 2026
Copilot finished work on behalf of wjfz March 6, 2026 09:28
@wjfz wjfz marked this pull request as ready for review March 6, 2026 09:35
@wjfz wjfz merged commit 7e0a2aa into master Mar 6, 2026
@wjfz wjfz deleted the copilot/fix-json-formatting-precision branch March 6, 2026 09:36
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.

2 participants