4 ms·
Fortunately we're seeing more JS DB libraries offering to read large numbers as the BigInt type.
by spiffytech 4mo ago
Fortunately we're seeing more JS DB libraries offering to read large numbers as the BigInt type.
- shakna 4mo agoBut frustratingly, a JS BigInt is nothing like a BigInt in any other language. In JS - BigInt is 64bit integer. In anything else - BigInt is a arbitrarily large integer.
- anematode 4mo agoHm? JavaScript BigInts are arbitrary precision, and you need to use methods like BigInt.asIntN(64, a) to convert them to 64 bits
- mort96 4mo agoI hate this so much because you can’t nicely serialise a BigInt as JSON. Using a string is nicer but it only makes sense where int64 is used as an ID, not where it’s used as a number; and you don’t wanna have to configure this per field per query.
- nh2 4mo agoJSON has arbitrary length numbers in the spec only.
- mort96 4mo agoCompletely and utterly irrelevant.
- sheept 4mo agoYou can serialize a BigInt by specifying a replacer: const obj = { a: 9007199254740993n } JSON.stringify(obj, (_key, value) => typeof value === 'bigint' ? JSON.rawJSON(value.toString()) : value)
- mort96 4mo agoAnd then you end up with strings on the other side, not numbers.
- sheept 4mo agoNo you don't? The example I gave produces {"a":9007199254740993} not {"a":"9007199254740993"}
- mort96 4mo agoOh, that's much worse! The JSON string `{"a":9007199254740993}` decodes to the object `{"a":9007199254740992}` with typical JSON parsers like JavaScript's `JSON.parse`.
- sheept 4mo agoIf you're applying a replacer, then you'd supply a reviver when parsing: const json = '{ "a": 9007199254740993 }' JSON.parse(json, (_key, value, context) => /^\d+$/.test(context.source) ? BigInt(context.source) : value)
- mort96 4mo agoYeah but now you have the world's biggest foot gun in your API.
- marcosdumay 4mo agoIMO, I'm tending toward thinking that having types on your readable serialization format is a mistake, and that they should be always input to the (de)serializer instead.
- Etheryte 4mo agoThis is simply not true? Or maybe I misunderstand what you mean?