-
Notifications
You must be signed in to change notification settings - Fork 79
Remove json-bigint dependency #1305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -298,6 +298,67 @@ function compareVersions(version1, version2, operator) { | |||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Parse JSON string with support for large integers (beyond Number.MAX_SAFE_INTEGER). | ||||||||||||||||||||||||||||||||||||||||||
| * Large integers are automatically converted to strings to preserve precision. | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * For Node.js >= 21.0.0, uses native JSON.parse() with context parameter. | ||||||||||||||||||||||||||||||||||||||||||
| * For older versions, preprocesses the JSON string with regex to convert unsafe integers. | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * Replaces: json-bigint package | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * @param {string} str - JSON string to parse | ||||||||||||||||||||||||||||||||||||||||||
| * @returns {any} Parsed JSON object with unsafe integers as strings | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * @example | ||||||||||||||||||||||||||||||||||||||||||
| * parseJSONWithBigInt('{"value": 9007199254740992}') | ||||||||||||||||||||||||||||||||||||||||||
| * // Returns: { value: '9007199254740992' } | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * parseJSONWithBigInt('{"value": 100}') | ||||||||||||||||||||||||||||||||||||||||||
| * // Returns: { value: 100 } | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| function parseJSONWithBigInt(str) { | ||||||||||||||||||||||||||||||||||||||||||
| const contextSupportedVersion = '21.0.0.0'; | ||||||||||||||||||||||||||||||||||||||||||
| const currentVersion = process.version; | ||||||||||||||||||||||||||||||||||||||||||
| const isContextSupported = compareVersions( | ||||||||||||||||||||||||||||||||||||||||||
| currentVersion.substring(1, currentVersion.length), | ||||||||||||||||||||||||||||||||||||||||||
| contextSupportedVersion, | ||||||||||||||||||||||||||||||||||||||||||
| '>=' | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // For nodejs >= 21.0.0, we leverage context parameter to | ||||||||||||||||||||||||||||||||||||||||||
| // convert unsafe integer to string. | ||||||||||||||||||||||||||||||||||||||||||
| // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse | ||||||||||||||||||||||||||||||||||||||||||
| if (isContextSupported) { | ||||||||||||||||||||||||||||||||||||||||||
| return JSON.parse(str, (key, value, context) => { | ||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||
| Number.isInteger(value) && | ||||||||||||||||||||||||||||||||||||||||||
| !Number.isSafeInteger(Number(context.source)) | ||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||
| return context.source; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // For older Node.js versions, preprocess the JSON string to convert | ||||||||||||||||||||||||||||||||||||||||||
| // large integers (outside the safe integer range) to strings before parsing. | ||||||||||||||||||||||||||||||||||||||||||
| // This replaces the functionality of json-bigint package. | ||||||||||||||||||||||||||||||||||||||||||
| const processed = str.replace( | ||||||||||||||||||||||||||||||||||||||||||
| /:\s*(-?\d+)(?=\s*[,}\]])/g, | ||||||||||||||||||||||||||||||||||||||||||
| (match, number) => { | ||||||||||||||||||||||||||||||||||||||||||
| const num = Number(number); | ||||||||||||||||||||||||||||||||||||||||||
| // If the number is not safe, keep it as a string | ||||||||||||||||||||||||||||||||||||||||||
| if (!Number.isSafeInteger(num)) { | ||||||||||||||||||||||||||||||||||||||||||
| return `: "${number}"`; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return match; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return JSON.parse(processed); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
358
to
382
|
||||||||||||||||||||||||||||||||||||||||||
| const processed = str.replace( | |
| /:\s*(-?\d+)(?=\s*[,}\]])/g, | |
| (match, number) => { | |
| const num = Number(number); | |
| // If the number is not safe, keep it as a string | |
| if (!Number.isSafeInteger(num)) { | |
| return `: "${number}"`; | |
| } | |
| return match; | |
| } | |
| ); | |
| return JSON.parse(processed); | |
| // Use a reviver to convert unsafe integers to strings | |
| return JSON.parse(str, (key, value) => { | |
| if (typeof value === 'number' && Number.isInteger(value) && !Number.isSafeInteger(value)) { | |
| return value.toString(); | |
| } | |
| return value; | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version comparison is executed on every call to
parseJSONWithBigInt. Consider cachingisContextSupportedas a module-level constant to avoid repeated comparisons, similar to how it was implemented in the original code.