safe-json-stringify
safe-json-stringify copied to clipboard
Stringify BigInts
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.
Any chance this could get merged and released soon?
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;
});