sidorares / json-bigint

JSON.parse/stringify with bigints support
MIT License
814 stars 196 forks source link

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

Open codyzu opened 5 years ago

codyzu commented 5 years ago

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!

huturen commented 5 years ago

@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>)
rramaa commented 5 years ago

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

mnpenner commented 4 years ago

@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);
}