From 116ee92eab749d546da8667db58a1549c7ddbadc Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sat, 27 Sep 2025 18:20:42 +0200 Subject: [PATCH] fix(JSON Bigint parser): better handle large numbers in strings --- src/utils/json5-bigint.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils/json5-bigint.ts b/src/utils/json5-bigint.ts index 670d368c..15d3eed4 100644 --- a/src/utils/json5-bigint.ts +++ b/src/utils/json5-bigint.ts @@ -1,8 +1,18 @@ import JSON5 from 'json5'; function quoteLargeNumbers(jsonStr: string, minDigits: number): string { + const strings: string[] = []; + // Replace each string with its index + const jsonWithoutStrings = jsonStr.replace(/"(?:\\.|[^"\\])*"/g, (str) => { + const replacement = `"@${strings.length}@"`; + strings.push(str); + return replacement; + }); const regex = new RegExp(`(-?\\d{${minDigits},})(?=\\s*[,}\\]])`, 'g'); - return jsonStr.replace(regex, '"$1"'); + const jsonQuotedBigint = jsonWithoutStrings.replace(regex, '"$1"'); + return jsonQuotedBigint.replace(/"@(\d+)@"/g, (_, ix) => { + return strings[Number(ix)]; + }); } JSON.parseBigInt = function (jsonStr: string, options?: { minDigits?: number; fallbackToString?: boolean }): unknown {