Created
December 14, 2024 16:49
-
-
Save jpb06/2bf115be25d0412dcc404d935e98b56d to your computer and use it in GitHub Desktop.
Stringify an object containing bigints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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