Skip to content

stringify should throw when serializing a BigInt like JSON.stringify does #273

Closed

Description

@jordanbtucker

stringify should throw an error when serializing a BigInt to match JSON behavior.

const json = JSON.stringify(123n)
// Uncaught TypeError: Do not know how to serialize a BigInt

const json5 = 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.

Activity

  1. added this to the v3.0.0 milestone on May 27, 2022
  2. chipweinberger commented on Aug 7, 2022

    @chipweinberger

    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.

  3. jordanbtucker commented on Aug 7, 2022

    @jordanbtucker
    MemberAuthor

    @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.

  4. jcubic commented on Sep 26, 2022

    @jcubic

    @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.

  5. jordanbtucker commented on Sep 26, 2022

    @jordanbtucker
    MemberAuthor

    @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 recommend reading the discussions in tc39/proposal-bigint#24 and json5/json5-spec#36

  6. jordanbtucker commented on Sep 26, 2022

    @jordanbtucker
    MemberAuthor

    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.

  7. jcubic commented on Sep 26, 2022

    @jcubic

    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.

  8. jordanbtucker commented on Sep 26, 2022

    @jordanbtucker
    MemberAuthor

    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.

  9. jordanbtucker commented on Oct 1, 2022

    @jordanbtucker
    MemberAuthor

    Fixed in e5b4b85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions