src/vs/workbench/services/extensions/common/rpcProtocol.ts/stringifyJsonWithBufferRefs
export function stringifyJsonWithBufferRefs<T>(obj: T, replacer: JSONStringifyReplacer | null = null, useSafeStringify = false): StringifiedJsonWithBufferRefs {
	const foundBuffers: VSBuffer[] = [];
	const serialized = (useSafeStringify ? safeStringify : JSON.stringify)(obj, (key, value) => {
		if (typeof value === 'undefined') {
			return undefinedRef; // JSON.stringify normally converts 'undefined' to 'null'
		} else if (typeof value === 'object') {
			if (value instanceof VSBuffer) {
				const bufferIndex = foundBuffers.push(value) - 1;
				return { [refSymbolName]: bufferIndex };
			}
			if (replacer) {
				return replacer(key, value);
			}
		}
		return value;
	});
	return {
		jsonString: serialized,
		referencedBuffers: foundBuffers
	};
}