We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 6a886a9 commit c1ca79bCopy full SHA for c1ca79b
src/db/customJSON.ts
@@ -12,10 +12,13 @@ export function enableCustomJSONParsingForLargeNumbers(pg: typeof pgLib) {
12
pg.types.setTypeParser(pg.types.builtins.JSONB, parseJSONWithLargeNumbersAsStrings);
13
}
14
15
+const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
16
+
17
function parseJSONWithLargeNumbersAsStrings(str: string) {
18
return parse(str, undefined, function (k, str) {
19
const n = +str; // JSON parser ensures this is an ordinary number, parseInt(str, 10) not needed
20
if (n === Infinity || n === -Infinity) return str;
21
+ if ((n < MIN_SAFE_INTEGER || n > MAX_SAFE_INTEGER) && str.indexOf('.') === -1) return str;
22
if (str.length <= 15 || numericStringToExponential(str) === n.toExponential()) return n;
23
return str;
24
});
0 commit comments