safe-json-stringify icon indicating copy to clipboard operation
safe-json-stringify copied to clipboard

Stringify BigInts

Open ntr-808 opened this issue 3 years ago • 2 comments

I ran into issues using https://github.com/trentm/node-bunyan which makes use of this library.

This PR simply converts BigInts to strings, which I think is reasonable because

> BigInt('11111111111111111111111111111111111');
11111111111111111111111111111111111n

BigNumber.js similarly uses strings as an intermediate representation.

ntr-808 avatar Mar 12 '21 07:03 ntr-808

Any chance this could get merged and released soon?

TwitchBronBron avatar Mar 15 '23 17:03 TwitchBronBron

For those who are interested, I worked around this issue in https://github.com/rokucommunity/logger/pull/3 by using the replacer 2nd argument.

const safeJsonStringify = require('safe-json-stringify');
const theObjectToStringify = {
    someBigInt: BigInt('123')
};
const jsonString = safeJsonStringify(theObjectToStringify, (key, value) => {
    //if it's a BigInt, return the string value instead
    return typeof value === 'bigint' ? value.toString() : value;
});

TwitchBronBron avatar Mar 16 '23 16:03 TwitchBronBron