-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
json.ts
351 lines (296 loc) · 8.68 KB
/
json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
/**
* A type alias for a JSON primitive.
*/
export
type JSONPrimitive = boolean | number | string | null;
/**
* A type alias for a JSON value.
*/
export
type JSONValue = JSONPrimitive | JSONObject | JSONArray;
/**
* A type definition for a JSON object.
*/
export
interface JSONObject { [key: string]: JSONValue | ReadonlyJSONValue; }
/**
* A type definition for a JSON array.
*/
export
interface JSONArray extends Array<JSONValue> { }
/**
* A type definition for a readonly JSON object.
*/
export
interface ReadonlyJSONObject { readonly [key: string]: ReadonlyJSONValue; }
/**
* A type definition for a readonly JSON array.
*/
export
interface ReadonlyJSONArray extends ReadonlyArray<ReadonlyJSONValue> { }
/**
* A type alias for a readonly JSON value.
*/
export
type ReadonlyJSONValue = JSONPrimitive | ReadonlyJSONObject | ReadonlyJSONArray;
/**
* A type alias for a partial JSON value.
*
* Note: Partial here means that JSON object attributes can be `undefined`.
*/
export
type PartialJSONValue = JSONPrimitive | PartialJSONObject | PartialJSONArray;
/**
* A type definition for a partial JSON object.
*
* Note: Partial here means that the JSON object attributes can be `undefined`.
*/
export
interface PartialJSONObject { [key: string]: PartialJSONValue | undefined; }
/**
* A type definition for a partial JSON array.
*
* Note: Partial here means that JSON object attributes can be `undefined`.
*/
export
interface PartialJSONArray extends Array<PartialJSONValue> { }
/**
* A type definition for a readonly partial JSON object.
*
* Note: Partial here means that JSON object attributes can be `undefined`.
*/
export
interface ReadonlyPartialJSONObject { readonly [key: string]: ReadonlyPartialJSONValue | undefined; }
/**
* A type definition for a readonly partial JSON array.
*
* Note: Partial here means that JSON object attributes can be `undefined`.
*/
export
interface ReadonlyPartialJSONArray extends ReadonlyArray<ReadonlyPartialJSONValue> { }
/**
* A type alias for a readonly partial JSON value.
*
* Note: Partial here means that JSON object attributes can be `undefined`.
*/
export
type ReadonlyPartialJSONValue = JSONPrimitive | ReadonlyPartialJSONObject | ReadonlyPartialJSONArray;
/**
* The namespace for JSON-specific functions.
*/
export
namespace JSONExt {
/**
* A shared frozen empty JSONObject
*/
export
const emptyObject = Object.freeze({}) as ReadonlyJSONObject;
/**
* A shared frozen empty JSONArray
*/
export
const emptyArray = Object.freeze([]) as ReadonlyJSONArray;
/**
* Test whether a JSON value is a primitive.
*
* @param value - The JSON value of interest.
*
* @returns `true` if the value is a primitive,`false` otherwise.
*/
export
function isPrimitive(value: ReadonlyPartialJSONValue): value is JSONPrimitive {
return (
value === null ||
typeof value === 'boolean' ||
typeof value === 'number' ||
typeof value === 'string'
);
}
/**
* Test whether a JSON value is an array.
*
* @param value - The JSON value of interest.
*
* @returns `true` if the value is a an array, `false` otherwise.
*/
export
function isArray(value: JSONValue): value is JSONArray;
export
function isArray(value: ReadonlyJSONValue): value is ReadonlyJSONArray;
export
function isArray(value: PartialJSONValue): value is PartialJSONArray;
export
function isArray(value: ReadonlyPartialJSONValue): value is ReadonlyPartialJSONArray;
export
function isArray(value: ReadonlyPartialJSONValue): boolean {
return Array.isArray(value);
}
/**
* Test whether a JSON value is an object.
*
* @param value - The JSON value of interest.
*
* @returns `true` if the value is a an object, `false` otherwise.
*/
export
function isObject(value: JSONValue): value is JSONObject;
export
function isObject(value: ReadonlyJSONValue): value is ReadonlyJSONObject;
export
function isObject(value: PartialJSONValue): value is PartialJSONObject;
export
function isObject(value: ReadonlyPartialJSONValue): value is ReadonlyPartialJSONObject;
export
function isObject(value: ReadonlyPartialJSONValue): boolean {
return !isPrimitive(value) && !isArray(value);
}
/**
* Compare two JSON values for deep equality.
*
* @param first - The first JSON value of interest.
*
* @param second - The second JSON value of interest.
*
* @returns `true` if the values are equivalent, `false` otherwise.
*/
export
function deepEqual(first: ReadonlyPartialJSONValue, second: ReadonlyPartialJSONValue): boolean {
// Check referential and primitive equality first.
if (first === second) {
return true;
}
// If one is a primitive, the `===` check ruled out the other.
if (isPrimitive(first) || isPrimitive(second)) {
return false;
}
// Test whether they are arrays.
let a1 = isArray(first);
let a2 = isArray(second);
// Bail if the types are different.
if (a1 !== a2) {
return false;
}
// If they are both arrays, compare them.
if (a1 && a2) {
return deepArrayEqual(first as ReadonlyPartialJSONArray, second as ReadonlyPartialJSONArray);
}
// At this point, they must both be objects.
return deepObjectEqual(first as ReadonlyPartialJSONObject, second as ReadonlyPartialJSONObject);
}
/**
* Create a deep copy of a JSON value.
*
* @param value - The JSON value to copy.
*
* @returns A deep copy of the given JSON value.
*/
export
function deepCopy<T extends ReadonlyPartialJSONValue>(value: T): T {
// Do nothing for primitive values.
if (isPrimitive(value)) {
return value;
}
// Deep copy an array.
if (isArray(value)) {
return deepArrayCopy(value);
}
// Deep copy an object.
return deepObjectCopy(value);
}
/**
* Compare two JSON arrays for deep equality.
*/
function deepArrayEqual(first: ReadonlyPartialJSONArray, second: ReadonlyPartialJSONArray): boolean {
// Check referential equality first.
if (first === second) {
return true;
}
// Test the arrays for equal length.
if (first.length !== second.length) {
return false;
}
// Compare the values for equality.
for (let i = 0, n = first.length; i < n; ++i) {
if (!deepEqual(first[i], second[i])) {
return false;
}
}
// At this point, the arrays are equal.
return true;
}
/**
* Compare two JSON objects for deep equality.
*/
function deepObjectEqual(first: ReadonlyPartialJSONObject, second: ReadonlyPartialJSONObject): boolean {
// Check referential equality first.
if (first === second) {
return true;
}
// Check for the first object's keys in the second object.
for (let key in first) {
if (first[key] !== undefined && !(key in second)) {
return false;
}
}
// Check for the second object's keys in the first object.
for (let key in second) {
if (second[key] !== undefined && !(key in first)) {
return false;
}
}
// Compare the values for equality.
for (let key in first) {
// Get the values.
let firstValue = first[key];
let secondValue = second[key];
// If both are undefined, ignore the key.
if (firstValue === undefined && secondValue === undefined) {
continue;
}
// If only one value is undefined, the objects are not equal.
if (firstValue === undefined || secondValue === undefined) {
return false;
}
// Compare the values.
if (!deepEqual(firstValue, secondValue)) {
return false;
}
}
// At this point, the objects are equal.
return true;
}
/**
* Create a deep copy of a JSON array.
*/
function deepArrayCopy(value: any): any {
let result = new Array<any>(value.length);
for (let i = 0, n = value.length; i < n; ++i) {
result[i] = deepCopy(value[i]);
}
return result;
}
/**
* Create a deep copy of a JSON object.
*/
function deepObjectCopy(value: any): any {
let result: any = {};
for (let key in value) {
// Ignore undefined values.
let subvalue = value[key];
if (subvalue === undefined) {
continue;
}
result[key] = deepCopy(subvalue);
}
return result;
}
}