-
Notifications
You must be signed in to change notification settings - Fork 266
Description
This is issue is somewhat related these two issues, but is not so much concerned with handling BigInt literals (ex: 12n) as simply supporting very absolutely large numbers, namely those that fall outside the [-(2**53)+1, (2**53)-1] range recommended by the JSON spec.
My specific use case is potentially using JSON5 as the human-readable for of a binary serialization format. For this purpose, expanding the range of support to [-(2**63)+1, (2**64)-1] is sufficient. However, in the Javascript case this requires using BigInt anyway, which can be arbitrarily large, so expanding to this range would implicitly allow any valid JSON number to be successfully parsed and stringified.
This is currently not supported, as seen here:
let j5 = require('json5');
let parsed = j5.parse('{"min_int64": -9223372036854775808, "max_int64": 9223372036854775807, "max_uint64": 18446744073709551615}');
console.log(parsed);
/* Logs the following - all 3 values are incorrect!
{
min_int64: -9223372036854776000,
max_int64: 9223372036854776000,
max_uint64: 18446744073709552000
} */I wonder if it wouldn't be possible to extend the library to automatically process these to and from BigInts when parsing and stringifying, respectively? To maintain ES5 compatibility, I imagine this could look something like:
- Check that
typeof BigInt == 'function'on startup. If this returns false, then no flag is set, and all code proceeds as it does today (ie, mangling very large integers in the manner seen above). If true, the library sets some flag,USE_BIG_INTor similar, to true. - Modify the parser and stringifier to check this flag, and iff the flag is true, optionally process the flag to/from BigInt if necessary.
Would this be a desirable feature for this library? If so, I am happy to do the implementation.