json-as 0.6.0 → 0.6.1
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 +2 -1
- package/assembly/src/json.ts +15 -12
- package/assembly/test.ts +1 -1
- package/package.json +1 -1
- package/transform/lib/index.js +29 -3
- package/transform/package.json +1 -1
- package/transform/src/index.ts +36 -3
package/README.md
CHANGED
|
@@ -59,7 +59,8 @@ const player: Player = {
|
|
|
59
59
|
isVerified: true
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
const stringified = JSON.stringify<Player>(player);
|
|
62
|
+
const stringified = JSON.stringify<Player>(player, true);
|
|
63
|
+
// You can toggle on setting default values with the 2nd parameter
|
|
63
64
|
// Alternative: use JSON.serializeTo(player, out);
|
|
64
65
|
|
|
65
66
|
const parsed = JSON.parse<Player>(stringified);
|
package/assembly/src/json.ts
CHANGED
|
@@ -193,7 +193,7 @@ export namespace JSON {
|
|
|
193
193
|
*/
|
|
194
194
|
|
|
195
195
|
// @ts-ignore: Decorator
|
|
196
|
-
@inline export function parse<T>(data: string): T {
|
|
196
|
+
@inline export function parse<T>(data: string, initializeDefaultValues: boolean = false): T {
|
|
197
197
|
let type: T;
|
|
198
198
|
if (isString<T>()) {
|
|
199
199
|
// @ts-ignore
|
|
@@ -212,7 +212,7 @@ export namespace JSON {
|
|
|
212
212
|
return null;
|
|
213
213
|
// @ts-ignore
|
|
214
214
|
} else if (isDefined(type.__JSON_Set_Key)) {
|
|
215
|
-
return parseObject<T>(data.trimStart());
|
|
215
|
+
return parseObject<T>(data.trimStart(), initializeDefaultValues);
|
|
216
216
|
} else if (idof<nonnull<T>>() == idof<Date>()) {
|
|
217
217
|
// @ts-ignore
|
|
218
218
|
return parseDate(data);
|
|
@@ -226,7 +226,7 @@ export namespace JSON {
|
|
|
226
226
|
}
|
|
227
227
|
|
|
228
228
|
// @ts-ignore: Decorator
|
|
229
|
-
@global @inline function __parseObjectValue<T>(data: string): T {
|
|
229
|
+
@global @inline function __parseObjectValue<T>(data: string, initializeDefaultValues: boolean): T {
|
|
230
230
|
let type: T;
|
|
231
231
|
if (isString<T>()) {
|
|
232
232
|
// @ts-ignore
|
|
@@ -289,7 +289,7 @@ export namespace JSON {
|
|
|
289
289
|
return null;
|
|
290
290
|
// @ts-ignore
|
|
291
291
|
} else if (isDefined(type.__JSON_Set_Key)) {
|
|
292
|
-
return parseObject<T>(data.trimStart());
|
|
292
|
+
return parseObject<T>(data.trimStart(), initializeDefaultValues);
|
|
293
293
|
} else if (idof<nonnull<T>>() == idof<Date>()) {
|
|
294
294
|
// @ts-ignore
|
|
295
295
|
return parseDate(data);
|
|
@@ -436,10 +436,13 @@ export namespace JSON {
|
|
|
436
436
|
}
|
|
437
437
|
|
|
438
438
|
// @ts-ignore: Decorator
|
|
439
|
-
@inline function parseObject<T>(data: string): T {
|
|
439
|
+
@inline function parseObject<T>(data: string, initializeDefaultValues: boolean): T {
|
|
440
440
|
let schema: nonnull<T> = changetype<nonnull<T>>(
|
|
441
441
|
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
442
442
|
);
|
|
443
|
+
// @ts-ignore
|
|
444
|
+
if (initializeDefaultValues) schema.__JSON_Initialize();
|
|
445
|
+
|
|
443
446
|
let key = Virtual.createEmpty<string>();
|
|
444
447
|
let isKey = false;
|
|
445
448
|
let depth = 0;
|
|
@@ -460,7 +463,7 @@ export namespace JSON {
|
|
|
460
463
|
if (depth === 0) {
|
|
461
464
|
++arrayValueIndex;
|
|
462
465
|
// @ts-ignore
|
|
463
|
-
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, arrayValueIndex);
|
|
466
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, arrayValueIndex, initializeDefaultValues);
|
|
464
467
|
outerLoopIndex = arrayValueIndex;
|
|
465
468
|
isKey = false;
|
|
466
469
|
break;
|
|
@@ -481,7 +484,7 @@ export namespace JSON {
|
|
|
481
484
|
if (depth === 0) {
|
|
482
485
|
++objectValueIndex;
|
|
483
486
|
// @ts-ignore
|
|
484
|
-
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, objectValueIndex);
|
|
487
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, objectValueIndex, initializeDefaultValues);
|
|
485
488
|
outerLoopIndex = objectValueIndex;
|
|
486
489
|
isKey = false;
|
|
487
490
|
break;
|
|
@@ -507,7 +510,7 @@ export namespace JSON {
|
|
|
507
510
|
isKey = true;
|
|
508
511
|
} else {
|
|
509
512
|
// @ts-ignore
|
|
510
|
-
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, stringValueIndex);
|
|
513
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, stringValueIndex, initializeDefaultValues);
|
|
511
514
|
isKey = false;
|
|
512
515
|
}
|
|
513
516
|
outerLoopIndex = ++stringValueIndex;
|
|
@@ -518,7 +521,7 @@ export namespace JSON {
|
|
|
518
521
|
}
|
|
519
522
|
} else if (char == nCode) {
|
|
520
523
|
// @ts-ignore
|
|
521
|
-
schema.__JSON_Set_Key<Virtual<string>>(key, nullWord, 0, 4);
|
|
524
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, nullWord, 0, 4, initializeDefaultValues);
|
|
522
525
|
isKey = false;
|
|
523
526
|
} else if (
|
|
524
527
|
char === tCode &&
|
|
@@ -527,7 +530,7 @@ export namespace JSON {
|
|
|
527
530
|
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
528
531
|
) {
|
|
529
532
|
// @ts-ignore
|
|
530
|
-
schema.__JSON_Set_Key<Virtual<string>>(key, trueWord, 0, 4);
|
|
533
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, trueWord, 0, 4, initializeDefaultValues);
|
|
531
534
|
isKey = false;
|
|
532
535
|
} else if (
|
|
533
536
|
char === fCode &&
|
|
@@ -537,7 +540,7 @@ export namespace JSON {
|
|
|
537
540
|
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
538
541
|
) {
|
|
539
542
|
// @ts-ignore
|
|
540
|
-
schema.__JSON_Set_Key<Virtual<string>>(key, falseWord, 0, 5);
|
|
543
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, falseWord, 0, 5, initializeDefaultValues);
|
|
541
544
|
isKey = false;
|
|
542
545
|
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
543
546
|
let numberValueIndex = ++outerLoopIndex;
|
|
@@ -545,7 +548,7 @@ export namespace JSON {
|
|
|
545
548
|
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
546
549
|
if (char === commaCode || char === rightBraceCode || isSpace(char)) {
|
|
547
550
|
// @ts-ignore
|
|
548
|
-
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex - 1, numberValueIndex);
|
|
551
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex - 1, numberValueIndex, initializeDefaultValues);
|
|
549
552
|
outerLoopIndex = numberValueIndex;
|
|
550
553
|
isKey = false;
|
|
551
554
|
break;
|
package/assembly/test.ts
CHANGED
|
@@ -40,7 +40,7 @@ JSON.stringifyTo(vec, out);
|
|
|
40
40
|
|
|
41
41
|
console.log("Original: " + out);
|
|
42
42
|
//console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
|
|
43
|
-
console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{}')));
|
|
43
|
+
console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{}', true)));
|
|
44
44
|
|
|
45
45
|
console.log("Original: " + JSON.stringify(player));
|
|
46
46
|
//console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
|
package/package.json
CHANGED
package/transform/lib/index.js
CHANGED
|
@@ -10,6 +10,7 @@ class SchemaData {
|
|
|
10
10
|
this.parent = "";
|
|
11
11
|
this.encodeStmts = [];
|
|
12
12
|
this.setDataStmts = [];
|
|
13
|
+
this.initializeStmts = [];
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
class AsJSONTransform extends BaseVisitor {
|
|
@@ -49,6 +50,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
49
50
|
node: node,
|
|
50
51
|
encodeStmts: [],
|
|
51
52
|
setDataStmts: [],
|
|
53
|
+
initializeStmts: []
|
|
52
54
|
};
|
|
53
55
|
if (this.currentClass.parent.length > 0) {
|
|
54
56
|
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
@@ -100,6 +102,9 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
100
102
|
return;
|
|
101
103
|
}
|
|
102
104
|
`);
|
|
105
|
+
if (member.initializer) {
|
|
106
|
+
this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
|
|
107
|
+
}
|
|
103
108
|
}
|
|
104
109
|
else // @ts-ignore
|
|
105
110
|
if ([
|
|
@@ -109,19 +114,25 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
109
114
|
this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
|
|
110
115
|
// @ts-ignore
|
|
111
116
|
this.currentClass.setDataStmts.push(`if (key.equals("${name}")) {
|
|
112
|
-
this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end));
|
|
117
|
+
this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
|
|
113
118
|
return;
|
|
114
119
|
}
|
|
115
120
|
`);
|
|
121
|
+
if (member.initializer) {
|
|
122
|
+
this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
|
|
123
|
+
}
|
|
116
124
|
}
|
|
117
125
|
else {
|
|
118
126
|
this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
|
|
119
127
|
// @ts-ignore
|
|
120
128
|
this.currentClass.setDataStmts.push(`if (key.equals("${name}")) {
|
|
121
|
-
this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data);
|
|
129
|
+
this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
|
|
122
130
|
return;
|
|
123
131
|
}
|
|
124
132
|
`);
|
|
133
|
+
if (member.initializer) {
|
|
134
|
+
this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
|
|
135
|
+
}
|
|
125
136
|
}
|
|
126
137
|
}
|
|
127
138
|
}
|
|
@@ -146,16 +157,31 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
146
157
|
// Odd behavior here... When pairing this transform with asyncify, having @inline on __JSON_Set_Key<T> with a generic will cause it to freeze.
|
|
147
158
|
// Binaryen cannot predict and add/mangle code when it is genericed.
|
|
148
159
|
const setKeyFunc = `
|
|
149
|
-
__JSON_Set_Key<T>(key: T, data: string, val_start: i32, val_end: i32): void {
|
|
160
|
+
__JSON_Set_Key<T>(key: T, data: string, val_start: i32, val_end: i32, initializeDefaultValues: boolean): void {
|
|
150
161
|
${
|
|
151
162
|
// @ts-ignore
|
|
152
163
|
this.currentClass.setDataStmts.join("")}
|
|
153
164
|
}
|
|
154
165
|
`;
|
|
166
|
+
let initializeFunc = "";
|
|
167
|
+
if (this.currentClass.initializeStmts.length > 0) {
|
|
168
|
+
initializeFunc = `
|
|
169
|
+
@inline __JSON_Initialize(): void {
|
|
170
|
+
${this.currentClass.initializeStmts.join(";\n")};
|
|
171
|
+
}
|
|
172
|
+
`;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
initializeFunc = `
|
|
176
|
+
@inline __JSON_Initialize(): void {}
|
|
177
|
+
`;
|
|
178
|
+
}
|
|
155
179
|
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
156
180
|
node.members.push(serializeMethod);
|
|
157
181
|
const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node);
|
|
158
182
|
node.members.push(setDataMethod);
|
|
183
|
+
const initializeMethod = SimpleParser.parseClassMember(initializeFunc, node);
|
|
184
|
+
node.members.push(initializeMethod);
|
|
159
185
|
this.schemasList.push(this.currentClass);
|
|
160
186
|
//console.log(toString(node));
|
|
161
187
|
}
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -17,6 +17,7 @@ class SchemaData {
|
|
|
17
17
|
public node!: ClassDeclaration;
|
|
18
18
|
public encodeStmts: string[] = [];
|
|
19
19
|
public setDataStmts: string[] = [];
|
|
20
|
+
public initializeStmts: string[] = [];
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
class AsJSONTransform extends BaseVisitor {
|
|
@@ -54,6 +55,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
54
55
|
node: node,
|
|
55
56
|
encodeStmts: [],
|
|
56
57
|
setDataStmts: [],
|
|
58
|
+
initializeStmts: []
|
|
57
59
|
};
|
|
58
60
|
|
|
59
61
|
if (this.currentClass.parent.length > 0) {
|
|
@@ -119,6 +121,11 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
119
121
|
}
|
|
120
122
|
`
|
|
121
123
|
);
|
|
124
|
+
if (member.initializer) {
|
|
125
|
+
this.currentClass.initializeStmts.push(
|
|
126
|
+
`this.${name} = ${toString(member.initializer)}`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
122
129
|
} else // @ts-ignore
|
|
123
130
|
if (
|
|
124
131
|
[
|
|
@@ -132,11 +139,16 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
132
139
|
// @ts-ignore
|
|
133
140
|
this.currentClass.setDataStmts.push(
|
|
134
141
|
`if (key.equals("${name}")) {
|
|
135
|
-
this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end));
|
|
142
|
+
this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
|
|
136
143
|
return;
|
|
137
144
|
}
|
|
138
145
|
`
|
|
139
146
|
);
|
|
147
|
+
if (member.initializer) {
|
|
148
|
+
this.currentClass.initializeStmts.push(
|
|
149
|
+
`this.${name} = ${toString(member.initializer)}`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
140
152
|
} else {
|
|
141
153
|
this.currentClass.encodeStmts.push(
|
|
142
154
|
`"${name}":\${JSON.stringify<${type}>(this.${name})},`
|
|
@@ -144,11 +156,16 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
144
156
|
// @ts-ignore
|
|
145
157
|
this.currentClass.setDataStmts.push(
|
|
146
158
|
`if (key.equals("${name}")) {
|
|
147
|
-
this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data);
|
|
159
|
+
this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
|
|
148
160
|
return;
|
|
149
161
|
}
|
|
150
162
|
`
|
|
151
163
|
);
|
|
164
|
+
if (member.initializer) {
|
|
165
|
+
this.currentClass.initializeStmts.push(
|
|
166
|
+
`this.${name} = ${toString(member.initializer)}`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
152
169
|
}
|
|
153
170
|
}
|
|
154
171
|
}
|
|
@@ -178,7 +195,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
178
195
|
// Odd behavior here... When pairing this transform with asyncify, having @inline on __JSON_Set_Key<T> with a generic will cause it to freeze.
|
|
179
196
|
// Binaryen cannot predict and add/mangle code when it is genericed.
|
|
180
197
|
const setKeyFunc = `
|
|
181
|
-
__JSON_Set_Key<T>(key: T, data: string, val_start: i32, val_end: i32): void {
|
|
198
|
+
__JSON_Set_Key<T>(key: T, data: string, val_start: i32, val_end: i32, initializeDefaultValues: boolean): void {
|
|
182
199
|
${
|
|
183
200
|
// @ts-ignore
|
|
184
201
|
this.currentClass.setDataStmts.join("")
|
|
@@ -186,12 +203,28 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
186
203
|
}
|
|
187
204
|
`;
|
|
188
205
|
|
|
206
|
+
let initializeFunc = "";
|
|
207
|
+
|
|
208
|
+
if (this.currentClass.initializeStmts.length > 0) {
|
|
209
|
+
initializeFunc = `
|
|
210
|
+
@inline __JSON_Initialize(): void {
|
|
211
|
+
${this.currentClass.initializeStmts.join(";\n")};
|
|
212
|
+
}
|
|
213
|
+
`;
|
|
214
|
+
} else {
|
|
215
|
+
initializeFunc = `
|
|
216
|
+
@inline __JSON_Initialize(): void {}
|
|
217
|
+
`;
|
|
218
|
+
}
|
|
189
219
|
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
190
220
|
node.members.push(serializeMethod);
|
|
191
221
|
|
|
192
222
|
const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node);
|
|
193
223
|
node.members.push(setDataMethod);
|
|
194
224
|
|
|
225
|
+
const initializeMethod = SimpleParser.parseClassMember(initializeFunc, node);
|
|
226
|
+
node.members.push(initializeMethod);
|
|
227
|
+
|
|
195
228
|
this.schemasList.push(this.currentClass);
|
|
196
229
|
//console.log(toString(node));
|
|
197
230
|
}
|