src/vs/base/common/marshalling.ts

84 LOC · 84 covered · 0 uncovered · 23 ranges · 3442 concepts · 13 introducers · 1946 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- marshalling.ts ×4
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { VSBuffer } from './buffer.js';
7 > import { URI, UriComponents } from './uri.js';
8 > import { MarshalledId } from './marshallingIds.js';
9 >
10 > export function stringify(obj: unknown): string {
11 > return JSON.stringify(obj, replacer); marshalling.ts ×3
12 > }
14 > export function parse(text: string): any {
15 > let data = JSON.parse(text); marshalling.ts ×1
16 > data = revive(data);
17 > return data;
18 > }
20 > export interface MarshalledObject {
21 > $mid: MarshalledId;
22 > }
23 >
24 > function replacer(key: string, value: any): any { marshalling.ts ×3
25 > // URI is done via toJSON-member
26 > if (value instanceof RegExp) {
27 > return { marshalling.ts ×1
28 > $mid: MarshalledId.Regexp,
29 > source: value.source,
30 > flags: value.flags,
31 > };
32 > }
33 > return value; marshalling.ts ×3
34 > }
36 >
37 > type Deserialize<T> = T extends UriComponents ? URI
38 > : T extends VSBuffer ? VSBuffer
39 > : T extends object
40 > ? Revived<T>
41 > : T;
42 >
43 > export type Revived<T> = { [K in keyof T]: Deserialize<T[K]> };
44 >
45 > export function revive<T = any>(obj: any, depth = 0): Revived<T> {
46 > if (!obj || depth > 200) { marshalling.ts ×2
47 > return obj; marshalling.ts ×1
48 > }
50 > if (typeof obj === 'object') {
52 > switch ((<MarshalledObject>obj).$mid) {
53 > // eslint-disable-next-line local/code-no-any-casts
54 > case MarshalledId.Uri: return <any>URI.revive(obj);
55 > // eslint-disable-next-line local/code-no-any-casts
56 > case MarshalledId.Regexp: return <any>new RegExp(obj.source, obj.flags);
57 > // eslint-disable-next-line local/code-no-any-casts
58 > case MarshalledId.Date: return <any>new Date(obj.source);
59 > }
61 > if (
62 > obj instanceof VSBuffer
63 > || obj instanceof Uint8Array
65 > // eslint-disable-next-line local/code-no-any-casts marshalling.ts ×1
66 > return <any>obj;
67 > }
69 > if (Array.isArray(obj)) {
70 > for (let i = 0; i < obj.length; ++i) { marshalling.ts ×1
71 > obj[i] = revive(obj[i], depth + 1); marshalling.ts ×1
72 > }
73 > } else { marshalling.ts ×3
74 > // walk object marshalling.ts ×1
75 > for (const key in obj) {
76 > if (Object.hasOwnProperty.call(obj, key)) {
77 > obj[key] = revive(obj[key], depth + 1);
78 > }
79 > }
80 > }
83 > return obj;
84 > }