JSON support for BigInt in Chrome/V8
Anders Rundgren
anders.rundgren.net at gmail.com
Sun Jul 15 04:23:17 UTC 2018
On 2018-07-15 04:27, J Decker wrote:
>
>
> On Sat, Jul 14, 2018 at 1:36 AM Anders Rundgren <anders.rundgren.net at gmail.com <mailto:anders.rundgren.net at gmail.com>> wrote:
>
> var small = BigInt("5");
> var big = BigInt("5555555555555555555555555500003");
> JSON.stringify([big,small]);
> VM330:1 Uncaught TypeError: Do not know how to serialize a BigInt
> at JSON.stringify (<anonymous>)
> at <anonymous>:1:6
>
>
> is BigInt the only way to create a BigInt ? Or did they also implement the 'n' suffix, which I noted here https://github.com/tc39/proposal-bigint/issues/24#issuecomment-392307848 would easily distinguish bigint from other numbers; and be easy to add on the parsing side; and call BigInt(xxx) instead of Number(xxx).
This problem is related to the BigInt object itself. If you create such using the 'n' notation you get the same result.
If you want to use BigInt with JSON you have to serialize it yourself:
var small = BigInt(5n);
var big = BigInt(5555555555555555555555555500003n);
JSON.stringify([big.toString(),small.toString()]);
which generates ["5555555555555555555555555500003","5"]
Anders
> var small = 5n;
> var big = 5555555555555555555555555500003n;
>
> n suffix as from
> https://github.com/tc39/proposal-bigint
>
> JSON Number serialization has apparently reached a new level (of confusion).
>
> Personally I don't see the problem. XML did just fine without hard-coded data types.
>
> The JSON type system is basically a relic from JavaScript. As such it has proved to be quite useful.
> However, when you are outside of that scope, the point with the JSON type system gets pretty much zero since you anyway need to map extended types.
>
> Oracle's JSON-B solution which serializes small values as Number and large values as String rather than having a unified serialization based on the underlying data type seems like a pretty broken concept although indeed fully conforming to the JSON specification. "Like the Devil reads the Bible" as we say in Scandinavia :-)
>
> Adding a couple of double quotes is a major problem? If so, it seems like a way more useful project making quotes optional for keys (named in a specific way), like they already are in JavaScript.
>
> Yeah, and of course adding support for comments.
>
>
> I'd rather not see numbers converted to strings; that would be required to allow application handling of values; at a layer higher than JSON core itself. It is nice that JSON keeps numbers as numbers and strings as strings without needing intimite knowledge about the actual 'types' they end up in.
>
> Comparing numeric length would be a half/useless solution since bigints are required to interop with other bigints only; so small numbers couldn't be 'guessed' and the application would have to provide a reviver.
>
>
>
> Anders
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
More information about the es-discuss
mailing list