You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
stringify should throw when serializing a BigInt like JSON.stringify does #273
stringify should throw an error when serializing a BigInt to match JSON behavior.
constjson=JSON.stringify(123n)// Uncaught TypeError: Do not know how to serialize a BigIntconstjson5=JSON5.stringify(123n)// json5 == undefined
In order to serialize a BigInt, one could use a replacer function that converts a BigInt to either a number or a string. Alternatively, we could add an option to stringify that enables BigInt support.
I mostly agree with this, with some documentation clarification.
JSON5 nor JSON document a maximum number size.
I do understand the motivation for stringify be backwards compatible with JavaScript’s parser. Stringify should be documented to output Javascript compatible json, and documented that it does not support the full range of JSON5 input (i.e. large numbers) by default.
IMO, JSON5 should continue to explicitly support arbitrarily large numbers.
@chipweinberger I agree that the JSON5 format should continue to allow for arbitrary-precision integers as it currently does.
However, JSON also allows for arbitrary-precision integers, ECMAScript 2020 supports BigInt and BigInt literals, yet Node.js and browsers still throw an error when stringifying BigInt values with JSON. My question is, why? Usually there are specific reasons for this kind of behavior. I'd like to see if we can find that out before deciding which way to go.
@jordanbtucker the reason why JSON.stringify throws an error is that BigInt is not part of JSON spec. You can't serialize BigInt because it doesn't have a literal representation in JSON. But JSON5 should support BigInt, it can just create bigInt literal. I don't see a reason why JSON5 should throw an error. JSON5 is JSON that is more like JavaScript, and JS supports BigInt literals.
@jcubic Both JSON and JSON5 are capable of representing BigInts because neither spec sets limits on the precision of integers. Both RFC 8259 and RFC 7493 recommend sticking to numbers that can be represented by IEEE 754 64-bit floating-points for interoperability.
The latest ES spec explicitly throws an error when stringifying BigInt, not because JSON doesn't support BigInt, but because the existing JSON implementations rely on numbers being IEEE 754 64-bit floating-points. JSON5 is in a similar situation. If we start silently supporting BigInt, then that could create interoperability problems for other JSON5 parsers. Also keep in mind that neither JSON nor JSON5 are only used with JavaScript, and not all environments support arbitrary-precision integers.
If a user wants to stringify BigInt, then they can add a toJSON method to BigInt.prototype or use a replacer function. This currently works with JSON and JSON5. Throwing on stringifying BigInts will keep JSON5 behavior in line with JSON behavior for interoperability reasons.
I should mention that adding a toJSON method to BigInt.prototype or using a replacer function will not allow a user to generate a JSON or JSON5 document that contains numbers outside the range of IEEE 754 64-bit floating-points. You still need to use a string for numbers outside that range.
Thanks, I will try to use a replacer in my project for BitInt. I've just realized that I was using serialization of custom classes with a replacer in my other project but I've replaced it with CBOR.
I'm not opposed to adding an option that would allow stringify to generate JSON5 documents with large integers using BigInt, but this would be an opt-in feature, there would be no n suffix, and the documentation must address the interoperability implications.
For parse, an option could be added to parse integers greater than Number.MAX_SAFE_INTEGER or less than Number.MIN_SAFE_INTEGER as BigInts.
Activity
chipweinberger commentedon Aug 7, 2022
I mostly agree with this, with some documentation clarification.
JSON5 nor JSON document a maximum number size.
I do understand the motivation for stringify be backwards compatible with JavaScript’s parser. Stringify should be documented to output Javascript compatible json, and documented that it does not support the full range of JSON5 input (i.e. large numbers) by default.
IMO, JSON5 should continue to explicitly support arbitrarily large numbers.
jordanbtucker commentedon Aug 7, 2022
@chipweinberger I agree that the JSON5 format should continue to allow for arbitrary-precision integers as it currently does.
However, JSON also allows for arbitrary-precision integers, ECMAScript 2020 supports BigInt and BigInt literals, yet Node.js and browsers still throw an error when stringifying BigInt values with JSON. My question is, why? Usually there are specific reasons for this kind of behavior. I'd like to see if we can find that out before deciding which way to go.
jcubic commentedon Sep 26, 2022
@jordanbtucker the reason why
JSON.stringifythrows an error is that BigInt is not part of JSON spec. You can't serialize BigInt because it doesn't have a literal representation in JSON. But JSON5 should support BigInt, it can just create bigInt literal. I don't see a reason why JSON5 should throw an error. JSON5 is JSON that is more like JavaScript, and JS supports BigInt literals.jordanbtucker commentedon Sep 26, 2022
@jcubic Both JSON and JSON5 are capable of representing BigInts because neither spec sets limits on the precision of integers. Both RFC 8259 and RFC 7493 recommend sticking to numbers that can be represented by IEEE 754 64-bit floating-points for interoperability.
The latest ES spec explicitly throws an error when stringifying BigInt, not because JSON doesn't support BigInt, but because the existing JSON implementations rely on numbers being IEEE 754 64-bit floating-points. JSON5 is in a similar situation. If we start silently supporting BigInt, then that could create interoperability problems for other JSON5 parsers. Also keep in mind that neither JSON nor JSON5 are only used with JavaScript, and not all environments support arbitrary-precision integers.
If a user wants to stringify BigInt, then they can add a
toJSONmethod toBigInt.prototypeor use areplacerfunction. This currently works with JSON and JSON5. Throwing on stringifying BigInts will keep JSON5 behavior in line with JSON behavior for interoperability reasons.I recommend reading the discussions in tc39/proposal-bigint#24 and json5/json5-spec#36
jordanbtucker commentedon Sep 26, 2022
I should mention that adding a
toJSONmethod toBigInt.prototypeor using areplacerfunction will not allow a user to generate a JSON or JSON5 document that contains numbers outside the range of IEEE 754 64-bit floating-points. You still need to use a string for numbers outside that range.jcubic commentedon Sep 26, 2022
Thanks, I will try to use a replacer in my project for BitInt. I've just realized that I was using serialization of custom classes with a replacer in my other project but I've replaced it with CBOR.
jordanbtucker commentedon Sep 26, 2022
I'm not opposed to adding an option that would allow
stringifyto generate JSON5 documents with large integers using BigInt, but this would be an opt-in feature, there would be nonsuffix, and the documentation must address the interoperability implications.For
parse, an option could be added to parse integers greater thanNumber.MAX_SAFE_INTEGERor less thanNumber.MIN_SAFE_INTEGERas BigInts.jordanbtucker commentedon Oct 1, 2022
Fixed in e5b4b85