Created
September 8, 2023 10:44
-
-
Save kiyop/8fdc11410074f2a7ac508815d48212e9 to your computer and use it in GitHub Desktop.
BigInt に対応した JSON エンコード・デコード
This file contains 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
export const JSON = { | |
stringify: (obj: unknown): string => globalThis.JSON.stringify(obj, (k, v) => typeof v === 'bigint' ? v.toString() + 'n' : v), | |
parse: (json: string): object => globalThis.JSON.parse(json, (k, v) => typeof v === 'string' && v.match(/^[0-9]+n$/) ? BigInt(v.slice(0, -1)) : v), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment