json-as 0.9.10 → 0.9.12
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/.github/FUNDING.yml +1 -0
- package/CHANGELOG +2 -0
- package/README.md +1 -1
- package/as-test.config.json +24 -0
- package/assembly/__tests__/test.spec.ts +0 -25
- package/assembly/custom/bs.ts +155 -0
- package/assembly/{util.ts → custom/util.ts} +28 -0
- package/assembly/deserialize/array/array.ts +2 -2
- package/assembly/deserialize/array/bool.ts +2 -2
- package/assembly/deserialize/array/float.ts +2 -2
- package/assembly/deserialize/array/integer.ts +2 -2
- package/assembly/deserialize/array/map.ts +2 -2
- package/assembly/deserialize/array/object.ts +2 -2
- package/assembly/deserialize/array/string.ts +2 -2
- package/assembly/deserialize/array.ts +3 -3
- package/assembly/deserialize/bool.ts +2 -2
- package/assembly/deserialize/integer.ts +1 -1
- package/assembly/deserialize/map.ts +3 -5
- package/assembly/deserialize/object.ts +2 -2
- package/assembly/deserialize/string.ts +3 -3
- package/assembly/index.ts +4 -191
- package/assembly/serialize/array.ts +3 -5
- package/assembly/serialize/map.ts +2 -3
- package/assembly/serialize/string.ts +63 -52
- package/assembly/test.ts +38 -34
- package/bench/benchmark.ts +18 -5
- package/bench/benchmark.wasm +0 -0
- package/package.json +14 -14
- package/transform/package.json +3 -5
- package/transform/src/index.ts +1 -0
- package/assembly/deserialize/mpz.ts +0 -12
- package/assembly/serialize/mpz.ts +0 -6
- package/assembly/serialize/unknown.ts +0 -45
- package/build/test.spec.wasm +0 -0
- package/build/test.spec.wasm.map +0 -1
- package/build/test.spec.wat +0 -107507
- package/build/test.wasm +0 -0
- package/build/test.wasm.map +0 -1
- package/build/test.wat +0 -14414
- package/tsconfig.json +0 -95
- /package/assembly/{chars.ts → custom/chars.ts} +0 -0
- /package/assembly/{sink.ts → custom/sink.ts} +0 -0
- /package/assembly/{types.ts → custom/types.ts} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
github: [JairusSW]
|
package/CHANGELOG
CHANGED
|
@@ -18,6 +18,8 @@ v0.9.8b - Fix #81 - Revert transform
|
|
|
18
18
|
v0.9.9 - Fix #82 - Initialize maps
|
|
19
19
|
v0.9.9a - Remove extraneous logs from transform
|
|
20
20
|
v0.9.10 - Fix transform type checks. switch to nodekind checks
|
|
21
|
+
v0.9.11 - Remove MpZ--implement custom serializers and deserializers in the works
|
|
22
|
+
v0.9.12 - Add compat with aspect
|
|
21
23
|
|
|
22
24
|
[UNRELEASED] v1.0.0
|
|
23
25
|
- Allow nullable primitives
|
package/README.md
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"input": [
|
|
3
|
+
"./assembly/__tests__/*.spec.ts"
|
|
4
|
+
],
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"config": "./asconfig.json",
|
|
7
|
+
"suites": [],
|
|
8
|
+
"coverage": {
|
|
9
|
+
"enabled": true,
|
|
10
|
+
"show": false
|
|
11
|
+
},
|
|
12
|
+
"buildOptions": {
|
|
13
|
+
"args": [],
|
|
14
|
+
"wasi": true,
|
|
15
|
+
"parallel": true,
|
|
16
|
+
"verbose": true
|
|
17
|
+
},
|
|
18
|
+
"runOptions": {
|
|
19
|
+
"runtime": {
|
|
20
|
+
"name": "wasmtime",
|
|
21
|
+
"run": "wasmtime <file>"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
run
|
|
6
6
|
} from "as-test/assembly";
|
|
7
7
|
import { DerivedObject, Null, ObjWithStrangeKey, ObjectWithFloat, OmitIf, Player, Vec3 } from "./types";
|
|
8
|
-
import { MpZ } from "@hypercubed/as-mpz";
|
|
9
8
|
|
|
10
9
|
describe("Should serialize strings", () => {
|
|
11
10
|
|
|
@@ -51,18 +50,6 @@ describe("Should serialize integers", () => {
|
|
|
51
50
|
|
|
52
51
|
});
|
|
53
52
|
|
|
54
|
-
describe("Should serialize MpZ (Big Int)", () => {
|
|
55
|
-
|
|
56
|
-
expect(
|
|
57
|
-
JSON.stringify<MpZ>(MpZ.from(0))
|
|
58
|
-
).toBe("0");
|
|
59
|
-
|
|
60
|
-
expect(
|
|
61
|
-
JSON.stringify<MpZ>(MpZ.from(2).pow(512))
|
|
62
|
-
).toBe("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096");
|
|
63
|
-
|
|
64
|
-
});
|
|
65
|
-
|
|
66
53
|
describe("Should serialize floats", () => {
|
|
67
54
|
|
|
68
55
|
expect(
|
|
@@ -370,18 +357,6 @@ describe("Should deserialize integers", () => {
|
|
|
370
357
|
|
|
371
358
|
});
|
|
372
359
|
|
|
373
|
-
describe("Should deserialize MpZ (Big Int)", () => {
|
|
374
|
-
|
|
375
|
-
expect(
|
|
376
|
-
JSON.parse<MpZ>("0").toString()
|
|
377
|
-
).toBe("0");
|
|
378
|
-
|
|
379
|
-
expect(
|
|
380
|
-
JSON.parse<MpZ>("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096").toString()
|
|
381
|
-
).toBe("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096");
|
|
382
|
-
|
|
383
|
-
});
|
|
384
|
-
|
|
385
360
|
describe("Should deserialize floats", () => {
|
|
386
361
|
|
|
387
362
|
expect(
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { dtoa_buffered, itoa_buffered } from "util/number";
|
|
2
|
+
import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
|
|
3
|
+
@inline const MAX_LEN: usize = 65536;
|
|
4
|
+
const STORE: usize[] = [];
|
|
5
|
+
let STORE_LEN: usize = 0;
|
|
6
|
+
const CACHE = memory.data(i32(MAX_LEN));
|
|
7
|
+
// Configurable amount of referenceable strings
|
|
8
|
+
let POINTER = changetype<usize>(CACHE);
|
|
9
|
+
@inline const MAX_CACHE = CACHE + MAX_LEN;
|
|
10
|
+
|
|
11
|
+
export namespace bs {
|
|
12
|
+
@inline export function write_int<T extends number>(num: T): void {
|
|
13
|
+
POINTER += itoa_buffered(POINTER, num) << 1;
|
|
14
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
15
|
+
}
|
|
16
|
+
@inline export function write_int_u<T extends number>(num: T): void {
|
|
17
|
+
POINTER += itoa_buffered(POINTER, num) << 1;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@inline export function write_fl<T extends number>(num: T): void {
|
|
21
|
+
POINTER += dtoa_buffered(POINTER, num) << 1;
|
|
22
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
23
|
+
}
|
|
24
|
+
@inline export function write_fl_u<T extends number>(num: T): void {
|
|
25
|
+
POINTER += dtoa_buffered(POINTER, num) << 1;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@inline export function write_b(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
|
|
29
|
+
memory.copy(POINTER, buf, bytes);
|
|
30
|
+
POINTER += bytes;
|
|
31
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
32
|
+
}
|
|
33
|
+
@inline export function write_b_u(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
|
|
34
|
+
memory.copy(POINTER, buf, bytes);
|
|
35
|
+
POINTER += bytes;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@inline export function write_s(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
|
|
39
|
+
memory.copy(POINTER, changetype<usize>(str), bytes);
|
|
40
|
+
POINTER += bytes;
|
|
41
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
42
|
+
}
|
|
43
|
+
@inline export function write_s_u(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
|
|
44
|
+
memory.copy(POINTER, changetype<usize>(str), bytes);
|
|
45
|
+
POINTER += bytes;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@inline export function write_s_se(str: string, start: usize, end: usize): void {
|
|
49
|
+
const bytes = end - start;
|
|
50
|
+
memory.copy(POINTER, changetype<usize>(str) + start, bytes);
|
|
51
|
+
POINTER += bytes;
|
|
52
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
53
|
+
}
|
|
54
|
+
@inline export function write_s_se_u(str: string, start: usize, end: usize): void {
|
|
55
|
+
const bytes = end - start;
|
|
56
|
+
memory.copy(POINTER, changetype<usize>(str) + start, bytes);
|
|
57
|
+
POINTER += bytes;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@inline export function write_16(char: i32): void {
|
|
61
|
+
store<u16>(POINTER, char);
|
|
62
|
+
POINTER += 2;
|
|
63
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
64
|
+
}
|
|
65
|
+
@inline export function write_16_u(char: i32): void {
|
|
66
|
+
store<u16>(POINTER, char);
|
|
67
|
+
//POINTER += 2;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@inline export function write_32(chars: i32): void {
|
|
71
|
+
store<u32>(POINTER, chars);
|
|
72
|
+
POINTER += 4;
|
|
73
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
74
|
+
}
|
|
75
|
+
@inline export function write_32_u(chars: i32): void {
|
|
76
|
+
store<u32>(POINTER, chars);
|
|
77
|
+
//POINTER += 4;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@inline export function write_64(chars: i64): void {
|
|
81
|
+
store<u64>(POINTER, chars);
|
|
82
|
+
POINTER += 8;
|
|
83
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@inline export function write_64_u(chars: i64): void {
|
|
87
|
+
store<u64>(POINTER, chars);
|
|
88
|
+
POINTER += 8;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@inline export function write_128(chars: v128): void {
|
|
92
|
+
store<v128>(POINTER, chars);
|
|
93
|
+
POINTER += 16;
|
|
94
|
+
if (MAX_CACHE <= POINTER) bs.shrink();
|
|
95
|
+
}
|
|
96
|
+
@inline export function write_128_u(chars: v128): void {
|
|
97
|
+
store<v128>(POINTER, chars);
|
|
98
|
+
//POINTER += 16;
|
|
99
|
+
//if (MAX_CACHE <= POINTER) bs.shrink();
|
|
100
|
+
}
|
|
101
|
+
@inline export function shrink(): void {
|
|
102
|
+
const len = POINTER - CACHE;
|
|
103
|
+
STORE_LEN += len;
|
|
104
|
+
const out = __new(
|
|
105
|
+
len,
|
|
106
|
+
idof<ArrayBuffer>()
|
|
107
|
+
);
|
|
108
|
+
memory.copy(out, CACHE, len);
|
|
109
|
+
bs.reset();
|
|
110
|
+
STORE.push(out);
|
|
111
|
+
}
|
|
112
|
+
@inline export function out<T>(): T {
|
|
113
|
+
const len = POINTER - CACHE;
|
|
114
|
+
let out = __new(
|
|
115
|
+
len + STORE_LEN,
|
|
116
|
+
idof<T>()
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
memory.copy(out, CACHE, len);
|
|
120
|
+
if (STORE_LEN) {
|
|
121
|
+
out += len;
|
|
122
|
+
for (let i = 0; i < STORE.length; i++) {
|
|
123
|
+
const ptr = changetype<usize>(unchecked(STORE[i]));
|
|
124
|
+
const storeLen = changetype<OBJECT>(ptr - TOTAL_OVERHEAD).rtSize;
|
|
125
|
+
memory.copy(out, ptr, storeLen);
|
|
126
|
+
//__unpin(ptr);
|
|
127
|
+
out += storeLen;
|
|
128
|
+
}
|
|
129
|
+
STORE_LEN = 0;
|
|
130
|
+
}
|
|
131
|
+
bs.reset();
|
|
132
|
+
|
|
133
|
+
return changetype<T>(out);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
@inline export function out_u<T>(): T {
|
|
137
|
+
const len = POINTER - CACHE;
|
|
138
|
+
const out = __new(
|
|
139
|
+
len + STORE_LEN,
|
|
140
|
+
idof<T>()
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
memory.copy(out, CACHE, len);
|
|
144
|
+
bs.reset();
|
|
145
|
+
|
|
146
|
+
return changetype<T>(out);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@inline export function _out(out: usize): void {
|
|
150
|
+
memory.copy(out, CACHE, POINTER - CACHE);
|
|
151
|
+
}
|
|
152
|
+
@inline export function reset(): void {
|
|
153
|
+
POINTER = CACHE;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -362,3 +362,31 @@ export function containsCodePoint(str: string, code: u32, start: i32, end: i32):
|
|
|
362
362
|
}
|
|
363
363
|
return false;
|
|
364
364
|
}
|
|
365
|
+
|
|
366
|
+
export function _intTo16(int: i32): i32 {
|
|
367
|
+
if (int < 10) {
|
|
368
|
+
// 0-10
|
|
369
|
+
return 48 + int;
|
|
370
|
+
} else {
|
|
371
|
+
// a-f
|
|
372
|
+
return 87 + int;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
@inline export function intTo16(int: i32): i32 {
|
|
377
|
+
const high = int >> 4;
|
|
378
|
+
const low = int & 0x0F;
|
|
379
|
+
if (low < 10) {
|
|
380
|
+
if (high < 10) {
|
|
381
|
+
return ((48 + low) << 16) | 48 + high;
|
|
382
|
+
} else {
|
|
383
|
+
return ((48 + low) << 16) | 87 + high;
|
|
384
|
+
}
|
|
385
|
+
} else {
|
|
386
|
+
if (high < 10) {
|
|
387
|
+
return ((87 + low) << 16) | 48 + high;
|
|
388
|
+
} else {
|
|
389
|
+
return ((87 + low) << 16) | 87 + high;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BRACKET_LEFT, BRACKET_RIGHT } from "../../chars";
|
|
1
|
+
import { BRACKET_LEFT, BRACKET_RIGHT } from "../../custom/chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
|
-
import { unsafeCharCodeAt } from "../../util";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../custom/util";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator valid here
|
|
6
6
|
@inline export function deserializeArrayArray<T extends unknown[][]>(data: string): T {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CHAR_E, CHAR_F, CHAR_T } from "../../chars";
|
|
2
|
-
import { unsafeCharCodeAt } from "../../util";
|
|
1
|
+
import { CHAR_E, CHAR_F, CHAR_T } from "../../custom/chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../custom/util";
|
|
3
3
|
import { deserializeBoolean } from "../bool";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator valid here
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isSpace } from "util/string";
|
|
2
|
-
import { unsafeCharCodeAt } from "../../util";
|
|
3
|
-
import { COMMA, BRACKET_RIGHT } from "../../chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../custom/util";
|
|
3
|
+
import { COMMA, BRACKET_RIGHT } from "../../custom/chars";
|
|
4
4
|
import { deserializeFloat } from "../float";
|
|
5
5
|
|
|
6
6
|
// @ts-ignore: Decorator valid here
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isSpace } from "util/string";
|
|
2
|
-
import { unsafeCharCodeAt } from "../../util";
|
|
3
|
-
import { COMMA, BRACKET_RIGHT } from "../../chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../custom/util";
|
|
3
|
+
import { COMMA, BRACKET_RIGHT } from "../../custom/chars";
|
|
4
4
|
import { deserializeInteger } from "../integer";
|
|
5
5
|
|
|
6
6
|
// @ts-ignore: Decorator valid here
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BRACE_LEFT, BRACE_RIGHT } from "../../chars";
|
|
1
|
+
import { BRACE_LEFT, BRACE_RIGHT } from "../../custom/chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
|
-
import { unsafeCharCodeAt } from "../../util";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../custom/util";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator valid here
|
|
6
6
|
@inline export function deserializeMapArray<T extends unknown[]>(data: string): T {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BRACE_LEFT, BRACE_RIGHT } from "../../chars";
|
|
1
|
+
import { BRACE_LEFT, BRACE_RIGHT } from "../../custom/chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
|
-
import { unsafeCharCodeAt } from "../../util";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../custom/util";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator valid here
|
|
6
6
|
@inline export function deserializeObjectArray<T extends unknown[]>(data: string): T {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BACK_SLASH, QUOTE } from "../../chars";
|
|
2
|
-
import { unsafeCharCodeAt } from "../../util";
|
|
1
|
+
import { BACK_SLASH, QUOTE } from "../../custom/chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../custom/util";
|
|
3
3
|
import { deserializeString } from "../string";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator valid here
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isMap } from "../util";
|
|
1
|
+
import { isMap } from "../custom/util";
|
|
2
2
|
import { deserializeArrayArray } from "./array/array";
|
|
3
3
|
import { deserializeBooleanArray } from "./array/bool";
|
|
4
4
|
import { deserializeFloatArray } from "./array/float";
|
|
@@ -31,8 +31,8 @@ export function deserializeArray<T extends unknown[]>(data: string): T {
|
|
|
31
31
|
if (isDefined(type.__DESERIALIZE)) {
|
|
32
32
|
return deserializeObjectArray<T>(data);
|
|
33
33
|
}
|
|
34
|
-
|
|
34
|
+
throw new Error("Could not parse array of type " + nameof<T>() + "! Make sure to add the @json decorator over classes!");
|
|
35
35
|
} else {
|
|
36
|
-
|
|
36
|
+
throw new Error("Could not parse array of type " + nameof<T>() + "!");
|
|
37
37
|
}
|
|
38
38
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Virtual } from "as-virtual/assembly";
|
|
2
|
-
import { containsCodePoint, unsafeCharCodeAt } from "../util";
|
|
2
|
+
import { containsCodePoint, unsafeCharCodeAt } from "../custom/util";
|
|
3
3
|
import {
|
|
4
4
|
CHAR_A,
|
|
5
5
|
BACK_SLASH,
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
CHAR_S,
|
|
19
19
|
CHAR_T,
|
|
20
20
|
CHAR_U
|
|
21
|
-
} from "../chars";
|
|
21
|
+
} from "../custom/chars";
|
|
22
22
|
import { deserializeBoolean } from "./bool";
|
|
23
23
|
import { JSON } from "..";
|
|
24
24
|
import { deserializeString } from "./string";
|
|
@@ -72,8 +72,6 @@ import { deserializeFloat } from "./float";
|
|
|
72
72
|
depth--;
|
|
73
73
|
if (depth === 0) {
|
|
74
74
|
++objectValueIndex;
|
|
75
|
-
console.log("Index: " + nameof<indexof<T>>());
|
|
76
|
-
console.log("Value: " + nameof<valueof<T>>());
|
|
77
75
|
map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, objectValueIndex)));
|
|
78
76
|
outerLoopIndex = objectValueIndex;
|
|
79
77
|
isKey = false;
|
|
@@ -181,4 +179,4 @@ function deserializeMapKey<T>(key: Virtual<string>): T {
|
|
|
181
179
|
}
|
|
182
180
|
|
|
183
181
|
throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
|
|
184
|
-
}
|
|
182
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CHAR_A, BACK_SLASH, COMMA, CHAR_E, CHAR_F, CHAR_L, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, CHAR_R, BRACE_RIGHT, BRACKET_RIGHT, CHAR_S, CHAR_T, CHAR_U } from "../chars";
|
|
1
|
+
import { unsafeCharCodeAt } from "../custom/util";
|
|
2
|
+
import { CHAR_A, BACK_SLASH, COMMA, CHAR_E, CHAR_F, CHAR_L, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, CHAR_R, BRACE_RIGHT, BRACKET_RIGHT, CHAR_S, CHAR_T, CHAR_U } from "../custom/chars";
|
|
3
3
|
import { isSpace } from "util/string";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator valid here
|
|
@@ -13,9 +13,9 @@ import {
|
|
|
13
13
|
CHAR_T,
|
|
14
14
|
TAB,
|
|
15
15
|
CHAR_U
|
|
16
|
-
} from "../chars";
|
|
17
|
-
import { Sink } from "../sink";
|
|
18
|
-
import { unsafeCharCodeAt } from "../util";
|
|
16
|
+
} from "../custom/chars";
|
|
17
|
+
import { Sink } from "../custom/sink";
|
|
18
|
+
import { unsafeCharCodeAt } from "../custom/util";
|
|
19
19
|
|
|
20
20
|
// @ts-ignore: Decorator valid here
|
|
21
21
|
@inline export function deserializeString(data: string, start: i32 = 0, end: i32 = 0): string {
|
package/assembly/index.ts
CHANGED
|
@@ -13,198 +13,14 @@ import { deserializeFloat } from "./deserialize/float";
|
|
|
13
13
|
import { deserializeObject } from "./deserialize/object";
|
|
14
14
|
import { deserializeMap } from "./deserialize/map";
|
|
15
15
|
import { deserializeDate } from "./deserialize/date";
|
|
16
|
-
import {
|
|
16
|
+
import { NULL_WORD } from "./custom/chars";
|
|
17
17
|
import { deserializeInteger } from "./deserialize/integer";
|
|
18
18
|
import { deserializeString } from "./deserialize/string";
|
|
19
|
-
import { Sink } from "./sink";
|
|
20
|
-
import { Variant } from "as-variant/assembly";
|
|
21
|
-
import { MpZ } from "@hypercubed/as-mpz";
|
|
22
|
-
import { serializeMpZ } from "./serialize/mpz";
|
|
23
|
-
import { deserializeMpZ } from "./deserialize/mpz";
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Offset of the 'storage' property in the JSON.Value class.
|
|
27
|
-
*/
|
|
28
|
-
// @ts-ignore: Decorator valid here
|
|
29
|
-
const STORAGE = offsetof<JSON.Value>("storage");
|
|
30
19
|
|
|
31
20
|
/**
|
|
32
21
|
* JSON Encoder/Decoder for AssemblyScript
|
|
33
22
|
*/
|
|
34
23
|
export namespace JSON {
|
|
35
|
-
/**
|
|
36
|
-
* Enum representing the different types supported by JSON.
|
|
37
|
-
*/
|
|
38
|
-
export enum Types {
|
|
39
|
-
U8,
|
|
40
|
-
U16,
|
|
41
|
-
U32,
|
|
42
|
-
U64,
|
|
43
|
-
F32,
|
|
44
|
-
F64,
|
|
45
|
-
Bool,
|
|
46
|
-
String,
|
|
47
|
-
ManagedString,
|
|
48
|
-
Struct,
|
|
49
|
-
ManagedStruct,
|
|
50
|
-
Array,
|
|
51
|
-
ManagedArray
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export class Value {
|
|
55
|
-
public type: i32;
|
|
56
|
-
// @ts-ignore: storage is set directly through memory
|
|
57
|
-
private storage: u64;
|
|
58
|
-
|
|
59
|
-
private constructor() { unreachable(); }
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Creates an JSON.Value instance from a given value.
|
|
63
|
-
* @param value - The value to be encapsulated.
|
|
64
|
-
* @returns An instance of JSON.Value.
|
|
65
|
-
*/
|
|
66
|
-
static from<T>(value: T): JSON.Value {
|
|
67
|
-
if (value instanceof Variant) {
|
|
68
|
-
// Handle
|
|
69
|
-
} else if (value instanceof JSON.Value) {
|
|
70
|
-
return value;
|
|
71
|
-
}
|
|
72
|
-
const out = changetype<JSON.Value>(__new(offsetof<JSON.Value>(), idof<JSON.Value>()));
|
|
73
|
-
out.set<T>(value);
|
|
74
|
-
return out;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Sets the value of the JSON.Value instance.
|
|
79
|
-
* @param value - The value to be set.
|
|
80
|
-
*/
|
|
81
|
-
set<T>(value: T): void {
|
|
82
|
-
if (isBoolean<T>()) {
|
|
83
|
-
this.type = JSON.Types.Bool;
|
|
84
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
85
|
-
} else if (value instanceof u8 || value instanceof i8) {
|
|
86
|
-
this.type = JSON.Types.U8;
|
|
87
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
88
|
-
} else if (value instanceof u16 || value instanceof i16) {
|
|
89
|
-
this.type = JSON.Types.U16;
|
|
90
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
91
|
-
} else if (value instanceof u32 || value instanceof i32) {
|
|
92
|
-
this.type = JSON.Types.U32;
|
|
93
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
94
|
-
} else if (value instanceof u64 || value instanceof i64) {
|
|
95
|
-
this.type = JSON.Types.U64;
|
|
96
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
97
|
-
} else if (value instanceof f32) {
|
|
98
|
-
this.type = JSON.Types.F64;
|
|
99
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
100
|
-
} else if (value instanceof f64) {
|
|
101
|
-
this.type = JSON.Types.F64;
|
|
102
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
103
|
-
} else if (isString<T>()) {
|
|
104
|
-
this.type = JSON.Types.String;
|
|
105
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
106
|
-
} else if (value instanceof Map) {
|
|
107
|
-
if (idof<T>() !== idof<Map<string, JSON.Value>>()) {
|
|
108
|
-
throw new Error("Maps must be of type Map<string, JSON.Value>!");
|
|
109
|
-
}
|
|
110
|
-
this.type = JSON.Types.Struct;
|
|
111
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
112
|
-
// @ts-ignore: __SERIALIZE is implemented by the transform
|
|
113
|
-
} else if (isDefined(value.__SERIALIZE)) {
|
|
114
|
-
this.type = JSON.Types.Struct;
|
|
115
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
116
|
-
} else if (isArray<T>()) {
|
|
117
|
-
// @ts-ignore: T satisfies constraints of any[]
|
|
118
|
-
this.type = JSON.Types.Array + getArrayDepth<T>(0);
|
|
119
|
-
store<T>(changetype<usize>(this), value, STORAGE);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Gets the value of the JSON.Value instance.
|
|
125
|
-
* @returns The encapsulated value.
|
|
126
|
-
*/
|
|
127
|
-
unwrap<T>(): T {
|
|
128
|
-
if (isManaged<T>()) {
|
|
129
|
-
if (this.type !== JSON.Types.Struct) throw new Error("Type mismatch");
|
|
130
|
-
if (idof<T>() !== load<u32>(changetype<usize>(this.storage), -8)) throw new Error("Type mismatch");
|
|
131
|
-
}
|
|
132
|
-
return load<T>(changetype<usize>(this), STORAGE);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Gets the value of the JSON.Value instance.
|
|
137
|
-
* @returns The encapsulated value.
|
|
138
|
-
*/
|
|
139
|
-
unwrapUnsafe<T>(): T {
|
|
140
|
-
return load<T>(changetype<usize>(this), STORAGE);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Gets the value of the JSON.Value instance.
|
|
145
|
-
* @returns The encapsulated value.
|
|
146
|
-
*/
|
|
147
|
-
get<T>(): T {
|
|
148
|
-
return load<T>(changetype<usize>(this), STORAGE);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Gets the value of the JSON.Value instance.
|
|
153
|
-
* @returns The encapsulated value.
|
|
154
|
-
*/
|
|
155
|
-
getUnsafe<T>(): T {
|
|
156
|
-
return load<T>(changetype<usize>(this), STORAGE);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Gets the value of the JSON.Value instance.
|
|
161
|
-
* @returns The encapsulated value.
|
|
162
|
-
*/
|
|
163
|
-
is<T>(): T {
|
|
164
|
-
return load<T>(changetype<usize>(this), STORAGE);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Gets the value of the JSON.Value instance.
|
|
169
|
-
* @returns The encapsulated value.
|
|
170
|
-
*/
|
|
171
|
-
clone<T>(): T {
|
|
172
|
-
return load<T>(changetype<usize>(this), STORAGE);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Converts the JSON.Value to a string representation.
|
|
177
|
-
* @param useString - If true, treats Buffer as a string.
|
|
178
|
-
* @returns The string representation of the JSON.Value.
|
|
179
|
-
*/
|
|
180
|
-
toString(useString: boolean = false): string {
|
|
181
|
-
switch (this.type) {
|
|
182
|
-
case JSON.Types.U8: return this.get<u8>().toString();
|
|
183
|
-
case JSON.Types.U16: return this.get<u16>().toString();
|
|
184
|
-
case JSON.Types.U32: return this.get<u32>().toString();
|
|
185
|
-
case JSON.Types.U64: return this.get<u64>().toString();
|
|
186
|
-
case JSON.Types.String: return "\"" + this.get<string>() + "\"";
|
|
187
|
-
case JSON.Types.Bool: return this.get<boolean>() ? TRUE_WORD : FALSE_WORD;
|
|
188
|
-
default: {
|
|
189
|
-
const arr = this.get<JSON.Value[]>();
|
|
190
|
-
if (!arr.length) return "[]";
|
|
191
|
-
const out = Sink.fromString("[");
|
|
192
|
-
for (let i = 0; i < arr.length - 1; i++) {
|
|
193
|
-
const element = unchecked(arr[i]);
|
|
194
|
-
out.write(element.toString(useString));
|
|
195
|
-
out.write(",");
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const element = unchecked(arr[arr.length - 1]);
|
|
199
|
-
out.write(element.toString(useString));
|
|
200
|
-
|
|
201
|
-
out.write("]");
|
|
202
|
-
return out.toString();
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
24
|
export class Box<T> {
|
|
209
25
|
constructor(public value: T) {}
|
|
210
26
|
@inline static from<T>(value: T): Box<T> {
|
|
@@ -236,6 +52,7 @@ export namespace JSON {
|
|
|
236
52
|
// @ts-ignore
|
|
237
53
|
} else if (isString<nonnull<T>>()) {
|
|
238
54
|
return serializeString(changetype<string>(data));
|
|
55
|
+
// @ts-ignore: Supplied by trasnform
|
|
239
56
|
} else if (isDefined(data.__SERIALIZE)) {
|
|
240
57
|
// @ts-ignore
|
|
241
58
|
return serializeObject(changetype<nonnull<T>>(data));
|
|
@@ -248,8 +65,6 @@ export namespace JSON {
|
|
|
248
65
|
} else if (data instanceof Map) {
|
|
249
66
|
// @ts-ignore
|
|
250
67
|
return serializeMap(changetype<nonnull<T>>(data));
|
|
251
|
-
} else if (data instanceof MpZ) {
|
|
252
|
-
return serializeMpZ(data);
|
|
253
68
|
} else {
|
|
254
69
|
throw new Error(
|
|
255
70
|
`Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
|
|
@@ -284,16 +99,14 @@ export namespace JSON {
|
|
|
284
99
|
return deserializeArray<nonnull<T>>(data);
|
|
285
100
|
}
|
|
286
101
|
let type: nonnull<T> = changetype<nonnull<T>>(0);
|
|
102
|
+
// @ts-ignore: Defined by trasnform
|
|
287
103
|
if (isDefined(type.__DESERIALIZE)) {
|
|
288
104
|
// @ts-ignore
|
|
289
105
|
return deserializeObject<nonnull<T>>(data.trimStart());
|
|
290
106
|
} else if (type instanceof Map) {
|
|
291
107
|
// @ts-ignore
|
|
292
108
|
return deserializeMap<nonnull<T>>(data.trimStart());
|
|
293
|
-
} else if (type instanceof
|
|
294
|
-
// @ts-ignore
|
|
295
|
-
return deserializeMpZ(data);
|
|
296
|
-
}else if (type instanceof Date) {
|
|
109
|
+
} else if (type instanceof Date) {
|
|
297
110
|
// @ts-ignore
|
|
298
111
|
return deserializeDate(data);
|
|
299
112
|
} else {
|