-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.ts
36 lines (31 loc) · 834 Bytes
/
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
// functions for converting primities and objects/arrays to/from string representation
import {
itoa,
dtoa
} from 'internal/number'
export function stringify<T>(x: T): string {
if ( x instanceof bool ) {
if (x) {
return 'true'
} else {
return 'false'
}
} else if ( x == null) {
return 'null'
} else if(isString<T>(x)) {
return '"'+<string>x+'"';
} else if (isInteger<T>(x)) {
return itoa(x);
} else if (isFloat<T>(x)) {
return dtoa(x);
} else if (isArray<T>(x)) {
let result = '[';
for(let i = 0; i < x.length; i++) {
result += stringify(x[i]) + (i < x.length - 1 ? ',' : '')
}
return result+=']'
} else if (isReference<T>(x)) { // must be an object if it is a reference but not a string or array
return x.toString();
}
return 'unknown_type'
}