json-as 0.5.18 → 0.5.20
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 -20
- package/asconfig.json +2 -5
- package/assembly/__tests__/as-json.spec.ts +4 -3
- package/assembly/src/json.ts +8 -8
- package/assembly/test.ts +10 -26
- package/package.json +3 -3
- package/transform/lib/index.js +11 -19
- package/transform/package.json +1 -1
- package/transform/src/index.ts +11 -25
package/README.md
CHANGED
|
@@ -73,28 +73,10 @@ const stringified = JSON.stringify<Player>(player);
|
|
|
73
73
|
const parsed = JSON.parse<Player>(stringified);
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
#
|
|
76
|
+
# Notes
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
Performance is nearly equal to the JavaScript JSON implementation which is in C++.
|
|
79
79
|
|
|
80
|
-
Yes, it does. However, dynamic objects and arrays are not supported, but planned in the near future.
|
|
81
|
-
|
|
82
|
-
**Is it fast?**
|
|
83
|
-
|
|
84
|
-
Look below
|
|
85
|
-
|
|
86
|
-
**How does it compare to other libs?**
|
|
87
|
-
|
|
88
|
-
Its pretty much the same as the other libraries out there (near/assemblyscript-json and @serial-as/json), but it focuses highly on performance
|
|
89
|
-
|
|
90
|
-
**Will it catch invalid JSON?**
|
|
91
|
-
|
|
92
|
-
No, it does not check for invalid JSON, but gives its best shot at parsing instead. Will probably throw an error.
|
|
93
|
-
|
|
94
|
-
**How does it compare performance-wise to other libraries?**
|
|
95
|
-
|
|
96
|
-
In my testing, parsing a Vector 2 runs at 2.2m ops/s with as-json and around 10,000 ops/s with assemblyscript-json and @serial-as/json.
|
|
97
|
-
Both are great libraries however.
|
|
98
80
|
## Performance
|
|
99
81
|
|
|
100
82
|
**Serialize Object (Vec2):** ~7.20m ops/s
|
package/asconfig.json
CHANGED
|
@@ -9,9 +9,9 @@ function canSerde<T>(data: T): void {
|
|
|
9
9
|
// @ts-ignore
|
|
10
10
|
@json
|
|
11
11
|
class Vec3 {
|
|
12
|
-
x:
|
|
13
|
-
y:
|
|
14
|
-
z:
|
|
12
|
+
x: f64;
|
|
13
|
+
y: f64;
|
|
14
|
+
z: f64;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
// @ts-ignore
|
|
@@ -136,6 +136,7 @@ describe("Ser/de Array", () => {
|
|
|
136
136
|
});
|
|
137
137
|
|
|
138
138
|
it("should ser/de string arrays", () => {
|
|
139
|
+
// ["abcdefg","st\\"ring\\" w\\"\\"ith quotes\\"","string \\t\\r\\"with ran\\tdom spa\\nces and \\nnewlines\\n\\n\\n","string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\""]
|
|
139
140
|
canSerde<string[]>([
|
|
140
141
|
"abcdefg",
|
|
141
142
|
'st"ring" w""ith quotes"',
|
package/assembly/src/json.ts
CHANGED
|
@@ -241,7 +241,7 @@ export function parseNumber<T>(data: string): T {
|
|
|
241
241
|
// @ts-ignore
|
|
242
242
|
const type: T = 0;
|
|
243
243
|
// @ts-ignore
|
|
244
|
-
if (type instanceof f64) return
|
|
244
|
+
if (type instanceof f64) return f64.parse(data);
|
|
245
245
|
// @ts-ignore
|
|
246
246
|
else if (type instanceof f32) return f32.parse(data);
|
|
247
247
|
// @ts-ignore
|
|
@@ -268,7 +268,7 @@ function parseObject<T>(data: string): T {
|
|
|
268
268
|
let schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
|
|
269
269
|
let key = "";
|
|
270
270
|
let isKey = false;
|
|
271
|
-
let depth =
|
|
271
|
+
let depth = 0;
|
|
272
272
|
let char = 0;
|
|
273
273
|
let outerLoopIndex = 1;
|
|
274
274
|
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
@@ -281,10 +281,10 @@ function parseObject<T>(data: string): T {
|
|
|
281
281
|
) {
|
|
282
282
|
char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
283
283
|
if (char === leftBracketCode) {
|
|
284
|
-
depth
|
|
284
|
+
depth++;
|
|
285
285
|
} else if (char === rightBracketCode) {
|
|
286
|
-
depth
|
|
287
|
-
if (depth ===
|
|
286
|
+
depth--;
|
|
287
|
+
if (depth === 0) {
|
|
288
288
|
++arrayValueIndex;
|
|
289
289
|
// @ts-ignore
|
|
290
290
|
schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, arrayValueIndex));
|
|
@@ -302,10 +302,10 @@ function parseObject<T>(data: string): T {
|
|
|
302
302
|
) {
|
|
303
303
|
char = unsafeCharCodeAt(data, objectValueIndex);
|
|
304
304
|
if (char === leftBraceCode) {
|
|
305
|
-
depth
|
|
305
|
+
depth++;
|
|
306
306
|
} else if (char === rightBraceCode) {
|
|
307
|
-
depth
|
|
308
|
-
if (depth ===
|
|
307
|
+
depth--;
|
|
308
|
+
if (depth === 0) {
|
|
309
309
|
++objectValueIndex;
|
|
310
310
|
// @ts-ignore
|
|
311
311
|
schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, objectValueIndex));
|
package/assembly/test.ts
CHANGED
|
@@ -3,31 +3,15 @@ import {
|
|
|
3
3
|
JSON
|
|
4
4
|
} from ".";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
this.high = high;
|
|
16
|
-
this.low = low;
|
|
17
|
-
this.open = open;
|
|
18
|
-
this.close = close;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
//console.log(JSON.stringify(candle));
|
|
23
|
-
|
|
24
|
-
const parsedCandleArray: Candle[] = JSON.parse<Candle[]>(`[{"timestamp":1620248400000,"high":573.3476371851107,"low":572.7653897862947,"close":572.7653897862947,"open":573.3476371851107},{"timestamp":1620249300000,"high":572.4966600947654,"low":572.3690847959957,"close":572.3690847959957,"open":572.4966600947654},{"timestamp":1620250200000,"high":572.2886600465616,"low":571.757999416375,"close":571.757999416375,"open":572.2886600465616},{"timestamp":1620251100000,"high":571.745549588537,"low":571.4831524457645,"close":571.4831524457645,"open":571.745549588537},{"timestamp":1620252000000,"high":571.4291070205617,"low":571.423771655803,"close":571.423771655803,"open":571.4291070205617},{"timestamp":1620252900000,"high":571.2682040427187,"low":570.0124454515138,"close":571.2543224050862,"open":571.2682040427187},{"timestamp":1620253800000,"high":571.950687856295,"low":571.950687856295,"close":571.950687856295,"open":571.950687856295},{"timestamp":1620254700000,"high":574.32247048919,"low":572.229619806987,"close":574.32247048919,"open":572.229619806987},{"timestamp":1620255600000,"high":573.7949788294341,"low":573.7949788294341,"close":573.7949788294341,"open":573.7949788294341},{"timestamp":1620256500000,"high":573.6703031128156,"low":573.6703031128156,"close":573.6703031128156,"open":573.6703031128156},{"timestamp":1620261000000,"high":573.4922654231464,"low":573.4922654231464,"close":573.4922654231464,"open":573.4922654231464},{"timestamp":1620261900000,"high":573.2630790607755,"low":572.1510958054668,"close":572.1510958054668,"open":573.2630790607755},{"timestamp":1620262800000,"high":570.381489136757,"low":570.381489136757,"close":570.381489136757,"open":570.381489136757},{"timestamp":1620264600000,"high":570.2742985701099,"low":570.0803689046074,"close":570.0803689046074,"open":570.2742985701099},{"timestamp":1620265500000,"high":570.1422401263446,"low":570.1422401263446,"close":570.1422401263446,"open":570.1422401263446},{"timestamp":1620266400000,"high":572.8052512272116,"low":572.8052201233321,"close":572.8052512272116,"open":572.8052201233321},{"timestamp":1620267300000,"high":572.7730922927839,"low":572.7447581107978,"close":572.7447581107978,"open":572.7730922927839},{"timestamp":1620268200000,"high":572.7272428903487,"low":572.7258230151764,"close":572.7258230151764,"open":572.7272428903487},{"timestamp":1620270900000,"high":572.2594215468664,"low":571.8195591664584,"close":571.8195591664584,"open":572.2578671930029},{"timestamp":1620271800000,"high":571.2809499888094,"low":571.2808568072832,"close":571.2808568072832,"open":571.2809499888094},{"timestamp":1620272700000,"high":571.1742152842157,"low":571.1742152842157,"close":571.1742152842157,"open":571.1742152842157},{"timestamp":1620273600000,"high":571.1716102454611,"low":571.0288842515337,"close":571.0288842515337,"open":571.0642228722581},{"timestamp":1620275400000,"high":571.0203010949516,"low":571.0203010949516,"close":571.0203010949516,"open":571.0203010949516},{"timestamp":1620277200000,"high":570.261632018576,"low":570.261632018576,"close":570.261632018576,"open":570.261632018576},{"timestamp":1620279000000,"high":570.1348876529629,"low":570.1348876529629,"close":570.1348876529629,"open":570.1348876529629},{"timestamp":1620279900000,"high":570.199730991694,"low":570.199730991694,"close":570.199730991694,"open":570.199730991694},{"timestamp":1620282600000,"high":568.4408310147167,"low":568.4408310147167,"close":568.4408310147167,"open":568.4408310147167},{"timestamp":1620284400000,"high":567.8217236373331,"low":567.5896453383704,"close":567.5896453383704,"open":567.8217236373331},{"timestamp":1620288000000,"high":568.652510360092,"low":568.652510360092,"close":568.652510360092,"open":568.652510360092},{"timestamp":1620288900000,"high":569.0169039594798,"low":569.0169039594798,"close":569.0169039594798,"open":569.0169039594798},{"timestamp":1620289800000,"high":569.1372681718557,"low":569.1372681718557,"close":569.1372681718557,"open":569.1372681718557},{"timestamp":1620293400000,"high":569.1425890413165,"low":569.1425890413165,"close":569.1425890413165,"open":569.1425890413165},{"timestamp":1620294300000,"high":571.4603226652566,"low":569.668539984208,"close":571.3510882379371,"open":569.668539984208},{"timestamp":1620295200000,"high":575.0297756308146,"low":565.6188600247405,"close":575.0297756308146,"open":571.2768914562338},{"timestamp":1620296100000,"high":579.5414875855951,"low":578.0875180733055,"close":579.5414875855951,"open":578.0875180733055},{"timestamp":1620297000000,"high":580.4591998073045,"low":580.4591998073045,"close":580.4591998073045,"open":580.4591998073045},{"timestamp":1620299700000,"high":580.462401844569,"low":580.462401844569,"close":580.462401844569,"open":580.462401844569},{"timestamp":1620303300000,"high":580.450191330308,"low":580.450191330308,"close":580.450191330308,"open":580.450191330308},{"timestamp":1620305100000,"high":580.4060918534583,"low":580.4060918534583,"close":580.4060918534583,"open":580.4060918534583},{"timestamp":1620306000000,"high":580.2988479516713,"low":580.2988479516713,"close":580.2988479516713,"open":580.2988479516713},{"timestamp":1620306900000,"high":578.5227831917862,"low":578.5121109550881,"close":578.5121109550881,"open":578.5227831917862},{"timestamp":1620307800000,"high":578.3521310367136,"low":578.224208690017,"close":578.224208690017,"open":578.3521310367136},{"timestamp":1620308700000,"high":578.1781390176766,"low":578.1781390176766,"close":578.1781390176766,"open":578.1781390176766},{"timestamp":1620309600000,"high":574.5402154361069,"low":574.2966785397088,"close":574.5402154361069,"open":574.2966785397088},{"timestamp":1620310500000,"high":574.6374782052543,"low":574.3595582585991,"close":574.6374782052543,"open":574.3595582585991},{"timestamp":1620311400000,"high":575.0993626799352,"low":575.0993626799352,"close":575.0993626799352,"open":575.0993626799352},{"timestamp":1620313200000,"high":573.7701312728145,"low":573.7701312728145,"close":573.7701312728145,"open":573.7701312728145},{"timestamp":1620314100000,"high":573.3906631152785,"low":573.3906631152785,"close":573.3906631152785,"open":573.3906631152785},{"timestamp":1620315000000,"high":573.4334610811361,"low":573.4334610811361,"close":573.4334610811361,"open":573.4334610811361},{"timestamp":1620315900000,"high":571.6595888644223,"low":571.6595888644223,"close":571.6595888644223,"open":571.6595888644223},{"timestamp":1620316800000,"high":571.7534533028926,"low":571.7534533028926,"close":571.7534533028926,"open":571.7534533028926},{"timestamp":1620317700000,"high":577.9623648979384,"low":574.8537177533384,"close":576.1674529591473,"open":574.8537177533384},{"timestamp":1620318600000,"high":576.1563193117207,"low":576.0620257439493,"close":576.0620257439493,"open":576.1563193117207},{"timestamp":1620319500000,"high":574.275952158709,"low":571.177013121888,"close":571.177013121888,"open":574.275952158709},{"timestamp":1620321300000,"high":571.2079586608608,"low":571.2079586608608,"close":571.2079586608608,"open":571.2079586608608},{"timestamp":1620323100000,"high":571.324057673865,"low":571.324057673865,"close":571.324057673865,"open":571.324057673865},{"timestamp":1620324000000,"high":571.1253547573914,"low":570.0875973567182,"close":570.0875973567182,"open":571.1253547573914},{"timestamp":1620324900000,"high":570.0805475832676,"low":570.0805475832676,"close":570.0805475832676,"open":570.0805475832676},{"timestamp":1620325800000,"high":570.0257972684403,"low":559.1035539331212,"close":559.1035539331212,"open":570.0257972684403},{"timestamp":1620328500000,"high":557.5649037694549,"low":556.0279702412793,"close":556.0279702412793,"open":557.5649037694549},{"timestamp":1620329400000,"high":556.6968218685071,"low":556.6968218685071,"close":556.6968218685071,"open":556.6968218685071},{"timestamp":1620330300000,"high":560.7228138291837,"low":558.2216468935494,"close":560.7228138291837,"open":558.2216468935494},{"timestamp":1620332100000,"high":560.5876666979001,"low":560.5876666979001,"close":560.5876666979001,"open":560.5876666979001},{"timestamp":1620333000000,"high":560.5186489036859,"low":559.9302964393763,"close":559.9302964393763,"open":560.5186489036859},{"timestamp":1620335700000,"high":560.8859962694225,"low":560.8859962694225,"close":560.8859962694225,"open":560.8859962694225},{"timestamp":1620337500000,"high":562.6549059693484,"low":561.1228262312293,"close":562.6549059693484,"open":561.1228262312293},{"timestamp":1620338400000,"high":562.5749239504988,"low":562.5749239504988,"close":562.5749239504988,"open":562.5749239504988},{"timestamp":1620340200000,"high":564.1086094674863,"low":564.1086094674863,"close":564.1086094674863,"open":564.1086094674863},{"timestamp":1620341100000,"high":564.1156752788603,"low":564.1156752788603,"close":564.1156752788603,"open":564.1156752788603},{"timestamp":1620342000000,"high":563.8686326269438,"low":563.8686326269438,"close":563.8686326269438,"open":563.8686326269438},{"timestamp":1620343800000,"high":565.4032072056408,"low":565.4032072056408,"close":565.4032072056408,"open":565.4032072056408},{"timestamp":1620344700000,"high":564.9161906724804,"low":564.846451581273,"close":564.846451581273,"open":564.9161906724804},{"timestamp":1620345600000,"high":565.3129449522828,"low":563.3005556458523,"close":565.3129449522828,"open":563.3005556458523},{"timestamp":1620346500000,"high":566.8491299730733,"low":566.7792504378252,"close":566.7792504378252,"open":566.8491299730733},{"timestamp":1620347400000,"high":567.6311377719769,"low":566.8226586070456,"close":567.6311377719769,"open":566.8226586070456},{"timestamp":1620349200000,"high":568.393145669603,"low":562.3084190409248,"close":562.3084190409248,"open":568.393145669603},{"timestamp":1620350100000,"high":563.0673266288399,"low":563.0673266288399,"close":563.0673266288399,"open":563.0673266288399},{"timestamp":1620351000000,"high":563.0460469915702,"low":563.0460469915702,"close":563.0460469915702,"open":563.0460469915702},{"timestamp":1620351900000,"high":562.6940165863563,"low":562.6940165863563,"close":562.6940165863563,"open":562.6940165863563},{"timestamp":1620352800000,"high":562.2290121119479,"low":562.2290121119479,"close":562.2290121119479,"open":562.2290121119479},{"timestamp":1620354600000,"high":560.6865799646575,"low":559.1464640169283,"close":559.1464640169283,"open":560.6865799646575},{"timestamp":1620356400000,"high":558.6737643409452,"low":558.5032601234623,"close":558.5032601234623,"open":558.6737643409452},{"timestamp":1620358200000,"high":557.7252518300813,"low":557.7252518300813,"close":557.7252518300813,"open":557.7252518300813},{"timestamp":1620359100000,"high":559.452591197762,"low":559.452591197762,"close":559.452591197762,"open":559.452591197762},{"timestamp":1620360000000,"high":557.9238732313372,"low":551.1413593852167,"close":554.1625302396624,"open":557.9238732313372},{"timestamp":1620360900000,"high":555.7240520136215,"low":555.674531175535,"close":555.7240520136215,"open":555.674531175535},{"timestamp":1620361800000,"high":554.0781517227211,"low":554.0781517227211,"close":554.0781517227211,"open":554.0781517227211},{"timestamp":1620362700000,"high":555.9093975255644,"low":554.0935763098537,"close":555.9093975255644,"open":554.0935763098537},{"timestamp":1620363600000,"high":556.7596063111193,"low":556.0023730597429,"close":556.7596063111193,"open":556.0023730597429},{"timestamp":1620365400000,"high":558.3270073972132,"low":556.8114287909416,"close":558.3270073972132,"open":556.8114287909416},{"timestamp":1620367200000,"high":558.7824867555943,"low":558.7824867555943,"close":558.7824867555943,"open":558.7824867555943},{"timestamp":1620369000000,"high":562.0179021454524,"low":560.3025781550792,"close":562.0179021454524,"open":560.3025781550792},{"timestamp":1620369900000,"high":562.0080817771758,"low":562.0080817771758,"close":562.0080817771758,"open":562.0080817771758},{"timestamp":1620373500000,"high":560.4783187009747,"low":560.4783187009747,"close":560.4783187009747,"open":560.4783187009747},{"timestamp":1620374400000,"high":558.9505059072079,"low":558.9505059072079,"close":558.9505059072079,"open":558.9505059072079},{"timestamp":1620375300000,"high":558.7316785819487,"low":558.7315744378288,"close":558.7315744378288,"open":558.7316785819487},{"timestamp":1620376200000,"high":559.2700965276954,"low":558.7201382932576,"close":558.7201382932576,"open":559.2700965276954},{"timestamp":1620377100000,"high":559.7884860040957,"low":558.2725955125745,"close":559.7884860040957,"open":558.3870242987017},{"timestamp":1620378000000,"high":561.2454700974902,"low":559.7277145674427,"close":561.0931142383333,"open":559.7780560002528},{"timestamp":1620379800000,"high":562.5589149792357,"low":561.0392143359311,"close":562.5589149792357,"open":561.0392143359311},{"timestamp":1620381600000,"high":564.825992475405,"low":564.0806119374416,"close":564.825992475405,"open":564.0806119374416},{"timestamp":1620382500000,"high":564.8106789982139,"low":564.8003967441988,"close":564.8003967441988,"open":564.8106789982139},{"timestamp":1620383400000,"high":565.0122815398387,"low":565.0122815398387,"close":565.0122815398387,"open":565.0122815398387},{"timestamp":1620384300000,"high":565.0122384439766,"low":565.0122384439766,"close":565.0122384439766,"open":565.0122384439766},{"timestamp":1620385200000,"high":564.9947583554654,"low":564.9947583554654,"close":564.9947583554654,"open":564.9947583554654},{"timestamp":1620386100000,"high":564.7315180958523,"low":564.726251367374,"close":564.726251367374,"open":564.7315180958523},{"timestamp":1620387000000,"high":566.1025337740446,"low":564.5781421341958,"close":566.1025337740446,"open":564.5781421341958}]`);
|
|
25
|
-
|
|
26
|
-
for (let i = 0; i < parsedCandleArray.length; i++) {
|
|
27
|
-
const chunk = parsedCandleArray[i];
|
|
28
|
-
console.log(JSON.stringify(chunk));
|
|
29
|
-
}
|
|
30
|
-
/*
|
|
6
|
+
const exp = `["abcdefg","st\\"ring\\" w\\"\\"ith quotes\\"","string \\t\\r\\"with ran\\tdom spa\\nces and \\nnewlines\\n\\n\\n","string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\""]`;
|
|
7
|
+
|
|
8
|
+
console.log(exp);
|
|
9
|
+
console.log(JSON.stringify([
|
|
10
|
+
"abcdefg",
|
|
11
|
+
'st"ring" w""ith quotes"',
|
|
12
|
+
'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
|
|
13
|
+
'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
|
|
14
|
+
]))
|
|
31
15
|
// @ts-ignore
|
|
32
16
|
@JSON
|
|
33
17
|
class Vec3 {
|
|
@@ -73,4 +57,4 @@ const player: Player = {
|
|
|
73
57
|
const serializedPlayer = JSON.stringify<Player>(player);
|
|
74
58
|
console.log("Serialized Player: " + serializedPlayer);
|
|
75
59
|
const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
|
|
76
|
-
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer))
|
|
60
|
+
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.20",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
"@as-tral/cli": "^2.0.0",
|
|
26
26
|
"@assemblyscript/loader": "^0.27.0",
|
|
27
27
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
28
|
-
"as-bignum": "^0.2.23",
|
|
29
28
|
"assemblyscript": "^0.27.0",
|
|
30
29
|
"assemblyscript-prettier": "^1.0.7",
|
|
31
30
|
"prettier": "^2.8.3",
|
|
@@ -34,7 +33,8 @@
|
|
|
34
33
|
},
|
|
35
34
|
"dependencies": {
|
|
36
35
|
"as-string-sink": "^0.5.0",
|
|
37
|
-
"as-variant": "^0.4.1"
|
|
36
|
+
"as-variant": "^0.4.1",
|
|
37
|
+
"as-bignum": "^0.2.23"
|
|
38
38
|
},
|
|
39
39
|
"repository": {
|
|
40
40
|
"type": "git",
|
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
|
}
|