mcollina / msgpack5

A msgpack v5 implementation for node.js, with extension points / msgpack.org[Node]
MIT License
493 stars 76 forks source link

How to solve the problem of large integer accuracy #106

Open weijian-zhu opened 2 years ago

weijian-zhu commented 2 years ago
<script src="./msgpack.js"></script>
<script>
    const { encode, decode } = msgpack5()
    var buffer = encode({ num: 86699530287996692 })
    var data = decode(buffer)
    console.log(data) // => { num: 86699530287996690  }
</script>
simoneb commented 2 years ago

This is not a problem with the library, the number you're trying to represent is larger than the largest integer that JS can represent

mcollina commented 2 years ago

It would be amazing if this library could support BigInts

weijian-zhu commented 2 years ago

ok,thanks you

simoneb commented 2 years ago

@weijian-zhu would you like to send a PR to introduce support for BigInt perhaps?

weijian-zhu commented 2 years ago

That sounds cool, but I still don't know enough about it to know where to start

simoneb commented 2 years ago

You may want to check out how the Date codec is implemented, but I think the implementation for BigInt should be much simpler than that https://github.com/mcollina/msgpack5/blob/master/lib/codecs/DateCodec.js