Files
jj-command-line/helpers/safeJson.ts
T
2024-11-09 19:15:29 -05:00

14 lines
383 B
TypeScript

export function safeStringify(obj) {
const seen = new WeakSet();
return JSON.stringify(obj, (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return `[Circular ${key}]`; // Label circular reference with the key
}
seen.add(value);
}
return value;
});
}