json-as 0.5.2 → 0.5.4
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 +3 -2
- package/assembly/__benches__/as-json.ts +1 -1
- package/assembly/__tests__/as-json.spec.ts +140 -91
- package/assembly/dynamic/dynamic.ts +3 -0
- package/assembly/index.ts +1 -1
- package/assembly/json.ts +373 -385
- package/assembly/{chars.ts → src/chars.ts} +9 -0
- package/assembly/src/json.ts +455 -0
- package/assembly/{util.ts → src/util.ts} +0 -0
- package/assembly/test.ts +5 -8
- package/index.ts +1 -0
- package/package.json +3 -2
- package/transform/lib/index.js +47 -18
- package/transform/package.json +1 -1
- package/transform/src/index.ts +47 -17
- package/assembly/type.ts +0 -13
package/transform/src/index.ts
CHANGED
|
@@ -14,8 +14,9 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
14
14
|
public currentClass!: ClassDeclaration;
|
|
15
15
|
public sources: Source[] = [];
|
|
16
16
|
public encodeStmts: string[] = [];
|
|
17
|
-
public decodeStmts: string[] = [];
|
|
18
|
-
public
|
|
17
|
+
//public decodeStmts: string[] = [];
|
|
18
|
+
public setDataStmts: string[] = [];
|
|
19
|
+
//public checkDecodeStmts: string[] = [];
|
|
19
20
|
|
|
20
21
|
visitMethodDeclaration(): void {}
|
|
21
22
|
visitFieldDeclaration(node: FieldDeclaration): void {
|
|
@@ -33,12 +34,23 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
33
34
|
);
|
|
34
35
|
|
|
35
36
|
// @ts-ignore
|
|
36
|
-
this.decodeStmts.push(
|
|
37
|
-
|
|
37
|
+
//this.decodeStmts.push(
|
|
38
|
+
// `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
|
|
39
|
+
//);
|
|
40
|
+
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
this.setDataStmts.push(
|
|
43
|
+
`if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) {
|
|
44
|
+
this.${name} = JSON.parseObjectValue<${type}>(value);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
`
|
|
38
48
|
);
|
|
39
49
|
|
|
40
50
|
// @ts-ignore
|
|
41
|
-
this.checkDecodeStmts.push(
|
|
51
|
+
//this.checkDecodeStmts.push(
|
|
52
|
+
// ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'
|
|
53
|
+
//);
|
|
42
54
|
}
|
|
43
55
|
visitClassDeclaration(node: ClassDeclaration): void {
|
|
44
56
|
if (!node.members) {
|
|
@@ -60,33 +72,46 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
60
72
|
stmt.length - 1
|
|
61
73
|
);
|
|
62
74
|
serializeFunc = `
|
|
63
|
-
@inline
|
|
64
75
|
__JSON_Serialize(): string {
|
|
65
76
|
return \`{${this.encodeStmts.join("")}}\`;
|
|
66
77
|
}
|
|
67
78
|
`;
|
|
68
79
|
} else {
|
|
69
80
|
serializeFunc = `
|
|
70
|
-
@inline
|
|
71
81
|
__JSON_Serialize(): string {
|
|
72
82
|
return "{}";
|
|
73
83
|
}
|
|
74
84
|
`;
|
|
75
85
|
}
|
|
76
|
-
const deserializeFunc = `
|
|
77
|
-
@inline
|
|
86
|
+
/*const deserializeFunc = `
|
|
78
87
|
__JSON_Deserialize<T>(values: Map<string, string>): T {
|
|
79
|
-
${
|
|
88
|
+
${
|
|
89
|
+
process.argv.includes("--debugJSON")
|
|
90
|
+
? this.checkDecodeStmts.join("else")
|
|
91
|
+
: ""
|
|
92
|
+
}
|
|
80
93
|
return {
|
|
81
94
|
${
|
|
82
|
-
|
|
83
|
-
|
|
95
|
+
// @ts-ignore
|
|
96
|
+
this.decodeStmts.join("")
|
|
84
97
|
}
|
|
85
98
|
}
|
|
86
99
|
}
|
|
87
|
-
|
|
100
|
+
`;*/
|
|
101
|
+
const setKeyFunc = `
|
|
102
|
+
__JSON_Set_Key(key: string, value: string): void {
|
|
103
|
+
${
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
this.setDataStmts.join("")
|
|
106
|
+
}
|
|
107
|
+
throw new Error("Cannot find key: " + key);
|
|
108
|
+
}
|
|
109
|
+
`
|
|
110
|
+
//console.log(setKeyFunc, deserializeFunc, serializeFunc)
|
|
88
111
|
this.encodeStmts = [];
|
|
89
|
-
this.decodeStmts = [];
|
|
112
|
+
//this.decodeStmts = [];
|
|
113
|
+
this.setDataStmts = [];
|
|
114
|
+
//this.checkDecodeStmts = [];
|
|
90
115
|
const serializedProperty = SimpleParser.parseClassMember(
|
|
91
116
|
serializedProp,
|
|
92
117
|
node
|
|
@@ -96,11 +121,16 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
96
121
|
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
97
122
|
node.members.push(serializeMethod);
|
|
98
123
|
|
|
99
|
-
const deserializeMethod = SimpleParser.parseClassMember(
|
|
100
|
-
|
|
124
|
+
//const deserializeMethod = SimpleParser.parseClassMember(
|
|
125
|
+
// deserializeFunc,
|
|
126
|
+
// node
|
|
127
|
+
//);
|
|
128
|
+
//node.members.push(deserializeMethod);
|
|
129
|
+
const setDataMethod = SimpleParser.parseClassMember(
|
|
130
|
+
setKeyFunc,
|
|
101
131
|
node
|
|
102
132
|
);
|
|
103
|
-
node.members.push(
|
|
133
|
+
node.members.push(setDataMethod);
|
|
104
134
|
}
|
|
105
135
|
get name(): string {
|
|
106
136
|
return "json";
|
package/assembly/type.ts
DELETED