Skip to content

Instantly share code, notes, and snippets.

@kiyop
Created September 8, 2023 10:44
Show Gist options
  • Save kiyop/8fdc11410074f2a7ac508815d48212e9 to your computer and use it in GitHub Desktop.
Save kiyop/8fdc11410074f2a7ac508815d48212e9 to your computer and use it in GitHub Desktop.
BigInt に対応した JSON エンコード・デコード
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