json-as 0.5.8 → 0.5.10
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 +21 -25
- package/assembly/__benches__/as-json.ts +5 -1
- package/assembly/__tests__/as-json.spec.ts +4 -4
- package/assembly/src/chars.ts +3 -1
- package/assembly/src/json.ts +81 -42
- package/assembly/src/util.ts +17 -1
- package/assembly/test.ts +21 -66
- package/package.json +9 -20
- package/transform/package.json +4 -2
- package/tsconfig.json +103 -0
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ Or, add it to `asconfig.json`
|
|
|
27
27
|
```
|
|
28
28
|
{
|
|
29
29
|
"options": {
|
|
30
|
-
"transform": "json-as/transform"
|
|
30
|
+
"transform": ["json-as/transform"]
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
```
|
|
@@ -36,32 +36,24 @@ Or, add it to `asconfig.json`
|
|
|
36
36
|
|
|
37
37
|
```js
|
|
38
38
|
import { JSON } from "json-as/assembly";
|
|
39
|
-
import { u128 } from "as-bignum/assembly";
|
|
40
39
|
|
|
41
|
-
// @ts-ignore
|
|
42
|
-
@json
|
|
43
|
-
class Stats {
|
|
44
|
-
wins: u128
|
|
45
|
-
loss: u128
|
|
46
|
-
}
|
|
47
40
|
// @ts-ignore
|
|
48
41
|
@json
|
|
49
42
|
class Vec3 {
|
|
50
|
-
x
|
|
51
|
-
y
|
|
52
|
-
z
|
|
43
|
+
x!: f32;
|
|
44
|
+
y!: f32;
|
|
45
|
+
z!: f32;
|
|
53
46
|
}
|
|
54
47
|
|
|
55
48
|
// @ts-ignore
|
|
56
49
|
@json
|
|
57
50
|
class Player {
|
|
58
|
-
firstName
|
|
59
|
-
lastName
|
|
60
|
-
lastActive
|
|
61
|
-
age
|
|
62
|
-
pos
|
|
63
|
-
isVerified
|
|
64
|
-
stats: Stats
|
|
51
|
+
firstName!: string;
|
|
52
|
+
lastName!: string;
|
|
53
|
+
lastActive!: i32[];
|
|
54
|
+
age!: i32;
|
|
55
|
+
pos!: Vec3 | null;
|
|
56
|
+
isVerified!: boolean;
|
|
65
57
|
}
|
|
66
58
|
|
|
67
59
|
const player: Player = {
|
|
@@ -74,14 +66,10 @@ const player: Player = {
|
|
|
74
66
|
y: 1.2,
|
|
75
67
|
z: 8.3
|
|
76
68
|
},
|
|
77
|
-
isVerified: true
|
|
78
|
-
stats: {
|
|
79
|
-
wins: u128.fromString("443"),
|
|
80
|
-
loss: u128.fromString("693")
|
|
81
|
-
}
|
|
69
|
+
isVerified: true
|
|
82
70
|
};
|
|
83
71
|
|
|
84
|
-
const stringified = JSON.stringify<Player>(
|
|
72
|
+
const stringified = JSON.stringify<Player>(player);
|
|
85
73
|
|
|
86
74
|
const parsed = JSON.parse<Player>(stringified);
|
|
87
75
|
```
|
|
@@ -89,17 +77,25 @@ const parsed = JSON.parse<Player>(stringified);
|
|
|
89
77
|
# FAQ
|
|
90
78
|
|
|
91
79
|
**Does it support the JSON specification?**
|
|
80
|
+
|
|
92
81
|
Yes, it does. However, dynamic objects and arrays are not supported, but planned in the near future.
|
|
93
82
|
|
|
94
83
|
**Is it fast?**
|
|
84
|
+
|
|
95
85
|
Look below
|
|
96
86
|
|
|
97
|
-
**How does it compare to other
|
|
87
|
+
**How does it compare to other libs?**
|
|
88
|
+
|
|
98
89
|
Its pretty much the same as the other libraries out there (near/assemblyscript-json and @serial-as/json), but it focuses highly on performance
|
|
99
90
|
|
|
100
91
|
**Will it catch invalid JSON?**
|
|
92
|
+
|
|
101
93
|
No, it does not check for invalid JSON, but gives its best shot at parsing instead. Will probably throw an error.
|
|
102
94
|
|
|
95
|
+
**How does it compare performance-wise to other libraries?**
|
|
96
|
+
|
|
97
|
+
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.
|
|
98
|
+
Both are great libraries however.
|
|
103
99
|
## Performance
|
|
104
100
|
|
|
105
101
|
**Serialize Object (Vec2):** ~7.20m ops/s
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
bench("1+1", () => {
|
|
2
|
+
blackbox("1+1".split("+w"))
|
|
3
|
+
})
|
|
4
|
+
/*import { JSON } from "..";
|
|
2
5
|
|
|
3
6
|
@json
|
|
4
7
|
class Vec2 {
|
|
@@ -73,3 +76,4 @@ bench("Stringify Float", () => {
|
|
|
73
76
|
bench("Parse Float", () => {
|
|
74
77
|
blackbox(JSON.parse<f32>(blackbox("3.14")));
|
|
75
78
|
});
|
|
79
|
+
*/
|
|
@@ -31,7 +31,7 @@ type Null = Nullable | null;
|
|
|
31
31
|
describe("Ser/de Nulls", () => {
|
|
32
32
|
canSerde<Null>(null);
|
|
33
33
|
});
|
|
34
|
-
|
|
34
|
+
/*
|
|
35
35
|
describe("Ser/de Numbers", () => {
|
|
36
36
|
it("should ser/de integers", () => {
|
|
37
37
|
canSerde<i32>(0);
|
|
@@ -40,7 +40,7 @@ describe("Ser/de Numbers", () => {
|
|
|
40
40
|
canSerde<u64>(101);
|
|
41
41
|
canSerde<i32>(-100);
|
|
42
42
|
canSerde<i64>(-101);
|
|
43
|
-
|
|
43
|
+
|
|
44
44
|
canSerde<u128>(u128.from("0"))
|
|
45
45
|
canSerde<u128>(u128.from("100"))
|
|
46
46
|
canSerde<u128>(u128.from("101"))
|
|
@@ -65,7 +65,7 @@ describe("Ser/de Numbers", () => {
|
|
|
65
65
|
canSerde<i128Safe>(i128Safe.from("100"))
|
|
66
66
|
canSerde<i128Safe>(i128Safe.from("101"))
|
|
67
67
|
canSerde<i128Safe>(i128Safe.from("-100"))
|
|
68
|
-
canSerde<i128Safe>(i128Safe.from("-101"))
|
|
68
|
+
canSerde<i128Safe>(i128Safe.from("-101"))
|
|
69
69
|
|
|
70
70
|
//canSerde<i256Safe>(new i256Safe(10, 11, 500, 501))
|
|
71
71
|
});
|
|
@@ -199,4 +199,4 @@ describe("Ser/de Objects", () => {
|
|
|
199
199
|
isVerified: true,
|
|
200
200
|
});
|
|
201
201
|
});
|
|
202
|
-
})
|
|
202
|
+
});*/
|
package/assembly/src/chars.ts
CHANGED
|
@@ -24,4 +24,6 @@ export const nullWord = "null";
|
|
|
24
24
|
export const leftBracketWord = "[";
|
|
25
25
|
export const emptyArrayWord = "[]";
|
|
26
26
|
export const commaWord = ",";
|
|
27
|
-
export const rightBracketWord = "]";
|
|
27
|
+
export const rightBracketWord = "]";
|
|
28
|
+
// Escape Codes
|
|
29
|
+
export const newLineCode = "\n".charCodeAt(0);
|
package/assembly/src/json.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { u128, u128Safe, u256, u256Safe, i128, i128Safe
|
|
1
|
+
import { u128, u128Safe, u256, u256Safe, i128, i128Safe } from "as-bignum/assembly";
|
|
2
2
|
import { StringSink } from "as-string-sink/assembly";
|
|
3
|
+
import { itoa32, itoa64, dtoa, dtoa_buffered } from "util/number";
|
|
3
4
|
import { isSpace } from "util/string";
|
|
4
5
|
import {
|
|
5
6
|
backSlashCode,
|
|
@@ -22,7 +23,7 @@ import {
|
|
|
22
23
|
uCode,
|
|
23
24
|
emptyArrayWord
|
|
24
25
|
} from "./chars";
|
|
25
|
-
import { isBigNum, unsafeCharCodeAt } from "./util";
|
|
26
|
+
import { escapeChar, isBigNum, unsafeCharCodeAt } from "./util";
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* JSON Encoder/Decoder for AssemblyScript
|
|
@@ -39,7 +40,52 @@ export namespace JSON {
|
|
|
39
40
|
export function stringify<T>(data: T): string {
|
|
40
41
|
// String
|
|
41
42
|
if (isString<T>()) {
|
|
42
|
-
|
|
43
|
+
let result = new StringSink("\"");
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
for (let i = 0; i < data.length; i++) {
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
switch (unsafeCharCodeAt(data, i)) {
|
|
48
|
+
case 0x22: {
|
|
49
|
+
result.write("\\\"");
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case 0x5C: {
|
|
53
|
+
result.write("\\\\");
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case 0x08: {
|
|
57
|
+
result.write("\\b");
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case 0x0A: {
|
|
61
|
+
result.write("\\n");
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
case 0x0D: {
|
|
65
|
+
result.write("\\r");
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case 0x09: {
|
|
69
|
+
result.write("\\t");
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case 0x0C: {
|
|
73
|
+
result.write("\\f");
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case 0x0B: {
|
|
77
|
+
result.write("\\u000b");
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
default: {
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
result.write(data.charAt(i));
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
result.write("\"");
|
|
88
|
+
return result.toString();
|
|
43
89
|
}
|
|
44
90
|
// Boolean
|
|
45
91
|
else if (isBoolean<T>()) {
|
|
@@ -81,8 +127,6 @@ export namespace JSON {
|
|
|
81
127
|
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
|
|
82
128
|
// @ts-ignore
|
|
83
129
|
return data.toString();
|
|
84
|
-
} else if (data instanceof Date) {
|
|
85
|
-
return data.toISOString();
|
|
86
130
|
} else {
|
|
87
131
|
throw new Error(`Could not serialize data of type ${nameof<T>()}. Invalid data provided.`);
|
|
88
132
|
}
|
|
@@ -95,8 +139,9 @@ export namespace JSON {
|
|
|
95
139
|
* @param data string
|
|
96
140
|
* @returns T
|
|
97
141
|
*/
|
|
142
|
+
// @ts-ignore
|
|
98
143
|
export function parse<T>(data: string): T {
|
|
99
|
-
let type
|
|
144
|
+
let type: T;
|
|
100
145
|
if (isString<T>()) {
|
|
101
146
|
// @ts-ignore
|
|
102
147
|
return parseString(data);
|
|
@@ -123,8 +168,9 @@ export namespace JSON {
|
|
|
123
168
|
throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Invalide data provided.`);
|
|
124
169
|
}
|
|
125
170
|
}
|
|
171
|
+
// @ts-ignore
|
|
126
172
|
function parseObjectValue<T>(data: string): T {
|
|
127
|
-
let type
|
|
173
|
+
let type: T;
|
|
128
174
|
if (isString<T>()) {
|
|
129
175
|
// @ts-ignore
|
|
130
176
|
return data.replaceAll('\\"', '"');
|
|
@@ -142,9 +188,7 @@ export namespace JSON {
|
|
|
142
188
|
return null;
|
|
143
189
|
// @ts-ignore
|
|
144
190
|
} else if (isDefined(type.__JSON_Set_Key)) {
|
|
145
|
-
|
|
146
|
-
//if (isNullable<T>()) return null;
|
|
147
|
-
return parseObject<T>(data);
|
|
191
|
+
return parseObject<T>(data.trimStart());
|
|
148
192
|
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
|
|
149
193
|
// @ts-ignore
|
|
150
194
|
return parseBigNum<T>(data);
|
|
@@ -154,11 +198,6 @@ export namespace JSON {
|
|
|
154
198
|
throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Invalide data provided.`)
|
|
155
199
|
}
|
|
156
200
|
}
|
|
157
|
-
// @ts-ignore
|
|
158
|
-
@unsafe
|
|
159
|
-
export function createObjectUnsafe<T>(): T {
|
|
160
|
-
return changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))
|
|
161
|
-
}
|
|
162
201
|
}
|
|
163
202
|
|
|
164
203
|
// @ts-ignore
|
|
@@ -197,37 +236,35 @@ function parseBoolean<T extends boolean>(data: string): T {
|
|
|
197
236
|
|
|
198
237
|
// @ts-ignore
|
|
199
238
|
@inline
|
|
200
|
-
|
|
201
|
-
|
|
239
|
+
// @ts-ignore
|
|
240
|
+
export function parseNumber<T>(data: string): T {
|
|
202
241
|
// @ts-ignore
|
|
203
|
-
|
|
242
|
+
const type: T = 0;
|
|
204
243
|
// @ts-ignore
|
|
205
|
-
|
|
244
|
+
if (type instanceof f64) return f32.parse(data);
|
|
206
245
|
// @ts-ignore
|
|
207
|
-
else if (type instanceof
|
|
246
|
+
else if (type instanceof f32) return f32.parse(data);
|
|
208
247
|
// @ts-ignore
|
|
209
|
-
else if (type instanceof
|
|
248
|
+
else if (type instanceof u64) return u64.parse(data);
|
|
210
249
|
// @ts-ignore
|
|
211
|
-
else if (type instanceof
|
|
250
|
+
else if (type instanceof u32) return u32.parse(data);
|
|
212
251
|
// @ts-ignore
|
|
213
|
-
else if (type instanceof
|
|
252
|
+
else if (type instanceof u8) return u8.parse(data);
|
|
214
253
|
// @ts-ignore
|
|
215
|
-
else if (type instanceof
|
|
254
|
+
else if (type instanceof u16) return u16.parse(data);
|
|
216
255
|
// @ts-ignore
|
|
217
|
-
else if (type instanceof
|
|
256
|
+
else if (type instanceof i64) return i64.parse(data);
|
|
218
257
|
// @ts-ignore
|
|
219
|
-
else if (type instanceof
|
|
258
|
+
else if (type instanceof i32) return i32.parse(data);
|
|
220
259
|
// @ts-ignore
|
|
221
|
-
else if (type instanceof
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
`JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.`
|
|
225
|
-
);
|
|
260
|
+
else if (type instanceof i16) return i16.parse(data);
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
else if (type instanceof i8) return i8.parse(data);
|
|
226
263
|
}
|
|
227
264
|
|
|
228
265
|
// @ts-ignore
|
|
229
266
|
@inline
|
|
230
|
-
|
|
267
|
+
function parseObject<T>(data: string): T {
|
|
231
268
|
let schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
|
|
232
269
|
let key = "";
|
|
233
270
|
let isKey = false;
|
|
@@ -344,9 +381,8 @@ export function parseObject<T>(data: string): T {
|
|
|
344
381
|
// @ts-ignore
|
|
345
382
|
@inline
|
|
346
383
|
// @ts-ignore
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (type instanceof String) {
|
|
384
|
+
function parseArray<T extends unknown[]>(data: string): T {
|
|
385
|
+
if (isString<valueof<T>>()) {
|
|
350
386
|
return <T>parseStringArray(data);
|
|
351
387
|
} else if (isBoolean<valueof<T>>()) {
|
|
352
388
|
// @ts-ignore
|
|
@@ -358,7 +394,10 @@ export function parseArray<T extends unknown[]>(data: string): T {
|
|
|
358
394
|
// @ts-ignore
|
|
359
395
|
return parseArrayArray<T>(data);
|
|
360
396
|
// @ts-ignore
|
|
361
|
-
}
|
|
397
|
+
}
|
|
398
|
+
const type = instantiate<T>();
|
|
399
|
+
// @ts-ignore
|
|
400
|
+
if (isDefined(type.__JSON_Set_Key)) {
|
|
362
401
|
// @ts-ignore
|
|
363
402
|
return parseObjectArray<T>(data);
|
|
364
403
|
}
|
|
@@ -366,7 +405,7 @@ export function parseArray<T extends unknown[]>(data: string): T {
|
|
|
366
405
|
|
|
367
406
|
// @ts-ignore
|
|
368
407
|
@inline
|
|
369
|
-
|
|
408
|
+
function parseStringArray(data: string): string[] {
|
|
370
409
|
const result: string[] = [];
|
|
371
410
|
let lastPos = 0;
|
|
372
411
|
let instr = false;
|
|
@@ -386,7 +425,7 @@ export function parseStringArray(data: string): string[] {
|
|
|
386
425
|
|
|
387
426
|
// @ts-ignore
|
|
388
427
|
@inline
|
|
389
|
-
|
|
428
|
+
function parseBooleanArray<T extends boolean[]>(data: string): T {
|
|
390
429
|
const result = instantiate<T>();
|
|
391
430
|
let lastPos = 1;
|
|
392
431
|
let char = 0;
|
|
@@ -414,7 +453,7 @@ export function parseBooleanArray<T extends boolean[]>(data: string): T {
|
|
|
414
453
|
|
|
415
454
|
// @ts-ignore
|
|
416
455
|
@inline
|
|
417
|
-
|
|
456
|
+
function parseNumberArray<T extends number[]>(data: string): T {
|
|
418
457
|
const result = instantiate<T>();
|
|
419
458
|
let lastPos = 0;
|
|
420
459
|
let char = 0;
|
|
@@ -440,7 +479,7 @@ export function parseNumberArray<T extends number[]>(data: string): T {
|
|
|
440
479
|
|
|
441
480
|
// @ts-ignore
|
|
442
481
|
@inline
|
|
443
|
-
|
|
482
|
+
function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
444
483
|
const result = instantiate<T>();
|
|
445
484
|
let char = 0;
|
|
446
485
|
let lastPos = 0;
|
|
@@ -470,7 +509,7 @@ export function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
|
470
509
|
|
|
471
510
|
// @ts-ignore
|
|
472
511
|
@inline
|
|
473
|
-
|
|
512
|
+
function parseObjectArray<T extends unknown[][]>(data: string): T {
|
|
474
513
|
const result = instantiate<T>();
|
|
475
514
|
let char = 0;
|
|
476
515
|
let lastPos = 1;
|
|
@@ -496,4 +535,4 @@ export function parseObjectArray<T extends unknown[][]>(data: string): T {
|
|
|
496
535
|
}
|
|
497
536
|
}
|
|
498
537
|
return result;
|
|
499
|
-
}
|
|
538
|
+
}
|
package/assembly/src/util.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { u128, u128Safe, u256, u256Safe, i128, i128Safe, i256Safe } from "as-big
|
|
|
5
5
|
|
|
6
6
|
// @ts-ignore
|
|
7
7
|
@inline
|
|
8
|
-
|
|
8
|
+
export function isBigNum<T>(): boolean {
|
|
9
9
|
if (idof<T>() == idof<u128>()) return true;
|
|
10
10
|
if (idof<T>() == idof<u128Safe>()) return true;
|
|
11
11
|
if (idof<T>() == idof<u256>()) return true;
|
|
@@ -43,3 +43,19 @@ export function removeWhitespace(data: string): string {
|
|
|
43
43
|
}
|
|
44
44
|
return result.toString();
|
|
45
45
|
}
|
|
46
|
+
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
@inline
|
|
49
|
+
export function escapeChar(char: string): string {
|
|
50
|
+
switch (unsafeCharCodeAt(char, 0)) {
|
|
51
|
+
case 0x22: return '\\"';
|
|
52
|
+
case 0x5C: return "\\\\";
|
|
53
|
+
case 0x08: return "\\b";
|
|
54
|
+
case 0x0A: return "\\n";
|
|
55
|
+
case 0x0D: return "\\r";
|
|
56
|
+
case 0x09: return "\\t";
|
|
57
|
+
case 0x0C: return "\\f";
|
|
58
|
+
case 0x0B: return "\\u000b";
|
|
59
|
+
default: return char;
|
|
60
|
+
}
|
|
61
|
+
}
|
package/assembly/test.ts
CHANGED
|
@@ -1,81 +1,38 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { u128 } from "as-bignum/assembly";
|
|
2
2
|
import {
|
|
3
3
|
JSON
|
|
4
4
|
} from ".";
|
|
5
|
-
@json
|
|
6
|
-
class Player {
|
|
7
|
-
firstName: string;
|
|
8
|
-
lastName: string;
|
|
9
|
-
lastActive: i32[];
|
|
10
|
-
age: i32;
|
|
11
|
-
pos: Vec3 | null;
|
|
12
|
-
isVerified: boolean;
|
|
13
|
-
stats: Stats
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@json
|
|
17
|
-
class Contacts {
|
|
18
|
-
type: string
|
|
19
|
-
player: string
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const player = JSON.createObjectUnsafe<Player>()
|
|
23
|
-
|
|
24
|
-
player.firstName = "John";
|
|
25
|
-
player.lastName = "West";
|
|
26
|
-
player.age = 23;
|
|
27
|
-
|
|
28
|
-
const contact: Contacts = {
|
|
29
|
-
player: JSON.stringify(player),
|
|
30
|
-
type: "friends"
|
|
31
|
-
}
|
|
32
5
|
|
|
33
|
-
let stringifiedContact = JSON.stringify(contact);
|
|
34
|
-
console.log("Input (Should see backslashes logged): " + stringifiedContact);
|
|
35
|
-
const contacts = JSON.parse<Contacts>(stringifiedContact)
|
|
36
|
-
console.log("Player: " + contacts.player);
|
|
37
|
-
console.log("Type: " + contacts.type);
|
|
38
|
-
const parsedPlayer = JSON.parse<Player>(contacts.player);
|
|
39
|
-
console.log("Final Player: " + JSON.stringify(parsedPlayer));
|
|
40
|
-
console.log("Final Result (Contacts): " + JSON.stringify(contacts));/*
|
|
41
|
-
/*
|
|
42
6
|
// @ts-ignore
|
|
43
7
|
@json
|
|
44
8
|
class Stats {
|
|
45
|
-
wins
|
|
46
|
-
loss
|
|
47
|
-
}*/
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
@json
|
|
50
|
-
class Vec3 {
|
|
51
|
-
x: f32;
|
|
52
|
-
y: f32;
|
|
53
|
-
z: f32;
|
|
9
|
+
wins!: u128
|
|
10
|
+
loss!: u128
|
|
54
11
|
}
|
|
55
12
|
// @ts-ignore
|
|
56
13
|
@json
|
|
57
|
-
class
|
|
58
|
-
|
|
14
|
+
class Vec3 {
|
|
15
|
+
x!: f32;
|
|
16
|
+
y!: f32;
|
|
17
|
+
z!: f32;
|
|
59
18
|
}
|
|
19
|
+
|
|
60
20
|
const vec: Vec3 = {
|
|
61
21
|
x: 3.4,
|
|
62
22
|
y: 1.2,
|
|
63
23
|
z: 8.3
|
|
64
24
|
}
|
|
65
|
-
|
|
66
|
-
data: JSON.stringify(vec)
|
|
67
|
-
}
|
|
68
|
-
/*
|
|
25
|
+
|
|
69
26
|
// @ts-ignore
|
|
70
27
|
@json
|
|
71
28
|
class Player {
|
|
72
|
-
firstName
|
|
73
|
-
lastName
|
|
74
|
-
lastActive
|
|
75
|
-
age
|
|
76
|
-
pos
|
|
77
|
-
isVerified
|
|
78
|
-
stats
|
|
29
|
+
firstName!: string;
|
|
30
|
+
lastName!: string;
|
|
31
|
+
lastActive!: i32[];
|
|
32
|
+
age!: i32;
|
|
33
|
+
pos!: Vec3 | null;
|
|
34
|
+
isVerified!: boolean;
|
|
35
|
+
stats!: Stats
|
|
79
36
|
}
|
|
80
37
|
|
|
81
38
|
const player: Player = {
|
|
@@ -94,10 +51,8 @@ const player: Player = {
|
|
|
94
51
|
loss: u128.fromString("693")
|
|
95
52
|
}
|
|
96
53
|
};
|
|
97
|
-
|
|
98
|
-
const serializedPlayer = JSON.stringify<
|
|
99
|
-
|
|
100
|
-
const deserializedPlayer = JSON.parse<
|
|
101
|
-
|
|
102
|
-
wasi_console.log("Deserialize Vec3: " + JSON.stringify(JSON.parse<Vec3>(deserializedPlayer.data)))
|
|
103
|
-
*/
|
|
54
|
+
|
|
55
|
+
const serializedPlayer = JSON.stringify<Player>(player);
|
|
56
|
+
console.log("Serialized Player: " + serializedPlayer);
|
|
57
|
+
const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
|
|
58
|
+
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.10",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -16,23 +16,18 @@
|
|
|
16
16
|
"test:wasmtime": "wasmtime ./build/test.wasm",
|
|
17
17
|
"test:lunatic": "lunatic ./build/test.wasm",
|
|
18
18
|
"test:wasm3": "wasm3 ./build/test.wasm",
|
|
19
|
-
"prettier": "as-prettier -w ."
|
|
20
|
-
"asbuild:debug": "asc assembly/index.ts --target debug",
|
|
21
|
-
"asbuild:release": "asc assembly/index.ts --target release",
|
|
22
|
-
"asbuild": "yarn asbuild:debug && yarn asbuild:release",
|
|
23
|
-
"test": "node tests",
|
|
24
|
-
"start": "npx serve ."
|
|
19
|
+
"prettier": "as-prettier -w ."
|
|
25
20
|
},
|
|
26
21
|
"devDependencies": {
|
|
27
|
-
"@as-pect/cli": "^
|
|
22
|
+
"@as-pect/cli": "^8.0.0",
|
|
28
23
|
"@as-tral/cli": "^1.2.0",
|
|
29
|
-
"@assemblyscript/loader": "^0.
|
|
24
|
+
"@assemblyscript/loader": "^0.25.0",
|
|
30
25
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
31
26
|
"as-bignum": "^0.2.23",
|
|
32
|
-
"assemblyscript": "^0.
|
|
33
|
-
"assemblyscript-prettier": "^1.0.
|
|
34
|
-
"prettier": "^2.
|
|
35
|
-
"typescript": "^4.
|
|
27
|
+
"assemblyscript": "^0.25.0",
|
|
28
|
+
"assemblyscript-prettier": "^1.0.6",
|
|
29
|
+
"prettier": "^2.8.1",
|
|
30
|
+
"typescript": "^4.9.4"
|
|
36
31
|
},
|
|
37
32
|
"dependencies": {
|
|
38
33
|
"as-string-sink": "^0.5.0",
|
|
@@ -54,11 +49,5 @@
|
|
|
54
49
|
"url": "https://github.com/JairusSW/as-json/issues"
|
|
55
50
|
},
|
|
56
51
|
"homepage": "https://github.com/JairusSW/as-json#readme",
|
|
57
|
-
"type": "module"
|
|
58
|
-
"exports": {
|
|
59
|
-
".": {
|
|
60
|
-
"import": "./build/release.js",
|
|
61
|
-
"types": "./build/release.d.ts"
|
|
62
|
-
}
|
|
63
|
-
}
|
|
52
|
+
"type": "module"
|
|
64
53
|
}
|
package/transform/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@json-as/transform",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.10",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
7
7
|
"contributors": [
|
|
8
|
-
"DogWhich"
|
|
8
|
+
"DogWhich",
|
|
9
|
+
"Josh Tenner",
|
|
10
|
+
"Rom"
|
|
9
11
|
],
|
|
10
12
|
"license": "MIT",
|
|
11
13
|
"devDependencies": {},
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
39
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
40
|
+
|
|
41
|
+
/* JavaScript Support */
|
|
42
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
43
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
44
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
45
|
+
|
|
46
|
+
/* Emit */
|
|
47
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
48
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
49
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
50
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
51
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
52
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
53
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
54
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
55
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
56
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
57
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
58
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
59
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
60
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
61
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
62
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
63
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
64
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
65
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
66
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
67
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
68
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
69
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
70
|
+
|
|
71
|
+
/* Interop Constraints */
|
|
72
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
73
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
74
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
75
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
76
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
77
|
+
|
|
78
|
+
/* Type Checking */
|
|
79
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
80
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
81
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
82
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
83
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
84
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
85
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
86
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
87
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
88
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
89
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
90
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
91
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
92
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
93
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
94
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
95
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
96
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
97
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
98
|
+
|
|
99
|
+
/* Completeness */
|
|
100
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
101
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
102
|
+
}
|
|
103
|
+
}
|