JSON support for BigInt in Chrome/V8

J Decker d3ck0r at gmail.com
Sun Jul 15 02:27:01 UTC 2018

On Sat, Jul 14, 2018 at 1:36 AM Anders Rundgren <
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
would easily distinguish bigint from other numbers; and be easy to add on
the parsing side; and call BigInt(xxx) instead of Number(xxx).

var small = 5n;
var big = 5555555555555555555555555500003n;

n suffix as from

> 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
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180714/9110150e/attachment.html>

More information about the es-discuss mailing list