Created
May 22, 2020 11:13
-
-
Save bangonkali/792e12f0c9fb794404dfcacdf4d0b233 to your computer and use it in GitHub Desktop.
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
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