-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathJSON.ts
459 lines (390 loc) · 9.1 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import { Buffer } from "./util";
import { JSONDecoder, JSONHandler } from './decoder';
class Handler extends JSONHandler {
stack: Value[] = new Array<Value>();
reset(): void {
while (this.stack.length > 0) {
this.stack.pop();
}
}
get peek(): Value {
return this.stack[this.stack.length - 1];
}
setString(name: string, value: string): void {
const obj: Value = Value.String(value);
this.addValue(name, obj);
}
setBoolean(name: string, value: bool): void {
const obj = Value.Bool(value);
this.addValue(name, obj);
}
setNull(name: string): void {
const obj = Value.Null();
this.addValue(name, obj);
}
setInteger(name: string, value: i64): void {
const obj = Value.Integer(value);
this.addValue(name, obj);
}
setFloat(name: string, value: f64): void {
const obj = Value.Float(value);
this.addValue(name, obj);
}
pushArray(name: string): bool {
const obj: Value = Value.Array();
if (this.stack.length == 0) {
this.stack.push(obj);
} else {
this.addValue(name, obj);
this.stack.push(obj);
}
return true;
}
popArray(): void {
if (this.stack.length > 1) {
this.stack.pop();
}
}
pushObject(name: string): bool {
const obj: Value = Value.Object();
this.addValue(name, obj);
this.stack.push(obj);
return true;
}
popObject(): void {
if (this.stack.length > 1) {
this.stack.pop();
}
}
addValue(name: string, obj: Value): void {
if (name.length == 0 && this.stack.length == 0) {
this.stack.push(obj);
return;
}
if (this.peek instanceof Obj) {
(this.peek as Obj).set(name, obj);
} else if (this.peek instanceof Arr) {
(<Arr>this.peek).push(obj);
}
}
}
namespace _JSON {
// @ts-ignore decorator is valid
@lazy
export const handler: Handler = new Handler();
// @ts-ignore decorator is valid
@lazy
export const decoder: JSONDecoder<Handler> = new JSONDecoder<Handler>(
_JSON.handler
);
/** Parses a string or Uint8Array and returns a Json Value. */
export function parse<T = Uint8Array>(str: T): Value {
var arr: Uint8Array;
if (isString<T>(str)) {
arr = Buffer.fromString(<string>str);
} else {
arr = changetype<Uint8Array>(str);
}
_JSON.decoder.deserialize(arr);
const res = _JSON.decoder.handler.peek;
_JSON.decoder.handler.reset();
return res;
}
}
// @ts-ignore
@lazy const NULL: Null = new Null();
export abstract class Value {
static String(str: string): Str {
return new Str(str);
}
static Number(num: f64): Num {
return new Num(num);
}
static Float(num: f64): Float {
return new Float(num);
}
static Integer(num: i64): Integer {
return new Integer(num);
}
static Bool(b: bool): Bool {
return new Bool(b);
}
static Null(): Null {
return NULL;
}
static Array(): Arr {
return new Arr();
}
static Object(): Obj {
return new Obj();
}
get isString(): boolean {
return this instanceof Str;
}
get isNum(): boolean {
return this instanceof Num;
}
get isFloat(): boolean {
return this instanceof Float;
}
get isInteger(): boolean {
return this instanceof Integer;
}
get isBool(): boolean {
return this instanceof Bool;
}
get isNull(): boolean {
return this instanceof Null;
}
get isArr(): boolean {
return this instanceof Arr;
}
get isObj(): boolean {
return this instanceof Obj;
}
/**
* @returns A valid JSON string of the value
*/
abstract stringify(): string;
/**
*
* @returns A AS string corresponding to the value.
*/
toString(): string {
return this.stringify();
}
}
function escapeChar(char: string): string {
const charCode = char.charCodeAt(0);
switch (charCode) {
case 0x22: return '\\"';
case 0x5C: return "\\\\";
case 0x08: return "\\b";
case 0x0A: return "\\n";
case 0x0D: return "\\r";
case 0x09: return "\\t";
case 0x0C: return "\\f";
case 0x0B: return "\\u000b";
default: return char;
}
}
export class Str extends Value {
constructor(public _str: string) {
super();
}
stringify(): string {
let escaped: string[] = new Array(this._str.length);
for (let i = 0; i < this._str.length; i++) {
const char = this._str.at(i);
escaped[i] = escapeChar(char);
}
return `"${escaped.join('')}"`;
}
toString(): string {
return this._str;
}
valueOf(): string {
return this._str;
}
}
export class Num extends Value {
constructor(public _num: f64) {
super();
}
stringify(): string {
return this._num.toString();
}
valueOf(): f64 {
return this._num;
}
}
export class Float extends Num {
}
export class Integer extends Value {
constructor(public _num: i64) {
super();
}
stringify(): string {
return this._num.toString();
}
valueOf(): i64 {
return this._num;
}
}
export class Null extends Value {
constructor() {
super();
}
stringify(): string {
return "null";
}
valueOf(): null {
return null;
}
}
export class Bool extends Value {
constructor(public _bool: bool) {
super();
}
stringify(): string {
return this._bool.toString();
}
valueOf(): bool {
return this._bool;
}
}
export class Arr extends Value {
_arr: Array<Value>;
constructor() {
super();
this._arr = new Array<Value>();
}
push(obj: Value): void {
this._arr.push(obj);
}
stringify(): string {
return (
"[" +
this._arr
.map<string>((val: Value, i: i32, _arr: Value[]): string =>
val.stringify()
)
.join(",") +
"]"
);
}
valueOf(): Array<Value> {
return this._arr;
}
}
export class Obj extends Value {
_obj: Map<string, Value>;
constructor() {
super();
this._obj = new Map();
}
get keys(): string[] {
return this._obj.keys();
}
stringify(): string {
const keys = this._obj.keys();
const objs: string[] = new Array<string>(keys.length);
for (let i: i32 = 0; i < keys.length; i++) {
const key = keys[i];
const value = this._obj.get(key);
// Currently must get the string value before interpolation
// see: https://github.com/AssemblyScript/assemblyscript/issues/1944
const valStr = value.stringify();
objs[i] = `"${key}":${valStr}`;
}
return `{${objs.join(",")}}`;
}
valueOf(): Map<string, Value> {
return this._obj;
}
set<T>(key: string, value: T): void {
if (isReference<T>(value)) {
if (value instanceof Value) {
this._obj.set(key, <Value>value);
return;
}
}
this._obj.set(key, from<T>(value));
}
has(key: string): bool {
return this._obj.has(key);
}
get(key: string): Value | null {
if (!this._obj.has(key)) {
return null;
}
return this._obj.get(key);
}
getValue(key: string): Value | null {
return this.get(key);
}
getString(key: string): Str | null {
let jsonValue = this.get(key);
if (jsonValue != null && jsonValue.isString) {
return <Str>jsonValue;
}
return null;
}
getNum(key: string): Num | null {
let jsonValue = this.get(key);
if (jsonValue != null && jsonValue.isNum) {
return <Num>jsonValue;
}
return null;
}
getFloat(key: string): Float | null {
let jsonValue = this.get(key);
if (jsonValue != null && jsonValue.isFloat) {
return <Float>jsonValue;
}
return null;
}
getInteger(key: string): Integer | null {
let jsonValue = this.get(key);
if (jsonValue != null && jsonValue.isInteger) {
return <Integer>jsonValue;
}
return null;
}
getBool(key: string): Bool | null {
let jsonValue = this.get(key);
if (jsonValue != null && jsonValue.isBool) {
return <Bool>jsonValue;
}
return null;
}
getArr(key: string): Arr | null {
let jsonValue = this.get(key);
if (jsonValue != null && jsonValue.isArr) {
return <Arr>jsonValue;
}
return null;
}
getObj(key: string): Obj | null {
let jsonValue = this.get(key);
if (jsonValue != null && jsonValue.isObj) {
return <Obj>jsonValue;
}
return null;
}
}
export function from<T>(val: T): Value {
if (isBoolean<T>(val)) {
return Value.Bool(<bool>val);
}
if (isInteger<T>(val)) {
return Value.Integer(val);
}
if (isFloat<T>(val)) {
return Value.Float(val);
}
if (isString<T>(val)) {
return Value.String(<string>val);
}
if (val == null) {
return Value.Null();
}
if (isArrayLike<T>(val)) {
const arr = Value.Array();
for (let i: i32 = 0; i < val.length; i++) {
// @ts-ignore
arr.push(from<valueof<T>>(val[i]));
}
return arr;
}
/**
* TODO: add object support.
*/
return Value.Object();
}
// @ts-ignore
@inline
/** Parses a string or Uint8Array and returns a Json Value. */
export function parse<T = Uint8Array>(str: T): Value {
return _JSON.parse(str);
}