Skip to content

Instantly share code, notes, and snippets.

@j1elo
Created February 2, 2022 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j1elo/5ae6694d7b04d21de075212c69341a1d to your computer and use it in GitHub Desktop.
Save j1elo/5ae6694d7b04d21de075212c69341a1d to your computer and use it in GitHub Desktop.
// Replacer function meant for `JSON.stringify()`.
function getCircularReplacer() {
const seen = new WeakSet();
return (_key, value) => {
// Non-null "object" properties: return a deep copy without circular references.
if (typeof value === "object" && value !== null) {
// Remove circular references.
if (seen.has(value)) {
// Option 1: Replace circular references with a string.
return "(circular)";
// Option 2: Return `undefined` to just skip circular references.
// return;
}
seen.add(value);
// Make a deep copy where all properties are enumerable.
const copy = Array.isArray(value) ? [] : {};
Object.getOwnPropertyNames(value).forEach((propName) => {
copy[propName] = value[propName];
});
return copy;
}
// Rest of properties: return as-is.
return value;
};
}
// The Error type has some non-enumerable properties: "stack", "message".
const myError = new Error("My Error message");
myError["a"] = "My property";
console.log(myError);
// Calls `myError.toString()` which is not a good serialized form:
// > Error: My Error message
console.log(JSON.stringify(myError));
// Doesn't print the non-enumerable properties:
// > {"a":"My property"}
myError.c = myError; // .c is a circular reference.
console.log(JSON.stringify(myError));
// Fails with:
// > TypeError: Converting circular structure to JSON
// > --> starting at object with constructor 'Error'
// > --- property 'c' closes the circle
// > at JSON.stringify
console.log(JSON.stringify(myError, getCircularReplacer()));
// Removes circular references and prints the non-enumerable properties:
// > {"stack":"Error: My Error message\n at <anonymous>:1:17","message":"My Error message","a":"My property","c":"(circular)"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment