Skip to content

Commit 7c66561

Browse files
author
Gancho Radkov
committed
fix: avoids matching bigint when not single value of an object
1 parent abdd40b commit 7c66561

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

misc/safe-json/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ const JSONParse = (json: string) => {
2424
*/
2525
// prettier-ignore
2626
// eslint-disable-next-line no-useless-escape
27-
const numbersBiggerThanMaxInt = `/([\[:])?(?:(?!"[^"]*")(\d{17,}|(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))([,\}\]])/g;`;
28-
29-
const serializedData = json.replace(numbersBiggerThanMaxInt, `$1"$2n"$3`);
30-
27+
const numbersBiggerThanMaxInt = /(?<!")(?<=:)\b(\d{17,}|(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=[,\}\]]|$)/g;
28+
const serializedData = json.replace(numbersBiggerThanMaxInt, (match) => `"${match}n"`);
3129
return JSON.parse(serializedData, (_, value) => {
3230
const isCustomFormatBigInt = typeof value === "string" && value.match(/^\d+n$/);
3331

misc/safe-json/test/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ describe("@walletconnect/safe-json", () => {
4242
const result = safeJsonParse(nested);
4343
chai.expect(result).to.deep.eq(JSON.parse(nested));
4444
});
45+
46+
it("should handle BigInt", () => {
47+
const bigIntId = "1702044452707006208";
48+
const data = `{"id":${bigIntId},"jsonrpc":"2.0","result":"cb0072aaf3f3aab9b5a334f3a273634a2a6d2bee5fe7be205dc9f30f032c9734"}`;
49+
const result = safeJsonParse(data);
50+
chai.expect(result.id).to.deep.eq(BigInt(bigIntId));
51+
});
4552
});
4653
describe("safeJsonStringify", () => {
4754
it("should return a stringfied json", () => {

0 commit comments

Comments
 (0)