json-as 0.5.17 → 0.5.19
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/asconfig.json +5 -2
- package/assembly/Candle.ts +18 -0
- package/assembly/src/json.ts +1 -1
- package/assembly/test.ts +2 -25
- package/package.json +1 -1
- package/transform/lib/index.js +11 -19
- package/transform/package.json +1 -1
- package/transform/src/index.ts +11 -25
package/asconfig.json
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JSON
|
|
3
|
+
} from ".";
|
|
4
|
+
@json
|
|
5
|
+
export class Candle {
|
|
6
|
+
timestamp!: i64;
|
|
7
|
+
high!: f64;
|
|
8
|
+
low!: f64;
|
|
9
|
+
open!: f64;
|
|
10
|
+
close!: f64;
|
|
11
|
+
constructor(timestamp: i64, high: f64, low: f64, open: f64, close: f64) {
|
|
12
|
+
this.timestamp = timestamp;
|
|
13
|
+
this.high = high;
|
|
14
|
+
this.low = low;
|
|
15
|
+
this.open = open;
|
|
16
|
+
this.close = close;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/assembly/src/json.ts
CHANGED
package/assembly/test.ts
CHANGED
|
@@ -1,32 +1,9 @@
|
|
|
1
1
|
import { u128 } from "as-bignum/assembly";
|
|
2
|
+
import { Candle } from "./Candle";
|
|
2
3
|
import {
|
|
3
4
|
JSON
|
|
4
5
|
} from ".";
|
|
5
|
-
import { parseObjectArray } from "./src/json";
|
|
6
6
|
|
|
7
|
-
@json
|
|
8
|
-
class Candle {
|
|
9
|
-
timestamp!: i64;
|
|
10
|
-
high!: f64;
|
|
11
|
-
low!: f64;
|
|
12
|
-
open!: f64;
|
|
13
|
-
close!: f64;
|
|
14
|
-
constructor(timestamp: i64, high: f64, low: f64, open: f64, close: f64) {
|
|
15
|
-
this.timestamp = timestamp;
|
|
16
|
-
this.high = high;
|
|
17
|
-
this.low = low;
|
|
18
|
-
this.open = open;
|
|
19
|
-
this.close = close;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
//console.log(JSON.stringify(candle));
|
|
24
|
-
|
|
25
|
-
const parsedCandleArray: Candle[] = parseObjectArray<Candle[]>('{"timestamp":1620248400000,"high":573.3476371851107,"low":572.7653897862947,"open":573.3476371851107,"close":572.7653897862947},{"timestamp":1620249300000,"high":572.4966600947654,"low":572.3690847959957,"open":572.4966600947654,"close":572.3690847959957}');
|
|
26
|
-
|
|
27
|
-
console.log(JSON.stringify(parsedCandleArray[0]));
|
|
28
|
-
console.log(JSON.stringify(parsedCandleArray[1]));
|
|
29
|
-
/*
|
|
30
7
|
// @ts-ignore
|
|
31
8
|
@JSON
|
|
32
9
|
class Vec3 {
|
|
@@ -72,4 +49,4 @@ const player: Player = {
|
|
|
72
49
|
const serializedPlayer = JSON.stringify<Player>(player);
|
|
73
50
|
console.log("Serialized Player: " + serializedPlayer);
|
|
74
51
|
const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
|
|
75
|
-
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer))
|
|
52
|
+
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
|
package/package.json
CHANGED
package/transform/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getName, toString } from "visitor-as/dist/utils.js";
|
|
1
|
+
import { getName, toString, isStdlib } from "visitor-as/dist/utils.js";
|
|
2
2
|
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
|
|
3
3
|
import { Transform } from "assemblyscript/dist/transform.js";
|
|
4
4
|
class SchemaData {
|
|
@@ -48,7 +48,8 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
48
48
|
//);
|
|
49
49
|
}
|
|
50
50
|
visitClassDeclaration(node) {
|
|
51
|
-
var _a
|
|
51
|
+
var _a;
|
|
52
|
+
const className = node.name.text;
|
|
52
53
|
if (!((_a = node.decorators) === null || _a === void 0 ? void 0 : _a.length))
|
|
53
54
|
return;
|
|
54
55
|
let foundDecorator = false;
|
|
@@ -59,16 +60,13 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
59
60
|
}
|
|
60
61
|
if (!foundDecorator)
|
|
61
62
|
return;
|
|
62
|
-
if (!node.members) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
63
|
// Prevent from being triggered twice
|
|
66
64
|
for (const member of node.members) {
|
|
67
65
|
if (member.name.text == "__JSON_Serialize")
|
|
68
66
|
return;
|
|
69
67
|
}
|
|
70
68
|
this.currentClass = {
|
|
71
|
-
name:
|
|
69
|
+
name: className,
|
|
72
70
|
keys: [],
|
|
73
71
|
values: [],
|
|
74
72
|
types: [],
|
|
@@ -78,21 +76,16 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
78
76
|
setDataStmts: []
|
|
79
77
|
};
|
|
80
78
|
if (this.currentClass.parent.length > 0) {
|
|
81
|
-
const parentSchema = this.schemasList.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
});
|
|
86
|
-
if (parentSchema.length > 0 && ((_b = parentSchema[0]) === null || _b === void 0 ? void 0 : _b.encodeStmts)) {
|
|
87
|
-
(_c = parentSchema[0]) === null || _c === void 0 ? void 0 : _c.encodeStmts.push(((_d = parentSchema[0]) === null || _d === void 0 ? void 0 : _d.encodeStmts.pop()) + ",");
|
|
88
|
-
this.currentClass.encodeStmts.push(...(_e = parentSchema[0]) === null || _e === void 0 ? void 0 : _e.encodeStmts);
|
|
79
|
+
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
80
|
+
if (parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts) {
|
|
81
|
+
parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts.push((parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts.pop()) + ",");
|
|
82
|
+
this.currentClass.encodeStmts.push(...parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts);
|
|
89
83
|
}
|
|
90
84
|
else {
|
|
91
|
-
|
|
85
|
+
console.error("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?");
|
|
92
86
|
}
|
|
93
87
|
}
|
|
94
88
|
this.visit(node.members);
|
|
95
|
-
// const serializedProp = '__JSON_Serialized: string = "";';
|
|
96
89
|
let serializeFunc = "";
|
|
97
90
|
if (this.currentClass.encodeStmts.length > 0) {
|
|
98
91
|
const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];
|
|
@@ -117,7 +110,6 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
117
110
|
this.currentClass.setDataStmts.join("")}
|
|
118
111
|
}
|
|
119
112
|
`;
|
|
120
|
-
//console.log(serializeFunc, setKeyFunc)
|
|
121
113
|
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
122
114
|
node.members.push(serializeMethod);
|
|
123
115
|
const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node);
|
|
@@ -135,8 +127,8 @@ export default class Transformer extends Transform {
|
|
|
135
127
|
const transformer = new AsJSONTransform();
|
|
136
128
|
// Loop over every source
|
|
137
129
|
for (const source of parser.sources) {
|
|
138
|
-
// Ignore all lib
|
|
139
|
-
if (!source
|
|
130
|
+
// Ignore all lib and std. Visit everything else.
|
|
131
|
+
if (!isStdlib(source)) {
|
|
140
132
|
transformer.visit(source);
|
|
141
133
|
}
|
|
142
134
|
}
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -4,11 +4,7 @@ import {
|
|
|
4
4
|
Source,
|
|
5
5
|
Parser
|
|
6
6
|
} from "assemblyscript/dist/assemblyscript";
|
|
7
|
-
import {
|
|
8
|
-
ClassDecorator,
|
|
9
|
-
registerDecorator,
|
|
10
|
-
} from "visitor-as/dist/decorator.js";
|
|
11
|
-
import { getName, toString } from "visitor-as/dist/utils.js";
|
|
7
|
+
import { getName, toString, isStdlib } from "visitor-as/dist/utils.js";
|
|
12
8
|
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
|
|
13
9
|
import { Transform } from "assemblyscript/dist/transform.js";
|
|
14
10
|
|
|
@@ -63,6 +59,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
63
59
|
//);
|
|
64
60
|
}
|
|
65
61
|
visitClassDeclaration(node: ClassDeclaration): void {
|
|
62
|
+
const className = node.name.text;
|
|
66
63
|
if (!node.decorators?.length) return;
|
|
67
64
|
let foundDecorator = false;
|
|
68
65
|
for (const decorator of node.decorators!) {
|
|
@@ -70,9 +67,6 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
70
67
|
if (decorator.name.text.toLowerCase() == "json" || decorator.name.text.toLowerCase() == "serializable") foundDecorator = true;
|
|
71
68
|
}
|
|
72
69
|
if (!foundDecorator) return;
|
|
73
|
-
if (!node.members) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
70
|
|
|
77
71
|
// Prevent from being triggered twice
|
|
78
72
|
for (const member of node.members) {
|
|
@@ -80,7 +74,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
80
74
|
}
|
|
81
75
|
|
|
82
76
|
this.currentClass = {
|
|
83
|
-
name:
|
|
77
|
+
name: className,
|
|
84
78
|
keys: [],
|
|
85
79
|
values: [],
|
|
86
80
|
types: [],
|
|
@@ -89,25 +83,19 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
89
83
|
encodeStmts: [],
|
|
90
84
|
setDataStmts: []
|
|
91
85
|
}
|
|
92
|
-
|
|
86
|
+
|
|
93
87
|
if (this.currentClass.parent.length > 0) {
|
|
94
|
-
const parentSchema = this.schemasList.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
});
|
|
99
|
-
if (parentSchema.length > 0 && parentSchema[0]?.encodeStmts) {
|
|
100
|
-
parentSchema[0]?.encodeStmts.push(parentSchema[0]?.encodeStmts.pop() + ",")
|
|
101
|
-
this.currentClass.encodeStmts.push(...parentSchema[0]?.encodeStmts)
|
|
88
|
+
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
89
|
+
if (parentSchema?.encodeStmts) {
|
|
90
|
+
parentSchema?.encodeStmts.push(parentSchema?.encodeStmts.pop() + ",");
|
|
91
|
+
this.currentClass.encodeStmts.push(...parentSchema?.encodeStmts);
|
|
102
92
|
} else {
|
|
103
|
-
|
|
93
|
+
console.error("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?");
|
|
104
94
|
}
|
|
105
95
|
}
|
|
106
96
|
|
|
107
97
|
this.visit(node.members);
|
|
108
98
|
|
|
109
|
-
// const serializedProp = '__JSON_Serialized: string = "";';
|
|
110
|
-
|
|
111
99
|
let serializeFunc = "";
|
|
112
100
|
|
|
113
101
|
if (this.currentClass.encodeStmts.length > 0) {
|
|
@@ -138,8 +126,6 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
138
126
|
}
|
|
139
127
|
`
|
|
140
128
|
|
|
141
|
-
//console.log(serializeFunc, setKeyFunc)
|
|
142
|
-
|
|
143
129
|
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
144
130
|
node.members.push(serializeMethod);
|
|
145
131
|
|
|
@@ -163,8 +149,8 @@ export default class Transformer extends Transform {
|
|
|
163
149
|
const transformer = new AsJSONTransform();
|
|
164
150
|
// Loop over every source
|
|
165
151
|
for (const source of parser.sources) {
|
|
166
|
-
// Ignore all lib
|
|
167
|
-
if (!source
|
|
152
|
+
// Ignore all lib and std. Visit everything else.
|
|
153
|
+
if (!isStdlib(source)) {
|
|
168
154
|
transformer.visit(source);
|
|
169
155
|
}
|
|
170
156
|
}
|