json-as 0.2.5 → 0.4.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/LICENSE +1 -1
- package/README.md +52 -98
- package/asconfig.json +10 -35
- package/assembly/__benches__/as-tral.d.ts +1 -0
- package/assembly/__benches__/benchmark.ts +69 -0
- package/assembly/chars.ts +16 -0
- package/assembly/index.ts +337 -781
- package/assembly/test.ts +35 -207
- package/assembly/tsconfig.json +9 -0
- package/index.ts +1 -0
- package/package.json +26 -37
- package/tests/index.js +3 -3
- package/tests/test.js +13 -6
- package/transform/lib/index.js +47 -150
- package/transform/package.json +30 -2
- package/transform/src/index.old.js +430 -0
- package/transform/src/index.ts +98 -0
- package/transform/tsconfig.json +68 -69
- package/assembly/DynamicObject.ts +0 -29
- package/assembly/bench.ts +0 -199
- package/assembly/unknown.ts +0 -175
- package/bench/bench.js +0 -102
- package/transform/index.ts +0 -224
package/assembly/test.ts
CHANGED
|
@@ -1,207 +1,35 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
f645: f64
|
|
37
|
-
f646: f64
|
|
38
|
-
f321: f32
|
|
39
|
-
f322: f32
|
|
40
|
-
f323: f32
|
|
41
|
-
f324: f32
|
|
42
|
-
f325: f32
|
|
43
|
-
f326: f32
|
|
44
|
-
u8: u8
|
|
45
|
-
u16: u16
|
|
46
|
-
u32: u32
|
|
47
|
-
u64: u64
|
|
48
|
-
i8: i8
|
|
49
|
-
i16: i16
|
|
50
|
-
i32: i32
|
|
51
|
-
i64: i64
|
|
52
|
-
bool: bool
|
|
53
|
-
boolean: boolean
|
|
54
|
-
stringArr: string[]
|
|
55
|
-
f64Arr: f64[]
|
|
56
|
-
u8Arr: u8[]
|
|
57
|
-
u16Arr: u16[]
|
|
58
|
-
u32Arr: u32[]
|
|
59
|
-
u64Arr: u64[]
|
|
60
|
-
i8Arr: i8[]
|
|
61
|
-
i16Arr: i16[]
|
|
62
|
-
i32Arr: i32[]
|
|
63
|
-
i64Arr: i64[]
|
|
64
|
-
emptyArr: string[]
|
|
65
|
-
boolArr: bool[]
|
|
66
|
-
booleanArr: boolean[]
|
|
67
|
-
stringArrArr: string[][]
|
|
68
|
-
object: JSONSchema2
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// @ts-ignore
|
|
72
|
-
@json
|
|
73
|
-
class JSONSchema2 {
|
|
74
|
-
string1: string
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const obj: JSONSchema = {
|
|
78
|
-
string1: 'Hello World',
|
|
79
|
-
string2: 'Hell[}o Wo[rld}{',
|
|
80
|
-
string3: 'Hel`"lo Wo"`r"ld',
|
|
81
|
-
f641: 7.23,
|
|
82
|
-
f642: 10e2,
|
|
83
|
-
f643: 10E2,
|
|
84
|
-
f644: 123456e-5,
|
|
85
|
-
f645: 123456E-5,
|
|
86
|
-
f646: 0.0,
|
|
87
|
-
f321: 7.23,
|
|
88
|
-
f322: 10e2,
|
|
89
|
-
f323: 10E2,
|
|
90
|
-
f324: 123456e-5,
|
|
91
|
-
f325: 123456E-5,
|
|
92
|
-
f326: 0.0,
|
|
93
|
-
u8: 100,
|
|
94
|
-
u16: 101,
|
|
95
|
-
u32: 102,
|
|
96
|
-
u64: 103,
|
|
97
|
-
i8: -100,
|
|
98
|
-
i16: -101,
|
|
99
|
-
i32: -102,
|
|
100
|
-
i64: -103,
|
|
101
|
-
bool: true,
|
|
102
|
-
boolean: false,
|
|
103
|
-
stringArr: ['Hello World', 'Hell[}o Wo[rld}{', 'Hel`"lo Wo"`r"ld'],
|
|
104
|
-
f64Arr: [7.23, 10e2, 10e2, 10E2, 123456e-5, 123456E-5, 0.0],
|
|
105
|
-
u8Arr: [100, 100, 100],
|
|
106
|
-
u16Arr: [101, 101, 101],
|
|
107
|
-
u32Arr: [102, 102, 102],
|
|
108
|
-
u64Arr: [103, 103, 103],
|
|
109
|
-
i8Arr: [-100, -100, -100],
|
|
110
|
-
i16Arr: [-101, -101, -101],
|
|
111
|
-
i32Arr: [-102, -102, -102],
|
|
112
|
-
i64Arr: [-103, -103, -103],
|
|
113
|
-
emptyArr: [],
|
|
114
|
-
boolArr: [true, false, true],
|
|
115
|
-
booleanArr: [true, false, true],
|
|
116
|
-
stringArrArr: [['Hey'], ['ha'], ['ho']],
|
|
117
|
-
object: {
|
|
118
|
-
string1: 'Hello World'
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const emptyObj: EmptySchema = {}
|
|
123
|
-
// Strings
|
|
124
|
-
check<string>('Encode/Decode String', 'Hello World')
|
|
125
|
-
check<string>('Encode/Decode String', 'Hell[}o Wo[rld}{')
|
|
126
|
-
check<string>('Encode/Decode String', 'Hel`"lo Wo"`r"ld')
|
|
127
|
-
// Floats
|
|
128
|
-
check<f64>('Encode/Decode f64', 7.23)
|
|
129
|
-
check<f64>('Encode/Decode f64', 10e2)
|
|
130
|
-
check<f64>('Encode/Decode f64', 10E2)
|
|
131
|
-
check<f64>('Encode/Decode f64', 123456e-5)
|
|
132
|
-
check<f64>('Encode/Decode f64', 123456E-5)
|
|
133
|
-
check<f64>('Encode/Decode f64', 0.0)
|
|
134
|
-
|
|
135
|
-
check<f32>('Encode/Decode f32', 7.23)
|
|
136
|
-
check<f32>('Encode/Decode f32', 10e2)
|
|
137
|
-
check<f32>('Encode/Decode f32', 10E2)
|
|
138
|
-
check<f32>('Encode/Decode f32', 123456e-5)
|
|
139
|
-
check<f32>('Encode/Decode f32', 123456E-5)
|
|
140
|
-
check<f32>('Encode/Decode f32', 0.0)
|
|
141
|
-
// Integers
|
|
142
|
-
check<u8>('Encode/Decode u8', 100)
|
|
143
|
-
check<u16>('Encode/Decode u16', 101)
|
|
144
|
-
check<u32>('Encode/Decode u32', 102)
|
|
145
|
-
check<u64>('Encode/Decode u64', 103)
|
|
146
|
-
|
|
147
|
-
check<i8>('Encode/Decode i8', -100)
|
|
148
|
-
check<i16>('Encode/Decode i16', -101)
|
|
149
|
-
check<i32>('Encode/Decode i32', -102)
|
|
150
|
-
check<i64>('Encode/Decode i64', -103)
|
|
151
|
-
|
|
152
|
-
// Bools
|
|
153
|
-
check<bool>('Encode/Decode bool', true)
|
|
154
|
-
check<bool>('Encode/Decode bool', false)
|
|
155
|
-
|
|
156
|
-
// Booleans
|
|
157
|
-
check<boolean>('Encode/Decode boolean', true)
|
|
158
|
-
check<boolean>('Encode/Decode boolean', false)
|
|
159
|
-
|
|
160
|
-
// Null
|
|
161
|
-
check('Encode/Decode null', null)
|
|
162
|
-
|
|
163
|
-
// Arrays
|
|
164
|
-
check<string[]>('Encode/Decode string[]', ['Hello World', 'Hell[}o Wo[rld}{', 'Hel`"lo Wo"`r"ld'])
|
|
165
|
-
check<f64[]>('Encode/Decode f32[]', [7.23, 10e2, 10e2, 10E2, 123456e-5, 123456E-5, 0.0])
|
|
166
|
-
check<u8[]>('Encode/Decode u8[]', [100, 100, 100])
|
|
167
|
-
check<u16[]>('Encode/Decode u16[]', [101, 101, 101])
|
|
168
|
-
check<u32[]>('Encode/Decode u32[]', [102, 102, 102])
|
|
169
|
-
check<u64[]>('Encode/Decode u64[]', [103, 103, 103])
|
|
170
|
-
check<i8[]>('Encode/Decode i8[]', [-100, -100, -100])
|
|
171
|
-
check<i16[]>('Encode/Decode i16[]', [-101, -101, -101])
|
|
172
|
-
check<i32[]>('Encode/Decode i32[]', [-102, -102, -102])
|
|
173
|
-
check<i64[]>('Encode/Decode i64[]', [-103, -103, -103])
|
|
174
|
-
check<string[]>('Encode/Decode empty[]', [])
|
|
175
|
-
check<bool[]>('Encode/Decode bool[]', [true, false, true])
|
|
176
|
-
check<boolean[]>('Encode/Decode boolean[]', [true, false, true])
|
|
177
|
-
check<string[][]>('Encode/Decode string[][]', [['Hey'], ['ha'], ['ho']])
|
|
178
|
-
|
|
179
|
-
// Object
|
|
180
|
-
check<JSONSchema>('Encode/Decode object', obj)
|
|
181
|
-
|
|
182
|
-
check<EmptySchema>('Encode/Decode object', emptyObj)
|
|
183
|
-
*/
|
|
184
|
-
// Unknown
|
|
185
|
-
//check<unknown[]>('Encode/Decode unknown[]', ["Welcome to dynamic arrays", ["Very", ["Deep", ["Array"]]], "It also supports nulls"])
|
|
186
|
-
|
|
187
|
-
//console.log(JSON.stringify(["Welcome to dynamic arrays", 3.14, ["Very", ["Deep", ["Array"]]], true, "It also supports nulls", null]))
|
|
188
|
-
|
|
189
|
-
//console.log(JSON.stringify(JSON.parse<unknown[]>('["Welcome to dynamic arrays",3.14,["Very",["Deep",["Array"]]],true,"It also supports nulls",null]')))
|
|
190
|
-
//const foo = new Map()
|
|
191
|
-
|
|
192
|
-
//console.log(JSON.stringify(parseDynamicObject('{"hello":"world"}')))
|
|
193
|
-
// @ts-ignore
|
|
194
|
-
@json
|
|
195
|
-
class Test {
|
|
196
|
-
stuff: string
|
|
197
|
-
things: i32
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const test: Test = {
|
|
201
|
-
stuff: "hi",
|
|
202
|
-
things: 42
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
console.log(stringify(JSON.stringify(test)))
|
|
206
|
-
console.log(removeJSONWhitespace('{"stuff":"hi", "things":42}'))
|
|
207
|
-
console.log(stringify(JSON.stringify(JSON.parse<Test>('{"stuff":"hi", "things":42}'))))
|
|
1
|
+
import "wasi";
|
|
2
|
+
import { JSON } from ".";
|
|
3
|
+
|
|
4
|
+
@json
|
|
5
|
+
class Vec2 {
|
|
6
|
+
x: f32
|
|
7
|
+
y: f32
|
|
8
|
+
}
|
|
9
|
+
@json
|
|
10
|
+
class Player {
|
|
11
|
+
firstName: string
|
|
12
|
+
lastName: string
|
|
13
|
+
lastActive: i32[]
|
|
14
|
+
age: i32
|
|
15
|
+
pos: Vec2
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const data: Player = {
|
|
19
|
+
firstName: "Emmet",
|
|
20
|
+
lastName: "West",
|
|
21
|
+
lastActive: [8, 27, 2022],
|
|
22
|
+
age: 23,
|
|
23
|
+
pos: {
|
|
24
|
+
x: -3.4,
|
|
25
|
+
y: 1.2
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const stringified = JSON.stringify<Player>(data);
|
|
30
|
+
// '{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23}'
|
|
31
|
+
console.log(`Stringified: ${stringified}`);
|
|
32
|
+
|
|
33
|
+
const parsed = JSON.parse<Player>(stringified)
|
|
34
|
+
// { firstName: "Emmet", lastName: "West", "lastActive": [8,27,2022], age: 23 }
|
|
35
|
+
console.log(`Parsed: ${JSON.stringify(parsed)}`)
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { JSON } from "./assembly/index";
|
package/package.json
CHANGED
|
@@ -1,60 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
6
|
+
"author": "Jairus Tanaka",
|
|
7
|
+
"contributors": [
|
|
8
|
+
"DogWhich"
|
|
9
|
+
],
|
|
8
10
|
"license": "MIT",
|
|
9
11
|
"scripts": {
|
|
10
|
-
"
|
|
11
|
-
"build:test": "asc assembly/test.ts --
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"bench:lunatic": "lunatic ./bench/output/bench.wasm",
|
|
17
|
-
"asbuild:untouched": "asc assembly/index.ts --target debug",
|
|
18
|
-
"asbuild:optimized": "asc assembly/index.ts --target release",
|
|
19
|
-
"asbuild": "yarn asbuild:untouched && yarn asbuild:optimized"
|
|
12
|
+
"bench:astral": "astral",
|
|
13
|
+
"build:test": "asc assembly/test.ts --target test",
|
|
14
|
+
"build:transform": "tsc -p ./transform",
|
|
15
|
+
"test:wasmtime": "wasmtime ./build/test.wasm",
|
|
16
|
+
"test:lunatic": "lunatic ./build/test.wasm",
|
|
17
|
+
"test:wasm3": "wasm3 ./build/test.wasm"
|
|
20
18
|
},
|
|
21
19
|
"devDependencies": {
|
|
22
|
-
"@as-
|
|
23
|
-
"@assemblyscript/loader": "^0.19.10",
|
|
24
|
-
"@serial-as/json": "^1.0.2",
|
|
25
|
-
"@types/jest": "^27.0.2",
|
|
26
|
-
"@types/line-column": "^1.0.0",
|
|
27
|
-
"as-base64": "^0.2.0",
|
|
28
|
-
"as-bignum": "^0.2.18",
|
|
20
|
+
"@as-tral/cli": "^1.1.1",
|
|
29
21
|
"as-console": "^6.0.2",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"jest": "^27.3.1",
|
|
36
|
-
"kati": "^0.6.2",
|
|
37
|
-
"lerna": "^4.0.0",
|
|
38
|
-
"rimraf": "^3.0.2",
|
|
39
|
-
"ts-jest": "^27.0.7",
|
|
40
|
-
"ts-node": "^10.4.0",
|
|
41
|
-
"typescript": "^4.4.4"
|
|
22
|
+
"assemblyscript": "^0.20.7",
|
|
23
|
+
"assemblyscript-prettier": "^1.0.2",
|
|
24
|
+
"benchmark": "^2.1.4",
|
|
25
|
+
"microtime": "^3.0.0",
|
|
26
|
+
"typescript": "^4.7.2"
|
|
42
27
|
},
|
|
43
28
|
"dependencies": {
|
|
44
|
-
"as-string-sink": "^0.
|
|
29
|
+
"as-string-sink": "^0.5.0",
|
|
30
|
+
"as-variant": "^0.3.0",
|
|
31
|
+
"visitor-as": "^0.10.0"
|
|
45
32
|
},
|
|
46
33
|
"repository": {
|
|
47
34
|
"type": "git",
|
|
48
|
-
"url": "git+https://github.com/
|
|
35
|
+
"url": "git+https://github.com/JairusSW/as-json.git"
|
|
49
36
|
},
|
|
50
37
|
"keywords": [
|
|
51
38
|
"assemblyscript",
|
|
52
39
|
"json",
|
|
53
40
|
"serialize",
|
|
54
|
-
"deserialize"
|
|
41
|
+
"deserialize",
|
|
42
|
+
"dynamic"
|
|
55
43
|
],
|
|
56
44
|
"bugs": {
|
|
57
|
-
"url": "https://github.com/
|
|
45
|
+
"url": "https://github.com/JairusSW/as-json/issues"
|
|
58
46
|
},
|
|
59
|
-
"homepage": "https://github.com/
|
|
47
|
+
"homepage": "https://github.com/JairusSW/as-json#readme",
|
|
48
|
+
"type": "module"
|
|
60
49
|
}
|
package/tests/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
assert.strictEqual(
|
|
1
|
+
import assert from "assert";
|
|
2
|
+
import { add } from "../build/debug.js";
|
|
3
|
+
assert.strictEqual(add(1, 2), 3);
|
|
4
4
|
console.log("ok");
|
package/tests/test.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
const loader = require("@assemblyscript/loader");
|
|
3
|
-
const { WASI } = require(
|
|
4
|
-
const
|
|
5
|
-
const
|
|
3
|
+
const { WASI } = require("wasi");
|
|
4
|
+
const ConsoleImport = require('as-console/imports')
|
|
5
|
+
const Console = new ConsoleImport()
|
|
6
|
+
const wasiOptions = {};
|
|
7
|
+
const wasi = new WASI(wasiOptions);
|
|
6
8
|
const imports = {
|
|
7
|
-
wasi_snapshot_preview1: wasi.wasiImport
|
|
9
|
+
wasi_snapshot_preview1: wasi.wasiImport,
|
|
10
|
+
...Console.wasmImports
|
|
8
11
|
};
|
|
9
|
-
const wasmModule = loader.instantiateSync(
|
|
10
|
-
|
|
12
|
+
const wasmModule = loader.instantiateSync(
|
|
13
|
+
fs.readFileSync(__dirname + "/output/test.wasm"),
|
|
14
|
+
imports
|
|
15
|
+
);
|
|
16
|
+
Console.wasmExports = wasmModule.exports
|
|
17
|
+
wasi.start(wasmModule);
|
package/transform/lib/index.js
CHANGED
|
@@ -1,181 +1,78 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
return _type;
|
|
12
|
-
}
|
|
13
|
-
// Replace Next Line
|
|
14
|
-
const replaceNextLineRegex = /^\W*\/\/ <replace next line>.*$/gm;
|
|
15
|
-
class JSONTransformer extends visitor_as_1.BaseVisitor {
|
|
16
|
-
currentClass;
|
|
17
|
-
encodeStmts = new Map();
|
|
18
|
-
decodeCode = new Map();
|
|
19
|
-
lastType = '';
|
|
20
|
-
sources = [];
|
|
21
|
-
globalStatements = [];
|
|
22
|
-
replaceNextLines = new Set();
|
|
23
|
-
visitObjectLiteralExpression(node) {
|
|
24
|
-
const keys = node.names;
|
|
25
|
-
const values = node.values;
|
|
26
|
-
const replacer = visitor_as_1.SimpleParser.parseExpression(`new Object()`);
|
|
27
|
-
for (const key of keys) {
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
visitElementAccessExpression(node) {
|
|
31
|
-
super.visitElementAccessExpression(node);
|
|
32
|
-
if ((0, utils_1.toString)(node.expression) === 'o') {
|
|
33
|
-
// Should be like if (node.expression.type.text === "Object") {
|
|
34
|
-
const replacer = visitor_as_1.SimpleParser.parseExpression(`u32(changetype<usize>(${(0, utils_1.toString)(node.elementExpression)}))`);
|
|
35
|
-
node.elementExpression = replacer;
|
|
36
|
-
this.sources.push(replacer.range.source);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
visitArrayLiteralExpression(node) {
|
|
40
|
-
super.visitArrayLiteralExpression(node);
|
|
41
|
-
if (isanyArray(node)) {
|
|
42
|
-
for (let i = 0; i < node.elementExpressions.length; i++) {
|
|
43
|
-
const expr = node.elementExpressions[i];
|
|
44
|
-
// @ts-ignore
|
|
45
|
-
let replacement;
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
if (expr.elementExpressions) {
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
this.convertToAnyArray(expr.elementExpressions);
|
|
50
|
-
}
|
|
51
|
-
// @ts-ignore
|
|
52
|
-
replacement = visitor_as_1.SimpleParser.parseExpression(`unknown.wrap(${(0, utils_1.toString)(expr)})`);
|
|
53
|
-
node.elementExpressions[i] = replacement;
|
|
54
|
-
this.sources.push(replacement.range.source);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
convertToAnyArray(exprs) {
|
|
59
|
-
for (let i = 0; i < exprs.length; i++) {
|
|
60
|
-
const expr = exprs[i];
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
let replacement;
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
if (expr.elementExpressions) {
|
|
65
|
-
// @ts-ignore
|
|
66
|
-
this.convertToAnyArray(expr.elementExpressions);
|
|
67
|
-
}
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
replacement = visitor_as_1.SimpleParser.parseExpression(`unknown.wrap(${(0, utils_1.toString)(expr)})`);
|
|
70
|
-
exprs[i] = replacement;
|
|
71
|
-
this.sources.push(replacement.range.source);
|
|
72
|
-
}
|
|
1
|
+
import { ClassDecorator, registerDecorator } from "visitor-as/dist/decorator.js";
|
|
2
|
+
import { getName } from "visitor-as/dist/utils.js";
|
|
3
|
+
import { SimpleParser } from "visitor-as/dist/index.js";
|
|
4
|
+
class AsJSONTransform extends ClassDecorator {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.sources = [];
|
|
8
|
+
this.encodeStmts = new Map();
|
|
9
|
+
this.decodeCode = new Map();
|
|
73
10
|
}
|
|
11
|
+
visitMethodDeclaration(node) { }
|
|
74
12
|
visitFieldDeclaration(node) {
|
|
75
|
-
|
|
76
|
-
const name = (0, utils_1.toString)(node.name);
|
|
13
|
+
const name = getName(node);
|
|
77
14
|
if (!node.type) {
|
|
78
15
|
throw new Error(`Field ${name} is missing a type declaration`);
|
|
79
16
|
}
|
|
80
|
-
const type =
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
this.decodeCode.get(className).push(`${name}: JSON.parse<${type}>(unchecked(values.get('${name}'))),\n`);
|
|
91
|
-
}
|
|
17
|
+
const type = getName(node.type);
|
|
18
|
+
const className = this.currentClass.name.text;
|
|
19
|
+
if (!this.encodeStmts.has(className))
|
|
20
|
+
this.encodeStmts.set(className, []);
|
|
21
|
+
if (!this.decodeCode.has(className))
|
|
22
|
+
this.decodeCode.set(className, []);
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
this.encodeStmts.get(className).push(`this.__JSON_Serialized += '' + '"' + '${name}' + '"' + ':' + JSON.stringify<${type}>(this.${name}) + ',';`);
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
this.decodeCode.get(className).push(`${name}: JSON.parse<${type}>(values.get("${name}")),\n`);
|
|
92
27
|
}
|
|
93
28
|
visitClassDeclaration(node) {
|
|
94
|
-
super.visitClassDeclaration(node);
|
|
95
29
|
if (!node.members) {
|
|
96
30
|
return;
|
|
97
31
|
}
|
|
98
32
|
this.currentClass = node;
|
|
99
|
-
const name =
|
|
33
|
+
const name = getName(node);
|
|
100
34
|
this.encodeStmts.delete(name);
|
|
101
35
|
this.decodeCode.delete(name);
|
|
102
36
|
this.visit(node.members);
|
|
103
|
-
const
|
|
104
|
-
let
|
|
37
|
+
const serializedProp = `__JSON_Serialized: string = "";`;
|
|
38
|
+
let serializeFunc = ``;
|
|
105
39
|
if (this.encodeStmts.has(name) && this.encodeStmts.get(name)) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (!this.
|
|
40
|
+
serializeFunc = `
|
|
41
|
+
__JSON_Serialize(): string {
|
|
42
|
+
if (!this.__JSON_Serialized) {
|
|
109
43
|
${ // @ts-ignore
|
|
110
44
|
this.encodeStmts.get(name).join("\n")};
|
|
111
|
-
this.
|
|
45
|
+
this.__JSON_Serialized = "{" + this.__JSON_Serialized.slice(0, this.__JSON_Serialized.length - 1) + "}";
|
|
112
46
|
}
|
|
47
|
+
return this.__JSON_Serialized;
|
|
113
48
|
}
|
|
114
49
|
`;
|
|
115
50
|
}
|
|
116
51
|
else {
|
|
117
|
-
|
|
118
|
-
|
|
52
|
+
serializeFunc = `
|
|
53
|
+
__JSON_Serialize(): string {
|
|
54
|
+
return "{}";
|
|
55
|
+
}
|
|
119
56
|
`;
|
|
120
57
|
}
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
58
|
+
const deserializeFunc = `
|
|
59
|
+
__JSON_Deserialize(values: Map<string, string>): ${name} {
|
|
60
|
+
return {
|
|
124
61
|
${ // @ts-ignore
|
|
125
|
-
this.decodeCode.get(name) ? this.decodeCode.get(name).join("") :
|
|
62
|
+
this.decodeCode.get(name) ? this.decodeCode.get(name).join("") : ""}
|
|
126
63
|
}
|
|
127
|
-
return decoded
|
|
128
64
|
}
|
|
129
65
|
`;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
static visit(node) {
|
|
138
|
-
new JSONTransformer().visit(node);
|
|
66
|
+
//console.log(serializedProp, serializeFunc, deserializeFunc)
|
|
67
|
+
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
|
|
68
|
+
node.members.push(serializedProperty);
|
|
69
|
+
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
|
70
|
+
node.members.push(serializeMethod);
|
|
71
|
+
const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node);
|
|
72
|
+
node.members.push(deserializeMethod);
|
|
139
73
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
super.visitSource(source);
|
|
74
|
+
get name() {
|
|
75
|
+
return "json";
|
|
143
76
|
}
|
|
144
77
|
}
|
|
145
|
-
|
|
146
|
-
if (node.elementExpressions.length === 0)
|
|
147
|
-
return false;
|
|
148
|
-
const firstKind = node.elementExpressions[0]?.kind;
|
|
149
|
-
const isBoolean = ((0, utils_1.toString)(node.elementExpressions[0]) === 'true' || (0, utils_1.toString)(node.elementExpressions[0]) === 'false');
|
|
150
|
-
for (const chunk of node.elementExpressions) {
|
|
151
|
-
if (isBoolean) {
|
|
152
|
-
if ((0, utils_1.toString)(chunk) !== 'true' || (0, utils_1.toString)(chunk) !== 'false')
|
|
153
|
-
true;
|
|
154
|
-
}
|
|
155
|
-
else if (chunk.kind !== firstKind)
|
|
156
|
-
return true;
|
|
157
|
-
}
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
module.exports = class MyTransform extends as_1.Transform {
|
|
161
|
-
// Trigger the transform after parse.
|
|
162
|
-
afterParse(parser) {
|
|
163
|
-
// Create new transform
|
|
164
|
-
const transformer = new JSONTransformer();
|
|
165
|
-
// Loop over every source
|
|
166
|
-
for (const source of parser.sources) {
|
|
167
|
-
// Ignore all lib (std lib). Visit everything else.
|
|
168
|
-
if (!source.isLibrary && !source.internalPath.startsWith(`~lib/`)) {
|
|
169
|
-
transformer.visit(source);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
let i = 0;
|
|
173
|
-
for (const source of transformer.sources) {
|
|
174
|
-
//source.internalPath += `${i++}.ts`
|
|
175
|
-
if (i === 0) {
|
|
176
|
-
parser.sources.push(source);
|
|
177
|
-
i++;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
};
|
|
78
|
+
export default registerDecorator(new AsJSONTransform());
|
package/transform/package.json
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
|
|
2
|
+
"name": "@json-as/transform",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"author": "Jairus Tanaka",
|
|
7
|
+
"contributors": [
|
|
8
|
+
"DogWhich"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"scripts": {},
|
|
12
|
+
"devDependencies": {},
|
|
13
|
+
"dependencies": {},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/JairusSW/as-json.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"assemblyscript",
|
|
20
|
+
"json",
|
|
21
|
+
"serialize",
|
|
22
|
+
"deserialize",
|
|
23
|
+
"dynamic"
|
|
24
|
+
],
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/JairusSW/as-json/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/JairusSW/as-json#readme",
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": "./lib/index.js"
|
|
31
|
+
}
|