Skip to content

Commit 0bf6778

Browse files
committed
Remove json-bigint dependency
1 parent c8f0ae6 commit 0bf6778

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
lines changed

lib/utils.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,67 @@ function compareVersions(version1, version2, operator) {
298298
}
299299
}
300300

301+
/**
302+
* Parse JSON string with support for large integers (beyond Number.MAX_SAFE_INTEGER).
303+
* Large integers are automatically converted to strings to preserve precision.
304+
*
305+
* For Node.js >= 21.0.0, uses native JSON.parse() with context parameter.
306+
* For older versions, preprocesses the JSON string with regex to convert unsafe integers.
307+
*
308+
* Replaces: json-bigint package
309+
*
310+
* @param {string} str - JSON string to parse
311+
* @returns {any} Parsed JSON object with unsafe integers as strings
312+
*
313+
* @example
314+
* parseJSONWithBigInt('{"value": 9007199254740992}')
315+
* // Returns: { value: '9007199254740992' }
316+
*
317+
* parseJSONWithBigInt('{"value": 100}')
318+
* // Returns: { value: 100 }
319+
*/
320+
function parseJSONWithBigInt(str) {
321+
const contextSupportedVersion = '21.0.0.0';
322+
const currentVersion = process.version;
323+
const isContextSupported = compareVersions(
324+
currentVersion.substring(1, currentVersion.length),
325+
contextSupportedVersion,
326+
'>='
327+
);
328+
329+
// For nodejs >= 21.0.0, we leverage context parameter to
330+
// convert unsafe integer to string.
331+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
332+
if (isContextSupported) {
333+
return JSON.parse(str, (key, value, context) => {
334+
if (
335+
Number.isInteger(value) &&
336+
!Number.isSafeInteger(Number(context.source))
337+
) {
338+
return context.source;
339+
}
340+
return value;
341+
});
342+
}
343+
344+
// For older Node.js versions, preprocess the JSON string to convert
345+
// large integers (outside the safe integer range) to strings before parsing.
346+
// This replaces the functionality of json-bigint package.
347+
const processed = str.replace(
348+
/:\s*(-?\d+)(?=\s*[,}\]])/g,
349+
(match, number) => {
350+
const num = Number(number);
351+
// If the number is not safe, keep it as a string
352+
if (!Number.isSafeInteger(num)) {
353+
return `: "${number}"`;
354+
}
355+
return match;
356+
}
357+
);
358+
359+
return JSON.parse(processed);
360+
}
361+
301362
module.exports = {
302363
// General utilities
303364
detectUbuntuCodename,
@@ -320,5 +381,7 @@ module.exports = {
320381
removeSync,
321382
readJsonSync,
322383

384+
// Version and JSON utilities
323385
compareVersions,
386+
parseJSONWithBigInt,
324387
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
"@rclnodejs/ref-struct-di": "^1.1.1",
7878
"bindings": "^1.5.0",
7979
"debug": "^4.4.0",
80-
"json-bigint": "^1.0.0",
8180
"node-addon-api": "^8.3.1",
8281
"walk": "^2.3.15"
8382
},

rosidl_parser/rosidl_parser.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,12 @@
1414

1515
'use strict';
1616

17-
const { compareVersions } = require('../lib/utils.js');
17+
const { compareVersions, parseJSONWithBigInt } = require('../lib/utils');
1818
const path = require('path');
1919
const execFile = require('child_process').execFile;
2020

2121
const pythonExecutable = require('./py_utils').getPythonExecutable('python3');
2222

23-
const contextSupportedVersion = '21.0.0.0';
24-
const currentVersion = process.version;
25-
const isContextSupported = compareVersions(
26-
currentVersion.substring(1, currentVersion.length),
27-
contextSupportedVersion,
28-
'>='
29-
);
30-
3123
const rosidlParser = {
3224
parseMessageFile(packageName, filePath) {
3325
return this._parseFile('parse_message_file', packageName, filePath);
@@ -41,25 +33,6 @@ const rosidlParser = {
4133
return this._parseFile('parse_action_file', packageName, filePath);
4234
},
4335

44-
_parseJSONObject(str) {
45-
// For nodejs >= `contextSupportedVersion`, we leverage context parameter to
46-
// convert unsafe integer to string, otherwise, json-bigint is used.
47-
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
48-
if (isContextSupported) {
49-
return JSON.parse(str, (key, value, context) => {
50-
if (
51-
Number.isInteger(value) &&
52-
!Number.isSafeInteger(Number(context.source))
53-
) {
54-
return context.source;
55-
}
56-
return value;
57-
});
58-
}
59-
const JSONbigString = require('json-bigint')({ storeAsString: true });
60-
return JSONbigString.parse(str);
61-
},
62-
6336
_parseFile(command, packageName, filePath) {
6437
return new Promise((resolve, reject) => {
6538
const args = [
@@ -82,7 +55,7 @@ const rosidlParser = {
8255
)
8356
);
8457
} else {
85-
resolve(this._parseJSONObject(stdout));
58+
resolve(parseJSONWithBigInt(stdout));
8659
}
8760
}
8861
);

0 commit comments

Comments
 (0)