Created
September 20, 2021 11:11
-
-
Save fostyfost/94eff7b8d562a918e7945902c3f7fb99 to your computer and use it in GitHub Desktop.
Converting circular structure to JSON
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
function censor(censor: any) { | |
let i = 0 | |
return function(_: any, value: unknown) { | |
if (i !== 0 && typeof censor === 'object' && typeof value === 'object' && censor === value) { | |
return '[Circular]' | |
} | |
// seems to be a harded maximum of 11 serialized objects? | |
if (i >= 10) { | |
return '[Unknown]' | |
} | |
++i // so we know we aren't using the original object anymore | |
return value | |
} | |
} |
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
JSON.stringify(obj, censor(obj)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment