Skip to content

Instantly share code, notes, and snippets.

@bangonkali
Created May 22, 2020 11:13
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 bangonkali/792e12f0c9fb794404dfcacdf4d0b233 to your computer and use it in GitHub Desktop.
Save bangonkali/792e12f0c9fb794404dfcacdf4d0b233 to your computer and use it in GitHub Desktop.
export class JsonUtils {
public static getCircularReplacer(): object {
const seen = new WeakSet();
return (key: string, value: object): any => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
}
public static stringify(input: any): string {
return JSON.stringify(input, JsonUtils.getCircularReplacer, 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment