Skip to content

Commit

Permalink
Use JSON11 to parse and serialize long numerals
Browse files Browse the repository at this point in the history
Signed-off-by: Miki <[email protected]>
  • Loading branch information
AMoo-Miki committed Jun 1, 2024
1 parent f7e2f81 commit b140177
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/Serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const debug = require('debug')('opensearch');
const sjson = require('secure-json-parse');
const { SerializationError, DeserializationError } = require('./errors');
const kJsonOptions = Symbol('secure json parse options');
const JSON11 = require('json11');

/* In JavaScript, a `Number` is a 64-bit floating-point value which can store 16 digits. However, the
* serializer and deserializer will need to cater to numeric values generated by other languages which
Expand Down Expand Up @@ -265,7 +266,12 @@ class Serializer {
json = JSON.stringify(object, shouldHandleLongNumerals ? checkForBigInts : null);

if (shouldHandleLongNumerals && !numeralsAreNumbers) {
const temp = this._stringifyWithBigInt(object, json);
// With `withBigInt: false`, valid JSON is produced while maintaining accuracy
const temp = JSON11.stringify(object, {
withBigInt: false,
quote: '"',
quoteNames: true,
});
if (temp) json = temp;
}
} catch (err) {
Expand Down Expand Up @@ -299,7 +305,7 @@ class Serializer {
);

if (shouldHandleLongNumerals && !numeralsAreNumbers) {
const temp = this._parseWithBigInt(json);
const temp = JSON11.parse(json, null, { withLongNumerals: true });
if (temp) {
object = temp;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"xmlbuilder2": "^3.0.2"
},
"dependencies": {
"json11": "^1.0.2",
"aws4": "^1.11.0",
"debug": "^4.3.1",
"hpagent": "^1.2.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,11 @@ json-stable-stringify-without-jsonify@^1.0.1:
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
integrity sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=

json11@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/json11/-/json11-1.0.2.tgz#98e6a3a01d3ed03d40c3ecc7e945767d23287b6f"
integrity sha512-XnGnLo/fE2gsyK+VSPZCOB0IQlJfQaDVIPd4A+KvK+1inMQbm1eEimu/1JzTZgZ0YO/n2WJcdJGDgPHtiQynjw==

json5@^2.1.2:
version "2.2.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
Expand Down

0 comments on commit b140177

Please sign in to comment.