JSON support for BigInt in Chrome/V8

T.J. Crowder tj.crowder at farsightsoftware.com
Sun Jul 29 09:04:37 UTC 2018

On Sun, Jul 29, 2018 at 9:01 AM, kai zhu
<kaizhu256 at gmail.com> wrote:
> i support @anders’ *conservative* scheme and opened up a github-issue to
> and get it included before stage-4 [1].

I agree with you and Anders that this should be sorted out now, not as a
follow-on proposal. If it's left to later, people will supply their own
`BigInt.prototype.toJSON` and cause themselves compatibility problems

Don't like the raw number in quotes ("conservative" option) at all. There
should be *some* indication that this is a bigint, just as pattern matching
tells us the default Date serialization is a date. This could be achieved
with just the lower-case `n` at the end, as in some early examples in the
github issue. And to support easy two-way, `BigInt(string)` should support
the lower-case `n` at the end.

aa = 12345678901234567890n; // BigInt primitive
aa = JSON.stringify(aa);    // '"12345678901234567890n"'
aa = JSON.parse(aa);        // '12345678901234567890n'
aa = BigInt(aa);            // BigInt primitive

`^\d+n$` isn't much of a pattern, but it's a pattern. (Earlier in this
thread I suggested `/BitInt(123)/` to match Microsoft's `/Date(123)/` which
is unambiguous and offers a path to extendibility, but Date already goes a
different way...)

This parallels Date handling and seems a reasonable stop-gap until the next
thing after JSON.

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180729/06a646f7/attachment-0001.html>

More information about the es-discuss mailing list