json-as 0.5.1 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/assembly/__benches__/as-json.ts +1 -1
- package/assembly/__tests__/as-json.spec.ts +140 -91
- package/assembly/index.ts +1 -1
- package/assembly/json.ts +374 -386
- package/assembly/test.ts +5 -9
- package/index.ts +1 -0
- package/package.json +2 -1
- package/transform/package.json +1 -1
- package/transform/src/index.ts +10 -4
- package/assembly/type.ts +0 -10
package/README.md
CHANGED
|
@@ -1,109 +1,158 @@
|
|
|
1
|
-
import { JSON } from ".."
|
|
1
|
+
import { JSON } from "..";
|
|
2
2
|
function canSerde<T>(data: T): void {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const serialized = JSON.stringify<T>(data);
|
|
4
|
+
const deserialized = JSON.stringify<T>(JSON.parse<T>(serialized));
|
|
5
|
+
expect(serialized).toBe(deserialized);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
// @ts-ignore
|
|
9
9
|
@json
|
|
10
|
-
class
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
class Vec3 {
|
|
11
|
+
x: f32;
|
|
12
|
+
y: f32;
|
|
13
|
+
z: f32;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
@json
|
|
18
|
+
class Player {
|
|
19
|
+
firstName: string;
|
|
20
|
+
lastName: string;
|
|
21
|
+
lastActive: i32[];
|
|
22
|
+
age: i32;
|
|
23
|
+
pos: Vec3 | null;
|
|
24
|
+
isVerified: boolean;
|
|
13
25
|
}
|
|
14
26
|
|
|
15
27
|
class Nullable {}
|
|
16
|
-
type Null = Nullable | null
|
|
28
|
+
type Null = Nullable | null;
|
|
17
29
|
|
|
18
30
|
describe("Ser/de Nulls", () => {
|
|
19
|
-
|
|
20
|
-
})
|
|
31
|
+
canSerde<Null>(null);
|
|
32
|
+
});
|
|
21
33
|
|
|
22
34
|
describe("Ser/de Numbers", () => {
|
|
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
|
-
}
|
|
35
|
+
it("should ser/de integers", () => {
|
|
36
|
+
canSerde<i32>(0);
|
|
37
|
+
|
|
38
|
+
canSerde<u32>(100);
|
|
39
|
+
canSerde<u64>(101);
|
|
40
|
+
canSerde<i32>(-100);
|
|
41
|
+
canSerde<i64>(-101);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should ser/de floats", () => {
|
|
45
|
+
canSerde<f64>(7.23);
|
|
46
|
+
canSerde<f64>(10e2);
|
|
47
|
+
canSerde<f64>(10e2);
|
|
48
|
+
|
|
49
|
+
canSerde<f64>(123456e-5);
|
|
50
|
+
|
|
51
|
+
canSerde<f64>(123456e-5);
|
|
52
|
+
|
|
53
|
+
canSerde<f64>(0.0);
|
|
54
|
+
canSerde<f64>(7.23);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should ser/de booleans", () => {
|
|
58
|
+
canSerde<bool>(true);
|
|
59
|
+
canSerde<bool>(false);
|
|
60
|
+
canSerde<boolean>(true);
|
|
61
|
+
canSerde<boolean>(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should ser/de strings", () => {
|
|
65
|
+
canSerde<string>("abcdefg");
|
|
66
|
+
canSerde<string>('st"ring" w""ith quotes"');
|
|
67
|
+
canSerde<string>(
|
|
68
|
+
'string \t\r\\"with ran\tdom spa\nces and \nnewlines\n\n\n'
|
|
69
|
+
);
|
|
70
|
+
canSerde<string>(
|
|
71
|
+
'string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"'
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
60
75
|
|
|
61
76
|
describe("Ser/de Array", () => {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
it("should ser/de integer arrays", () => {
|
|
78
|
+
canSerde<u32[]>([0, 100, 101]);
|
|
79
|
+
canSerde<u64[]>([0, 100, 101]);
|
|
80
|
+
|
|
81
|
+
canSerde<i32[]>([0, 100, 101, -100, -101]);
|
|
82
|
+
canSerde<i64[]>([0, 100, 101, -100, -101]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("should ser/de float arrays", () => {
|
|
86
|
+
canSerde<f64[]>([7.23, 10e2, 10e2, 123456e-5, 123456e-5, 0.0, 7.23]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should ser/de boolean arrays", () => {
|
|
90
|
+
canSerde<bool[]>([true, false]);
|
|
91
|
+
canSerde<boolean[]>([true, false]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("should ser/de string arrays", () => {
|
|
95
|
+
canSerde<string[]>([
|
|
96
|
+
"abcdefg",
|
|
97
|
+
'st"ring" w""ith quotes"',
|
|
98
|
+
'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
|
|
99
|
+
'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should ser/de nested integer arrays", () => {
|
|
104
|
+
canSerde<i64[][]>([[100, 101], [-100, -101], [0]]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("should ser/de float arrays", () => {
|
|
108
|
+
canSerde<f64[][]>([
|
|
109
|
+
[7.23],
|
|
110
|
+
[10e2],
|
|
111
|
+
[10e2],
|
|
112
|
+
[123456e-5],
|
|
113
|
+
[123456e-5],
|
|
114
|
+
[0.0],
|
|
115
|
+
[7.23],
|
|
116
|
+
]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("should ser/de boolean arrays", () => {
|
|
120
|
+
canSerde<bool[][]>([[true], [false]]);
|
|
121
|
+
canSerde<boolean[][]>([[true], [false]]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("should ser/de string arrays", () => {
|
|
125
|
+
canSerde<string[][]>([
|
|
126
|
+
["abcdefg"],
|
|
127
|
+
['st"ring" w""ith quotes"'],
|
|
128
|
+
['string \t\r\\"with ran\tdom spa\nces and \nnewlines\n\n\n'],
|
|
129
|
+
[
|
|
130
|
+
'string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"',
|
|
131
|
+
],
|
|
132
|
+
]);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
82
135
|
|
|
83
|
-
|
|
84
|
-
|
|
136
|
+
describe("Ser/de Objects", () => {
|
|
137
|
+
it("should ser/de Vec3 Objects", () => {
|
|
138
|
+
canSerde<Vec3>({
|
|
139
|
+
x: 3.4,
|
|
140
|
+
y: 1.2,
|
|
141
|
+
z: 8.3
|
|
85
142
|
});
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
143
|
+
});
|
|
144
|
+
it("should ser/de deep objects", () => {
|
|
145
|
+
canSerde<Player>({
|
|
146
|
+
firstName: "Emmet",
|
|
147
|
+
lastName: "West",
|
|
148
|
+
lastActive: [8, 27, 2022],
|
|
149
|
+
age: 23,
|
|
150
|
+
pos: {
|
|
151
|
+
x: 3.4,
|
|
152
|
+
y: 1.2,
|
|
153
|
+
z: 8.3
|
|
154
|
+
},
|
|
155
|
+
isVerified: true,
|
|
98
156
|
});
|
|
157
|
+
});
|
|
99
158
|
});
|
|
100
|
-
|
|
101
|
-
describe("Ser/de Objects", () => {
|
|
102
|
-
it("should ser/de Vec2 Objects", () => {
|
|
103
|
-
canSerde<Vec2>({
|
|
104
|
-
x: 3.4,
|
|
105
|
-
y: 1.2
|
|
106
|
-
})
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
})
|
package/assembly/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { JSON } from "./json";
|
|
1
|
+
export { JSON } from "./json";
|
package/assembly/json.ts
CHANGED
|
@@ -1,362 +1,350 @@
|
|
|
1
1
|
import { StringSink } from "as-string-sink/assembly";
|
|
2
2
|
import { isSpace } from "util/string";
|
|
3
|
-
import { Type } from "./type";
|
|
4
3
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
rightBraceCode,
|
|
17
|
-
rightBracketCode,
|
|
18
|
-
tCode,
|
|
19
|
-
uCode,
|
|
4
|
+
backSlashCode,
|
|
5
|
+
commaCode,
|
|
6
|
+
eCode,
|
|
7
|
+
fCode,
|
|
8
|
+
leftBraceCode,
|
|
9
|
+
leftBracketCode,
|
|
10
|
+
nCode,
|
|
11
|
+
quoteCode,
|
|
12
|
+
rightBraceCode,
|
|
13
|
+
rightBracketCode,
|
|
14
|
+
tCode,
|
|
20
15
|
} from "./chars";
|
|
21
|
-
import {
|
|
16
|
+
import { unsafeCharCodeAt } from "./util";
|
|
22
17
|
|
|
23
18
|
/**
|
|
24
19
|
* JSON Encoder/Decoder for AssemblyScript
|
|
25
20
|
*/
|
|
26
21
|
export class JSON {
|
|
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
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Stringifies valid JSON data.
|
|
24
|
+
* ```js
|
|
25
|
+
* JSON.stringify<T>(data)
|
|
26
|
+
* ```
|
|
27
|
+
* @param data T
|
|
28
|
+
* @returns string
|
|
29
|
+
*/
|
|
30
|
+
static stringify<T>(data: T): string {
|
|
31
|
+
// String
|
|
32
|
+
if (isString<T>()) {
|
|
33
|
+
return '"' + (<string>data).replaceAll('"', '\\"') + '"';
|
|
34
|
+
}
|
|
35
|
+
// Boolean
|
|
36
|
+
else if (isBoolean<T>()) {
|
|
37
|
+
return data ? "true" : "false";
|
|
38
|
+
}
|
|
39
|
+
// Nullable
|
|
40
|
+
else if (isNullable<T>() && data == null) {
|
|
41
|
+
return "null";
|
|
42
|
+
}
|
|
43
|
+
// Integers/Floats
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
else if ((isInteger<T>() || isFloat<T>()) && isFinite(data)) {
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
return data.toString();
|
|
48
|
+
}
|
|
49
|
+
// Class-Based serialization
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
else if (isDefined(data.__JSON_Serialize)) {
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
//if (isNullable<T>()) return "null";
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
return data.__JSON_Serialize();
|
|
56
|
+
}
|
|
57
|
+
// ArrayLike
|
|
58
|
+
else if (isArrayLike<T>()) {
|
|
59
|
+
let result = new StringSink("[");
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
if (data.length == 0) return "[]";
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
for (let i = 0; i < data.length - 1; i++) {
|
|
55
64
|
// @ts-ignore
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
let result = new StringSink("[");
|
|
65
|
-
// @ts-ignore
|
|
66
|
-
if (data.length == 0) return "[]";
|
|
67
|
-
// @ts-ignore
|
|
68
|
-
for (let i = 0; i < data.length - 1; i++) {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
result.write(JSON.stringify(unchecked(data[i])) + ",");
|
|
71
|
-
}
|
|
72
|
-
// @ts-ignore
|
|
73
|
-
result.write(JSON.stringify(unchecked(data[data.length - 1])));
|
|
74
|
-
result.write("]");
|
|
75
|
-
return result.toString();
|
|
76
|
-
} else {
|
|
77
|
-
return "null";
|
|
78
|
-
}
|
|
65
|
+
result.write(JSON.stringify(unchecked(data[i])) + ",");
|
|
66
|
+
}
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
result.write(JSON.stringify(unchecked(data[data.length - 1])));
|
|
69
|
+
result.write("]");
|
|
70
|
+
return result.toString();
|
|
71
|
+
} else {
|
|
72
|
+
return "null";
|
|
79
73
|
}
|
|
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
|
-
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Parses valid JSON strings into their original format.
|
|
77
|
+
* ```js
|
|
78
|
+
* JSON.parse<T>(data)
|
|
79
|
+
* ```
|
|
80
|
+
* @param data string
|
|
81
|
+
* @returns T
|
|
82
|
+
*/
|
|
83
|
+
static parse<T>(data: string): T {
|
|
84
|
+
let type!: T;
|
|
85
|
+
if (isString<T>()) {
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
return parseString(data);
|
|
88
|
+
} else if (isBoolean<T>()) {
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
return parseBoolean<T>(data);
|
|
91
|
+
} else if (isFloat<T>() || isInteger<T>()) {
|
|
92
|
+
return parseNumber<T>(data);
|
|
93
|
+
} else if (isArrayLike<T>()) {
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
return parseArray<T>(data.trimStart());
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
} else if (isNullable<T>() && data == "null") {
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
return null;
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
} else if (isDefined(type.__JSON_Deserialize)) {
|
|
102
|
+
return parseObject<T>(data.trimStart());
|
|
103
|
+
} else {
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
return null;
|
|
112
106
|
}
|
|
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
|
-
|
|
107
|
+
}
|
|
108
|
+
private static parseObjectValue<T>(data: string): T {
|
|
109
|
+
let type!: T;
|
|
110
|
+
if (isString<T>()) {
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
return data.replaceAll('\\"', '"');
|
|
113
|
+
} else if (isBoolean<T>()) {
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
return parseBoolean<T>(data);
|
|
116
|
+
} else if (isFloat<T>() || isInteger<T>()) {
|
|
117
|
+
return parseNumber<T>(data);
|
|
118
|
+
} else if (isArrayLike<T>()) {
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
return parseArray<T>(data);
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
} else if (isNullable<T>() && data == "null") {
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
return null;
|
|
125
|
+
// @ts-ignore
|
|
126
|
+
} else if (isDefined(type.__JSON_Deserialize)) {
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
//if (isNullable<T>()) return null;
|
|
129
|
+
return parseObject<T>(data);
|
|
130
|
+
} else {
|
|
131
|
+
// @ts-ignore
|
|
132
|
+
//return null;
|
|
133
|
+
throw new Error(`Could not parse value: ${data}`);
|
|
140
134
|
}
|
|
135
|
+
}
|
|
141
136
|
}
|
|
142
137
|
|
|
143
138
|
// @ts-ignore
|
|
144
139
|
@inline
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
function parseString(data: string): string {
|
|
141
|
+
return data.slice(1, data.length - 1).replaceAll('\\"', '"');
|
|
147
142
|
}
|
|
148
143
|
|
|
149
144
|
// @ts-ignore
|
|
150
145
|
@inline
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
146
|
+
function parseBoolean<T extends boolean>(data: string): T {
|
|
147
|
+
if (data.length > 3 && data.startsWith("true")) return <T>true;
|
|
148
|
+
else if (data.length > 4 && data.startsWith("false")) return <T>false;
|
|
149
|
+
else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
|
|
155
150
|
}
|
|
156
151
|
|
|
157
152
|
// @ts-ignore
|
|
158
153
|
@inline
|
|
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
|
-
|
|
154
|
+
function parseNumber<T>(data: string): T {
|
|
155
|
+
let type: T;
|
|
156
|
+
// @ts-ignore
|
|
157
|
+
if (type instanceof f64) return F64.parseFloat(data);
|
|
158
|
+
// @ts-ignore
|
|
159
|
+
else if (type instanceof f32) return F32.parseFloat(data);
|
|
160
|
+
// @ts-ignore
|
|
161
|
+
else if (type instanceof u64) return U64.parseInt(data);
|
|
162
|
+
// @ts-ignore
|
|
163
|
+
else if (type instanceof u32) return U32.parseInt(data);
|
|
164
|
+
// @ts-ignore
|
|
165
|
+
else if (type instanceof u8) return U8.parseInt(data);
|
|
166
|
+
// @ts-ignore
|
|
167
|
+
else if (type instanceof u16) return U16.parseInt(data);
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
else if (type instanceof i64) return I64.parseInt(data);
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
else if (type instanceof i32) return I32.parseInt(data);
|
|
172
|
+
// @ts-ignore
|
|
173
|
+
else if (type instanceof i16) return I16.parseInt(data);
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
else if (type instanceof i8) return I8.parseInt(data);
|
|
176
|
+
else
|
|
177
|
+
throw new Error(
|
|
178
|
+
`JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.`
|
|
179
|
+
);
|
|
185
180
|
}
|
|
186
181
|
|
|
187
182
|
// @ts-ignore
|
|
188
183
|
@inline
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
184
|
+
export function parseObject<T>(data: string): T {
|
|
185
|
+
let schema: T = changetype<T>(__new(offsetof<T>(), idof<T>()));
|
|
186
|
+
const result = new Map<string, string>();
|
|
187
|
+
let key = "";
|
|
188
|
+
let isKey = false;
|
|
189
|
+
let depth = 1;
|
|
190
|
+
let char = 0;
|
|
191
|
+
let outerLoopIndex = 1;
|
|
192
|
+
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
193
|
+
char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
194
|
+
if (char === leftBracketCode) {
|
|
195
|
+
for (
|
|
196
|
+
let arrayValueIndex = outerLoopIndex;
|
|
197
|
+
arrayValueIndex < data.length - 1;
|
|
198
|
+
arrayValueIndex++
|
|
199
|
+
) {
|
|
200
|
+
char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
199
201
|
if (char === leftBracketCode) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
++arrayValueIndex;
|
|
208
|
-
result.set(
|
|
209
|
-
key,
|
|
210
|
-
data.slice(outerLoopIndex, arrayValueIndex)
|
|
211
|
-
);
|
|
212
|
-
outerLoopIndex = arrayValueIndex;
|
|
213
|
-
isKey = false;
|
|
214
|
-
break;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
} else if (char === leftBraceCode) {
|
|
219
|
-
for (let objectValueIndex = outerLoopIndex; objectValueIndex < data.length - 1; objectValueIndex++) {
|
|
220
|
-
char = unsafeCharCodeAt(data, objectValueIndex);
|
|
221
|
-
if (char === leftBraceCode) {
|
|
222
|
-
depth = depth << 1;
|
|
223
|
-
} else if (char === rightBraceCode) {
|
|
224
|
-
depth = depth >> 1;
|
|
225
|
-
if (depth === 1) {
|
|
226
|
-
++objectValueIndex;
|
|
227
|
-
result.set(
|
|
228
|
-
key,
|
|
229
|
-
data.slice(outerLoopIndex, objectValueIndex)
|
|
230
|
-
);
|
|
231
|
-
outerLoopIndex = objectValueIndex;
|
|
232
|
-
isKey = false;
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
} else if (char === quoteCode) {
|
|
238
|
-
for (let stringValueIndex = ++outerLoopIndex; stringValueIndex < data.length - 1; stringValueIndex++) {
|
|
239
|
-
char = unsafeCharCodeAt(data, stringValueIndex);
|
|
240
|
-
if (
|
|
241
|
-
char === quoteCode &&
|
|
242
|
-
unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
|
|
243
|
-
) {
|
|
244
|
-
if (isKey === false) {
|
|
245
|
-
key = data.slice(outerLoopIndex, stringValueIndex);
|
|
246
|
-
isKey = true;
|
|
247
|
-
} else {
|
|
248
|
-
result.set(
|
|
249
|
-
key,
|
|
250
|
-
data.slice(outerLoopIndex, stringValueIndex)
|
|
251
|
-
);
|
|
252
|
-
isKey = false;
|
|
253
|
-
}
|
|
254
|
-
outerLoopIndex = ++stringValueIndex;
|
|
255
|
-
break;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
} else if (char == nCode) {
|
|
259
|
-
result.set(
|
|
260
|
-
key,
|
|
261
|
-
"null"
|
|
262
|
-
)
|
|
202
|
+
depth = depth << 1;
|
|
203
|
+
} else if (char === rightBracketCode) {
|
|
204
|
+
depth = depth >> 1;
|
|
205
|
+
if (depth === 1) {
|
|
206
|
+
++arrayValueIndex;
|
|
207
|
+
result.set(key, data.slice(outerLoopIndex, arrayValueIndex));
|
|
208
|
+
outerLoopIndex = arrayValueIndex;
|
|
263
209
|
isKey = false;
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
} else if (char === leftBraceCode) {
|
|
215
|
+
for (
|
|
216
|
+
let objectValueIndex = outerLoopIndex;
|
|
217
|
+
objectValueIndex < data.length - 1;
|
|
218
|
+
objectValueIndex++
|
|
219
|
+
) {
|
|
220
|
+
char = unsafeCharCodeAt(data, objectValueIndex);
|
|
221
|
+
if (char === leftBraceCode) {
|
|
222
|
+
depth = depth << 1;
|
|
223
|
+
} else if (char === rightBraceCode) {
|
|
224
|
+
depth = depth >> 1;
|
|
225
|
+
if (depth === 1) {
|
|
226
|
+
++objectValueIndex;
|
|
227
|
+
result.set(key, data.slice(outerLoopIndex, objectValueIndex));
|
|
228
|
+
outerLoopIndex = objectValueIndex;
|
|
271
229
|
isKey = false;
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
} else if (char === quoteCode) {
|
|
235
|
+
for (
|
|
236
|
+
let stringValueIndex = ++outerLoopIndex;
|
|
237
|
+
stringValueIndex < data.length - 1;
|
|
238
|
+
stringValueIndex++
|
|
239
|
+
) {
|
|
240
|
+
char = unsafeCharCodeAt(data, stringValueIndex);
|
|
241
|
+
if (
|
|
242
|
+
char === quoteCode &&
|
|
243
|
+
unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
|
|
278
244
|
) {
|
|
279
|
-
|
|
245
|
+
if (isKey === false) {
|
|
246
|
+
key = data.slice(outerLoopIndex, stringValueIndex);
|
|
247
|
+
isKey = true;
|
|
248
|
+
} else {
|
|
249
|
+
result.set(key, data.slice(outerLoopIndex, stringValueIndex));
|
|
280
250
|
isKey = false;
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
251
|
+
}
|
|
252
|
+
outerLoopIndex = ++stringValueIndex;
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
} else if (char == nCode) {
|
|
257
|
+
result.set(key, "null");
|
|
258
|
+
isKey = false;
|
|
259
|
+
} else if (
|
|
260
|
+
char === tCode &&
|
|
261
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "r".charCodeAt(0) &&
|
|
262
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "u".charCodeAt(0) &&
|
|
263
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
264
|
+
) {
|
|
265
|
+
result.set(key, "true");
|
|
266
|
+
isKey = false;
|
|
267
|
+
} else if (
|
|
268
|
+
char === fCode &&
|
|
269
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "a".charCodeAt(0) &&
|
|
270
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "l".charCodeAt(0) &&
|
|
271
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "s".charCodeAt(0) &&
|
|
272
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
273
|
+
) {
|
|
274
|
+
result.set(key, "false");
|
|
275
|
+
isKey = false;
|
|
276
|
+
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
277
|
+
let numberValueIndex = ++outerLoopIndex;
|
|
278
|
+
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
279
|
+
char = unsafeCharCodeAt(data, numberValueIndex);
|
|
280
|
+
if (char === commaCode || char === rightBraceCode || isSpace(char)) {
|
|
281
|
+
result.set(key, data.slice(outerLoopIndex - 1, numberValueIndex));
|
|
282
|
+
outerLoopIndex = numberValueIndex;
|
|
283
|
+
isKey = false;
|
|
284
|
+
break;
|
|
299
285
|
}
|
|
286
|
+
}
|
|
300
287
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
288
|
+
}
|
|
289
|
+
// @ts-ignore
|
|
290
|
+
//const deserialized =
|
|
291
|
+
//changetype<nonnull<T>>(schema).__JSON_Deserialize<T>(result);
|
|
292
|
+
//heap.free(changetype<usize>(schema));
|
|
293
|
+
return changetype<nonnull<T>>(schema).__JSON_Deserialize<T>(result);
|
|
305
294
|
}
|
|
306
295
|
|
|
307
296
|
// @ts-ignore
|
|
308
297
|
@inline
|
|
298
|
+
// @ts-ignore
|
|
299
|
+
export function parseArray<T extends unknown[]>(data: string): T {
|
|
300
|
+
let type!: valueof<T>;
|
|
301
|
+
if (type instanceof String) {
|
|
302
|
+
return <T>parseStringArray(data);
|
|
303
|
+
} else if (isBoolean<valueof<T>>()) {
|
|
309
304
|
// @ts-ignore
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
// @ts-ignore
|
|
323
|
-
return parseArrayArray<T>(data);
|
|
324
|
-
// @ts-ignore
|
|
325
|
-
} else if (isDefined(type.__JSON_Deserialize)) {
|
|
326
|
-
// @ts-ignore
|
|
327
|
-
return parseObjectArray<T>(data);
|
|
328
|
-
}
|
|
305
|
+
return parseBooleanArray<T>(data);
|
|
306
|
+
} else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
|
|
307
|
+
// @ts-ignore
|
|
308
|
+
return parseNumberArray<T>(data);
|
|
309
|
+
} else if (isArrayLike<valueof<T>>()) {
|
|
310
|
+
// @ts-ignore
|
|
311
|
+
return parseArrayArray<T>(data);
|
|
312
|
+
// @ts-ignore
|
|
313
|
+
} else if (isDefined(type.__JSON_Deserialize)) {
|
|
314
|
+
// @ts-ignore
|
|
315
|
+
return parseObjectArray<T>(data);
|
|
316
|
+
}
|
|
329
317
|
}
|
|
330
318
|
|
|
331
319
|
// @ts-ignore
|
|
332
320
|
@inline
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
321
|
+
export function parseStringArray(data: string): string[] {
|
|
322
|
+
const result: string[] = [];
|
|
323
|
+
let lastPos = 0;
|
|
324
|
+
let instr = false;
|
|
325
|
+
for (let i = 1; i < data.length - 1; i++) {
|
|
326
|
+
if (unsafeCharCodeAt(data, i) === quoteCode) {
|
|
327
|
+
if (instr === false) {
|
|
328
|
+
instr = true;
|
|
329
|
+
lastPos = i;
|
|
330
|
+
} else if (unsafeCharCodeAt(data, i - 1) !== backSlashCode) {
|
|
331
|
+
instr = false;
|
|
332
|
+
result.push(data.slice(lastPos + 1, i).replaceAll('\\"', '"'));
|
|
333
|
+
}
|
|
347
334
|
}
|
|
348
|
-
|
|
335
|
+
}
|
|
336
|
+
return result;
|
|
349
337
|
}
|
|
350
338
|
|
|
351
339
|
// @ts-ignore
|
|
352
340
|
@inline
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
341
|
+
export function parseBooleanArray<T extends boolean[]>(data: string): T {
|
|
342
|
+
const result = instantiate<T>();
|
|
343
|
+
let lastPos = 1;
|
|
344
|
+
let char = 0;
|
|
345
|
+
for (let i = 1; i < data.length - 1; i++) {
|
|
346
|
+
char = unsafeCharCodeAt(data, i);
|
|
347
|
+
/*// if char == "t" && i+3 == "e"
|
|
360
348
|
if (char === tCode && data.charCodeAt(i + 3) === eCode) {
|
|
361
349
|
//i += 3;
|
|
362
350
|
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+2)));
|
|
@@ -366,98 +354,98 @@ export class JSON {
|
|
|
366
354
|
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+3)));
|
|
367
355
|
//i++;
|
|
368
356
|
}*/
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
}
|
|
357
|
+
if (char === tCode || char === fCode) {
|
|
358
|
+
lastPos = i;
|
|
359
|
+
} else if (char === eCode) {
|
|
360
|
+
i++;
|
|
361
|
+
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i)));
|
|
375
362
|
}
|
|
376
|
-
|
|
363
|
+
}
|
|
364
|
+
return result;
|
|
377
365
|
}
|
|
378
366
|
|
|
379
367
|
// @ts-ignore
|
|
380
368
|
@inline
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
369
|
+
export function parseNumberArray<T extends number[]>(data: string): T {
|
|
370
|
+
const result = instantiate<T>();
|
|
371
|
+
let lastPos = 0;
|
|
372
|
+
let char = 0;
|
|
373
|
+
let i = 1;
|
|
374
|
+
for (; i < data.length - 1; i++) {
|
|
375
|
+
char = unsafeCharCodeAt(data, i);
|
|
376
|
+
if ((lastPos === 0 && char >= 48 && char <= 57) || char === 45) {
|
|
377
|
+
lastPos = i;
|
|
378
|
+
} else if ((isSpace(char) || char == commaCode) && lastPos > 0) {
|
|
379
|
+
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i)));
|
|
380
|
+
lastPos = 0;
|
|
394
381
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
382
|
+
}
|
|
383
|
+
for (; i > lastPos - 1; i--) {
|
|
384
|
+
char = unsafeCharCodeAt(data, i);
|
|
385
|
+
if (char !== rightBracketCode) {
|
|
386
|
+
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i + 1)));
|
|
387
|
+
break;
|
|
401
388
|
}
|
|
402
|
-
|
|
389
|
+
}
|
|
390
|
+
return result;
|
|
403
391
|
}
|
|
404
392
|
|
|
405
393
|
// @ts-ignore
|
|
406
394
|
@inline
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
}
|
|
395
|
+
export function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
396
|
+
const result = instantiate<T>();
|
|
397
|
+
let char = 0;
|
|
398
|
+
let lastPos = 0;
|
|
399
|
+
let depth = 1;
|
|
400
|
+
let i = 1;
|
|
401
|
+
// Find start of bracket
|
|
402
|
+
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
|
|
403
|
+
//i++;
|
|
404
|
+
for (; i < data.length - 1; i++) {
|
|
405
|
+
char = unsafeCharCodeAt(data, i);
|
|
406
|
+
if (char === leftBracketCode) {
|
|
407
|
+
if (depth === 1) {
|
|
408
|
+
lastPos = i;
|
|
409
|
+
}
|
|
410
|
+
// Shifting is 6% faster than incrementing
|
|
411
|
+
depth = depth << 1;
|
|
412
|
+
} else if (char === rightBracketCode) {
|
|
413
|
+
depth = depth >> 1;
|
|
414
|
+
if (depth === 1) {
|
|
415
|
+
i++;
|
|
416
|
+
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
|
|
417
|
+
}
|
|
431
418
|
}
|
|
432
|
-
|
|
419
|
+
}
|
|
420
|
+
return result;
|
|
433
421
|
}
|
|
434
422
|
|
|
435
423
|
// @ts-ignore
|
|
436
424
|
@inline
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
}
|
|
425
|
+
export function parseObjectArray<T extends unknown[][]>(data: string): T {
|
|
426
|
+
const result = instantiate<T>();
|
|
427
|
+
let char = 0;
|
|
428
|
+
let lastPos = 1;
|
|
429
|
+
let depth = 1;
|
|
430
|
+
let i = 1;
|
|
431
|
+
// Find start of bracket
|
|
432
|
+
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) { }
|
|
433
|
+
//i++;
|
|
434
|
+
for (; i < data.length - 1; i++) {
|
|
435
|
+
char = unsafeCharCodeAt(data, i);
|
|
436
|
+
if (char === leftBraceCode) {
|
|
437
|
+
if (depth === 1) {
|
|
438
|
+
lastPos = i;
|
|
439
|
+
}
|
|
440
|
+
// Shifting is 6% faster than incrementing
|
|
441
|
+
depth = depth << 1;
|
|
442
|
+
} else if (char === rightBraceCode) {
|
|
443
|
+
depth = depth >> 1;
|
|
444
|
+
if (depth === 1) {
|
|
445
|
+
i++;
|
|
446
|
+
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
|
|
447
|
+
}
|
|
461
448
|
}
|
|
462
|
-
|
|
449
|
+
}
|
|
450
|
+
return result;
|
|
463
451
|
}
|
package/assembly/test.ts
CHANGED
|
@@ -2,13 +2,13 @@ import "wasi";
|
|
|
2
2
|
import {
|
|
3
3
|
JSON
|
|
4
4
|
} from ".";
|
|
5
|
-
import { parseObject } from "./json";
|
|
6
5
|
|
|
7
6
|
// @ts-ignore
|
|
8
7
|
@json
|
|
9
|
-
class
|
|
8
|
+
class Vec3 {
|
|
10
9
|
x: f32;
|
|
11
10
|
y: f32;
|
|
11
|
+
z: f32;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
// @ts-ignore
|
|
@@ -18,7 +18,7 @@ class Player {
|
|
|
18
18
|
lastName: string;
|
|
19
19
|
lastActive: i32[];
|
|
20
20
|
age: i32;
|
|
21
|
-
pos:
|
|
21
|
+
pos: Vec3 | null;
|
|
22
22
|
isVerified: boolean;
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -30,16 +30,12 @@ const player: Player = {
|
|
|
30
30
|
pos: {
|
|
31
31
|
x: 3.4,
|
|
32
32
|
y: 1.2,
|
|
33
|
+
z: 8.3
|
|
33
34
|
},
|
|
34
35
|
isVerified: true,
|
|
35
36
|
};
|
|
36
37
|
|
|
37
|
-
const vec: Vec2 = {
|
|
38
|
-
x: 3.4,
|
|
39
|
-
y: 1.2
|
|
40
|
-
}
|
|
41
|
-
|
|
42
38
|
const serializedPlayer = JSON.stringify<Player>(player);
|
|
43
39
|
console.log("Serialized Player: " + serializedPlayer);
|
|
44
40
|
const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
|
|
45
|
-
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer))
|
|
41
|
+
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
|
package/index.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { JSON } from "./assembly/json";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"@serial-as/json": "^2.0.0",
|
|
26
26
|
"assemblyscript": "^0.20.7",
|
|
27
27
|
"assemblyscript-prettier": "^1.0.2",
|
|
28
|
+
"prettier": "^2.7.1",
|
|
28
29
|
"typescript": "^4.7.2"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -38,7 +38,9 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
// @ts-ignore
|
|
41
|
-
this.checkDecodeStmts.push(
|
|
41
|
+
this.checkDecodeStmts.push(
|
|
42
|
+
' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'
|
|
43
|
+
);
|
|
42
44
|
}
|
|
43
45
|
visitClassDeclaration(node: ClassDeclaration): void {
|
|
44
46
|
if (!node.members) {
|
|
@@ -76,11 +78,15 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
76
78
|
const deserializeFunc = `
|
|
77
79
|
@inline
|
|
78
80
|
__JSON_Deserialize<T>(values: Map<string, string>): T {
|
|
79
|
-
${
|
|
81
|
+
${
|
|
82
|
+
process.argv.includes("--debugJSON")
|
|
83
|
+
? this.checkDecodeStmts.join("else")
|
|
84
|
+
: ""
|
|
85
|
+
}
|
|
80
86
|
return {
|
|
81
87
|
${
|
|
82
|
-
|
|
83
|
-
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
this.decodeStmts.join("")
|
|
84
90
|
}
|
|
85
91
|
}
|
|
86
92
|
}
|