41
42
export function stringifyJsonWithBufferRefs<T>(obj: T, replacer: JSONStringifyReplacer | null = null, useSafeStringify = false): StringifiedJsonWithBufferRefs {
44
>
const serialized = (useSafeStringify ? safeStringify : JSON.stringify)(obj, (key, value) => {
45
>
if (typeof value === 'undefined') {
46
return undefinedRef; // JSON.stringify normally converts 'undefined' to 'null'
48
>
if (value instanceof VSBuffer) {
49
>
const bufferIndex = foundBuffers.push(value) - 1;
50
>
return { [refSymbolName]: bufferIndex };
51
>
}
52
>
if (replacer) {
53
return replacer(key, value);
54
}
56
>
return value;
57
>
});
58
>
return {
59
>
jsonString: serialized,
60
>
referencedBuffers: foundBuffers
61
>
};
62
>
}
63
64
export function parseJsonAndRestoreBufferRefs(jsonString: string, buffers: readonly VSBuffer[], uriTransformer: IURITransformer | null): any {
66
>
if (value) {
67
>
const ref = value[refSymbolName];
68
>
if (typeof ref === 'number') {
69
>
return buffers[ref];
70
>
}
71
>
72
>
if (uriTransformer && (<MarshalledObject>value).$mid === MarshalledId.Uri) {
73
return uriTransformer.transformIncoming(value);
74
}
76
>
return value;
77
>
});
78
>
}
79
80