logoalt Hacker News

mort96today at 7:27 AM2 repliesview on HN

I 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.


Replies

sheepttoday at 10:06 AM

You 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)
show 1 reply
nh2today at 8:11 AM

JSON has arbitrary length numbers in the spec only.

show 1 reply