json-bigint icon indicating copy to clipboard operation
json-bigint copied to clipboard

Why not use BigInt now native to node.js since v10.4.0?

Open codyzu opened this issue 6 years ago • 3 comments

Since v8 added native BigInt support and that has been available since node.js v10.4.0, why not use that instead of bignumber.js?

Thanks for this package! Greate work!

codyzu avatar Feb 21 '19 14:02 codyzu

@codyzu it has the problems that JSON.stringify a BigInt value as of v10.4.0

> theBiggestInt = 9007199254740991n;
9007199254740991n

> JSON.stringify(theBiggestInt)
Thrown:
TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)

huturen avatar May 29 '19 09:05 huturen

@huturen BigInt's toJSON is not implemented. I think that it can be handled using by implementing the BigInt.prototype.toJSON function.

But it would also override the default behavior of BigInt!

rramaa avatar Aug 29 '19 10:08 rramaa

@huturen But we wouldn't be using JSON.stringify, we'd be using JSONbig.stringify and they handle it.

@rramaa No need to override to prototype either.

if(typeof x === 'bigint') {
    return String(x);
}

mnpenner avatar Dec 11 '19 05:12 mnpenner