debitoor / safe-json-stringify

A wrapper for JSON.stringify that handles circular references and prevent defined getters from throwing errors.
https://www.npmjs.com/package/safe-json-stringify
56 stars 16 forks source link

Stringify BigInts #12

Open ntr-808 opened 3 years ago

ntr-808 commented 3 years ago

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.

TwitchBronBron commented 1 year ago

Any chance this could get merged and released soon?

TwitchBronBron commented 1 year ago

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;
});