Skip to content

Instantly share code, notes, and snippets.

@jpb06
Created December 14, 2024 16:49
Show Gist options
  • Select an option

  • Save jpb06/2bf115be25d0412dcc404d935e98b56d to your computer and use it in GitHub Desktop.

Select an option

Save jpb06/2bf115be25d0412dcc404d935e98b56d to your computer and use it in GitHub Desktop.
Stringify an object containing bigints
const bigIntReplacer = (_key: string, value: unknown) => {
if (typeof value === 'bigint') {
return {
type: 'bigint',
value: value.toString(),
};
}
return value;
};
const withBigInt = {
name: 'task',
attributes: {},
durationInMilliseconds: 3,
startTime: {
type: 'bigint',
value: '1734194057253893501',
},
endTime: {
type: 'bigint',
value: '1734194057256936959',
},
};
const asString = JSON.stringify(withBigInt, bigIntReplacer, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment