5 ms·
How about the backwards compatible way of: {"value": "90071992547409949007199254740994n"} Which would get parsed into a BigInt. This specifically means that v
by askmike 8y ago
How about the backwards compatible way of:
{"value": "90071992547409949007199254740994n"}
Which would get parsed into a BigInt. This specifically means that value isn't a number but a bigint. All JSON consumers that don't support that get strings, all JSON consumers that do get BigInts (when parsing).
The only downside I see is existing json that has strings containing a number and a "n" at the end.
- rocqua 8y ago> The only downside I see is existing json that has strings containing a number and a "n" at the end. Imagine being on the receiving end of that bug!
- thedufer 8y agoThe other problem is that this would be a bizarre JavaScript-specific quirk, and would fail to interop with JSON implementations in other languages.
- Spivak 8y agoThis would require changing the JSON spec (good luck) as it doesn't fit the grammar for JSON numbers.
- myfonj 8y agoYes, it doesn't fit grammar for ordinary JSON Numbers, but since it is plain old standard JSON there is nothing wrong about it: it just puts some demand on consuming application. Instead of {"boring Number not greater than 2^53": 1234} you could send something like {"exciting huge BigInt": {"value": "12(…)89", "type": "BigInt"}} where you have all hints what to do with it. You can even invent some funny perks, like {"exciting huge BigInt serialized to base36": {"value": "12(…)yz", "type": "BigInt", "base": 36} } assuming you could create some parseBigInt(radix) function.
- Spivak 8y agoThis seems like it unnecessarily couples the JSON representation to the language types. JSON numbers are already arbitrarily precise. How you represent the type is up to the parser. JSON.stringify(5n) === "5" JSON.parse("5") === Number(5) JSON.parse("5000000000000000000") => Error JSON.parse({ use_bignum: true }) === 5000000000000000000n A project that does something like this: https://www.npmjs.com/package/json-bignum https://www.npmjs.com/package/json-bignum