startrek
1/7/2019 - 11:45 PM

Custom JSON Stringify

Custom JSON Stringify function

function customStringify(v) {
		const cache = new Set();
		return JSON.stringify(v, function (key, value) {
			if (typeof value === 'object' && value !== null) {
				if (cache.has(value)) {
					// Circular reference found
					try {
						// If this value does not reference a parent it can be deduped
						return JSON.parse(JSON.stringify(value));
					}
					catch (err) {
						// discard key if value cannot be deduped
						return;
					}
				}
				// Store value in our set
				cache.add(value);
			}
			return value;
		});
	}