-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements a JSON.parse that transforms unsafe numbers to bigints
- Loading branch information
1 parent
04942c9
commit 86ed7d4
Showing
12 changed files
with
545 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/rpc-transport-http/src/__benchmarks__/json-parse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env -S pnpm dlx tsx -- | ||
|
||
import { Bench } from 'tinybench'; | ||
import { fs, path } from 'zx'; | ||
|
||
import { | ||
jsonParseWithLargeIntegersAsBigInts, | ||
wrapLargeIntegersUsingParser, | ||
wrapLargeIntegersUsingParserAndRegex, | ||
wrapLargeIntegersUsingRegex, | ||
} from '../json-parse-with-bigint'; | ||
|
||
Object.assign(globalThis, { | ||
__BROWSER__: false, | ||
__DEV__: false, | ||
__NODEJS__: true, | ||
__REACTNATIVE____: false, | ||
}); | ||
|
||
const bench = new Bench({ | ||
throws: true, | ||
}); | ||
|
||
const largeJsonPath = path.join(__dirname, '..', '__tests__', 'large-json-file.json'); | ||
const largeJsonString = fs.readFileSync(largeJsonPath, 'utf8'); | ||
|
||
bench | ||
.add('JSON.parse', () => { | ||
return JSON.parse(largeJsonString); | ||
}) | ||
.add('JSON.parse with noop reviver', () => { | ||
return jsonParseWithLargeIntegersAsBigInts(largeJsonString, x => x); | ||
}) | ||
.add('jsonParseWithLargeIntegersAsBigInts (parser)', () => { | ||
return jsonParseWithLargeIntegersAsBigInts(largeJsonString); | ||
}) | ||
.add('jsonParseWithLargeIntegersAsBigInts (parser and regex)', () => { | ||
return jsonParseWithLargeIntegersAsBigInts(largeJsonString, wrapLargeIntegersUsingParserAndRegex); | ||
}) | ||
.add('jsonParseWithLargeIntegersAsBigInts (parser and noop)', () => { | ||
return jsonParseWithLargeIntegersAsBigInts(largeJsonString, x => wrapLargeIntegersUsingParser(x, () => null)); | ||
}) | ||
.add('jsonParseWithLargeIntegersAsBigInts (regex)', () => { | ||
return jsonParseWithLargeIntegersAsBigInts(largeJsonString, wrapLargeIntegersUsingRegex); | ||
}); | ||
|
||
(async () => { | ||
await bench.run(); | ||
console.table(bench.table()); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/rpc-transport-http/src/__tests__/json-parse-with-bigint-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { jsonParseWithLargeIntegersAsBigInts, wrapLargeIntegers } from '../json-parse-with-bigint'; | ||
|
||
const MAX_SAFE_INTEGER: number = Number.MAX_SAFE_INTEGER; | ||
const MAX_SAFE_INTEGER_PLUS_ONE: bigint = BigInt(Number.MAX_SAFE_INTEGER) + 1n; | ||
|
||
describe('jsonParseWithLargeNumbersAsBigInts', () => { | ||
it('parses large integers as bigints', () => { | ||
const result = jsonParseWithLargeIntegersAsBigInts(`{"value":${MAX_SAFE_INTEGER_PLUS_ONE}}`) as { | ||
value: unknown; | ||
}; | ||
expect(result).toEqual({ value: MAX_SAFE_INTEGER_PLUS_ONE }); | ||
expect(typeof result.value).toBe('bigint'); | ||
}); | ||
it('keeps safe integers as numbers', () => { | ||
const result = jsonParseWithLargeIntegersAsBigInts(`{"value":${MAX_SAFE_INTEGER}}`) as { value: unknown }; | ||
expect(result).toEqual({ value: MAX_SAFE_INTEGER }); | ||
expect(typeof result.value).toBe('number'); | ||
}); | ||
it('does not affect numbers in strings', () => { | ||
expect(jsonParseWithLargeIntegersAsBigInts(`{"value":"Hello 123 World"}`)).toEqual({ | ||
value: 'Hello 123 World', | ||
}); | ||
}); | ||
it('does not affect numbers in strings with escaped double quotes', () => { | ||
expect(jsonParseWithLargeIntegersAsBigInts(`{"value":"Hello\\" 123 World"}`)).toEqual({ | ||
value: 'Hello" 123 World', | ||
}); | ||
}); | ||
it('parses large integers with exponents as bigints', () => { | ||
expect(jsonParseWithLargeIntegersAsBigInts(`1e32`)).toBe(100000000000000000000000000000000n); | ||
expect(jsonParseWithLargeIntegersAsBigInts(`-1189e+32`)).toBe(-118900000000000000000000000000000000n); | ||
}); | ||
it('does not affect negative exponents', () => { | ||
expect(jsonParseWithLargeIntegersAsBigInts(`1e-32`)).toBe(1e-32); | ||
expect(jsonParseWithLargeIntegersAsBigInts(`-1189e-32`)).toBe(-1189e-32); | ||
}); | ||
it('can parse complex JSON files', () => { | ||
const largeJsonPath = path.join(__dirname, 'large-json-file.json'); | ||
const largeJsonString = fs.readFileSync(largeJsonPath, 'utf8'); | ||
const expectedResult = JSON.parse(largeJsonString, (key, value) => | ||
// eslint-disable-next-line jest/no-conditional-in-test | ||
key === 'lamports' ? 142302234983644260n : value, | ||
); | ||
expect(jsonParseWithLargeIntegersAsBigInts(largeJsonString)).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('wrapLargeIntegers', () => { | ||
it('wraps large integers as bigint strings', () => { | ||
expect(wrapLargeIntegers(`{"value":${MAX_SAFE_INTEGER_PLUS_ONE}}`)).toBe( | ||
`{"value":{"$n":"${MAX_SAFE_INTEGER_PLUS_ONE}"}}`, | ||
); | ||
}); | ||
it('keeps safe integers as numbers', () => { | ||
expect(wrapLargeIntegers(`{"value":${MAX_SAFE_INTEGER}}`)).toBe(`{"value":${MAX_SAFE_INTEGER}}`); | ||
}); | ||
it('does not wrap numbers in strings', () => { | ||
expect(wrapLargeIntegers(`{"value":"Hello 123 World"}`)).toBe(`{"value":"Hello 123 World"}`); | ||
}); | ||
it('does not wrap numbers in strings with escaped double quotes', () => { | ||
expect(wrapLargeIntegers(`{"value":"Hello\\" 123 World"}`)).toBe(`{"value":"Hello\\" 123 World"}`); | ||
}); | ||
it('does not alter special keywords', () => { | ||
expect(wrapLargeIntegers('true')).toBe('true'); | ||
expect(wrapLargeIntegers('false')).toBe('false'); | ||
expect(wrapLargeIntegers('null')).toBe('null'); | ||
}); | ||
it('does not alter safe numbers', () => { | ||
expect(wrapLargeIntegers('0')).toBe('0'); | ||
expect(wrapLargeIntegers('123456')).toBe('123456'); | ||
expect(wrapLargeIntegers('-123456')).toBe('-123456'); | ||
expect(wrapLargeIntegers('3.14')).toBe('3.14'); | ||
expect(wrapLargeIntegers('-3.14')).toBe('-3.14'); | ||
expect(wrapLargeIntegers('1e5')).toBe('1e5'); | ||
expect(wrapLargeIntegers('-1e5')).toBe('-1e5'); | ||
expect(wrapLargeIntegers('1E5')).toBe('1E5'); | ||
expect(wrapLargeIntegers('-1E5')).toBe('-1E5'); | ||
expect(wrapLargeIntegers(`${MAX_SAFE_INTEGER}`)).toBe(`${MAX_SAFE_INTEGER}`); | ||
expect(wrapLargeIntegers(`-${MAX_SAFE_INTEGER}`)).toBe(`-${MAX_SAFE_INTEGER}`); | ||
}); | ||
it('wraps unsafe large integers', () => { | ||
expect(wrapLargeIntegers('1e32')).toBe('{"$n":"1e32"}'); | ||
expect(wrapLargeIntegers('-1e32')).toBe('{"$n":"-1e32"}'); | ||
expect(wrapLargeIntegers('1E32')).toBe('{"$n":"1E32"}'); | ||
expect(wrapLargeIntegers('-1E32')).toBe('{"$n":"-1E32"}'); | ||
expect(wrapLargeIntegers(`${MAX_SAFE_INTEGER_PLUS_ONE}`)).toBe(`{"$n":"${MAX_SAFE_INTEGER_PLUS_ONE}"}`); | ||
expect(wrapLargeIntegers(`-${MAX_SAFE_INTEGER_PLUS_ONE}`)).toBe(`{"$n":"-${MAX_SAFE_INTEGER_PLUS_ONE}"}`); | ||
}); | ||
it('does not alter unsafe large floating points', () => { | ||
expect(wrapLargeIntegers('3.14e32')).toBe('3.14e32'); | ||
expect(wrapLargeIntegers('-3.14e32')).toBe('-3.14e32'); | ||
expect(wrapLargeIntegers('3.14E32')).toBe('3.14E32'); | ||
expect(wrapLargeIntegers('-3.14E32')).toBe('-3.14E32'); | ||
expect(wrapLargeIntegers(`${MAX_SAFE_INTEGER_PLUS_ONE}.123`)).toBe(`${MAX_SAFE_INTEGER_PLUS_ONE}.123`); | ||
expect(wrapLargeIntegers(`-${MAX_SAFE_INTEGER_PLUS_ONE}.123`)).toBe(`-${MAX_SAFE_INTEGER_PLUS_ONE}.123`); | ||
}); | ||
it('does not alter strings', () => { | ||
expect(wrapLargeIntegers('""')).toBe('""'); | ||
expect(wrapLargeIntegers('"Hello World"')).toBe('"Hello World"'); | ||
}); | ||
}); |
Oops, something went wrong.