Skip to content

Instantly share code, notes, and snippets.

@jordanbtucker
Created May 27, 2022 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanbtucker/d48b826aa9f4e868f8d406d84fa10526 to your computer and use it in GitHub Desktop.
Save jordanbtucker/d48b826aa9f4e868f8d406d84fa10526 to your computer and use it in GitHub Desktop.
JSON/JSON5 BigInt Replacer and Reviver
function bigIntReplacer(key, value) {
if (typeof value === 'bigint') {
return `${value}n`
}
return value
}
function bigIntReviver(name, value) {
const bigIntRegExp = /^([-+]?\d+)n$/
if (typeof value === 'string') {
const match = bigIntRegExp.exec(value)
if (match != null) {
return BigInt(match[1])
}
}
return value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment