Skip to content

Commit c1ca79b

Browse files
committed
Custom JSON parsing: return *all* unsafe integers as strings, even if numeric representation as string is the same
1 parent 6a886a9 commit c1ca79b

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/db/customJSON.ts

+3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ export function enableCustomJSONParsingForLargeNumbers(pg: typeof pgLib) {
1212
pg.types.setTypeParser(pg.types.builtins.JSONB, parseJSONWithLargeNumbersAsStrings);
1313
}
1414

15+
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
16+
1517
function parseJSONWithLargeNumbersAsStrings(str: string) {
1618
return parse(str, undefined, function (k, str) {
1719
const n = +str; // JSON parser ensures this is an ordinary number, parseInt(str, 10) not needed
1820
if (n === Infinity || n === -Infinity) return str;
21+
if ((n < MIN_SAFE_INTEGER || n > MAX_SAFE_INTEGER) && str.indexOf('.') === -1) return str;
1922
if (str.length <= 15 || numericStringToExponential(str) === n.toExponential()) return n;
2023
return str;
2124
});

0 commit comments

Comments
 (0)