3 ms·
It's not the default, but JS is capable of this. (JavaScript has a big integer type nowadays, and the JSON.parse function's "reviver" parameter I think should b
by deathanatos 1y ago
It's not the default, but JS is capable of this. (JavaScript has a big integer type nowadays, and the JSON.parse function's "reviver" parameter I think should be capable of parsing to bigints, but you'd need to specify such a reviver.)
Something like this, I think:
JSON.parse(
/* just a test input JSON */
`{"a": 1.1, "b": 22222222222222222222222222222222, "c": {"d": 999999999999999999999999}}`,
/* a reviver that returns BigInts, if it's an integer. */
(key, value, context) => {
if(typeof value === "number" && /^[0-9]+$/.test(context.source)) {
return BigInt(context.source);
} else {
return value;
}
}
);