json-as 0.9.7 → 0.9.8-b
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/workflows/nodejs.yml +9 -10
- package/CHANGELOG +11 -2
- package/README.md +52 -7
- package/asconfig.json +24 -3
- package/assembly/__tests__/test.spec.ts +564 -0
- package/assembly/__tests__/types.ts +82 -0
- package/assembly/{src/chars.ts → chars.ts} +36 -36
- package/assembly/deserialize/array/array.ts +4 -4
- package/assembly/deserialize/array/bool.ts +4 -4
- package/assembly/deserialize/array/float.ts +4 -4
- package/assembly/deserialize/array/integer.ts +4 -4
- package/assembly/deserialize/array/map.ts +4 -4
- package/assembly/deserialize/array/object.ts +4 -4
- package/assembly/deserialize/array/string.ts +4 -4
- package/assembly/deserialize/array.ts +5 -4
- package/assembly/deserialize/bool.ts +4 -4
- package/assembly/deserialize/date.ts +2 -2
- package/assembly/deserialize/float.ts +2 -2
- package/assembly/deserialize/integer.ts +3 -3
- package/assembly/deserialize/map.ts +4 -4
- package/assembly/deserialize/mpz.ts +12 -0
- package/assembly/deserialize/object.ts +4 -4
- package/assembly/deserialize/string.ts +5 -5
- package/assembly/index.ts +25 -23
- package/assembly/serialize/array.ts +6 -5
- package/assembly/serialize/bool.ts +2 -2
- package/assembly/serialize/date.ts +2 -2
- package/assembly/serialize/float.ts +2 -2
- package/assembly/serialize/integer.ts +2 -2
- package/assembly/serialize/map.ts +8 -7
- package/assembly/serialize/mpz.ts +6 -0
- package/assembly/serialize/object.ts +2 -2
- package/assembly/serialize/string.ts +5 -5
- package/assembly/serialize/unknown.ts +5 -5
- package/assembly/test.ts +10 -47
- package/assembly/types.ts +4 -0
- package/assembly/{src/util.ts → util.ts} +3 -3
- package/bench/benchmark.ts +1 -1
- package/build/test.spec.wasm +0 -0
- package/build/test.spec.wasm.map +1 -0
- package/build/test.spec.wat +112616 -0
- package/build/test.wasm +0 -0
- package/build/test.wasm.map +1 -0
- package/build/test.wat +12267 -0
- package/package.json +13 -14
- package/transform/lib/visitor.js +516 -0
- package/transform/package.json +1 -1
- package/transform/src/visitor.ts +543 -0
- package/transform/tsconfig.json +23 -63
- package/tsconfig.json +13 -13
- package/as-pect.asconfig.json +0 -24
- package/as-pect.config.js +0 -30
- package/assembly/__tests__/deserialize.spec.ts +0 -301
- package/assembly/__tests__/serialize.spec.ts +0 -398
- package/assembly/deserialize/box.ts +0 -17
- package/assembly/serialize/box.ts +0 -11
- package/develop/assembly/serialize/unknown.ts +0 -46
- package/transform/lib/index.old.js +0 -257
- package/transform/lib/types.js +0 -17
- package/transform/src/index.old.ts +0 -312
- /package/assembly/{src/sink.ts → sink.ts} +0 -0
|
@@ -1,76 +1,76 @@
|
|
|
1
1
|
// Characters
|
|
2
2
|
// @ts-ignore = Decorator is valid here
|
|
3
|
-
export const COMMA = 44;
|
|
3
|
+
@inline export const COMMA = 44;
|
|
4
4
|
// @ts-ignore = Decorator is valid here
|
|
5
|
-
export const QUOTE = 34;
|
|
5
|
+
@inline export const QUOTE = 34;
|
|
6
6
|
// @ts-ignore = Decorator is valid here
|
|
7
|
-
export const BACK_SLASH = 92;
|
|
7
|
+
@inline export const BACK_SLASH = 92;
|
|
8
8
|
// @ts-ignore: Decorator is valid here
|
|
9
|
-
export const FWD_SLASH = 47;
|
|
9
|
+
@inline export const FWD_SLASH = 47;
|
|
10
10
|
// @ts-ignore: Decorator is valid here
|
|
11
|
-
export const BRACE_LEFT = 123;
|
|
11
|
+
@inline export const BRACE_LEFT = 123;
|
|
12
12
|
// @ts-ignore: Decorator is valid here
|
|
13
|
-
export const BRACE_RIGHT = 125;
|
|
13
|
+
@inline export const BRACE_RIGHT = 125;
|
|
14
14
|
// @ts-ignore: Decorator is valid here
|
|
15
|
-
export const BRACKET_LEFT = 91;
|
|
15
|
+
@inline export const BRACKET_LEFT = 91;
|
|
16
16
|
// @ts-ignore: Decorator is valid here
|
|
17
|
-
export const BRACKET_RIGHT = 93;
|
|
17
|
+
@inline export const BRACKET_RIGHT = 93;
|
|
18
18
|
// @ts-ignore: Decorator is valid here
|
|
19
|
-
export const COLON = 58;
|
|
19
|
+
@inline export const COLON = 58;
|
|
20
20
|
// @ts-ignore: Decorator is valid here
|
|
21
|
-
export const CHAR_T = 116;
|
|
21
|
+
@inline export const CHAR_T = 116;
|
|
22
22
|
// @ts-ignore: Decorator is valid here
|
|
23
|
-
export const CHAR_R = 114;
|
|
23
|
+
@inline export const CHAR_R = 114;
|
|
24
24
|
// @ts-ignore: Decorator is valid here
|
|
25
|
-
export const CHAR_U = 117;
|
|
25
|
+
@inline export const CHAR_U = 117;
|
|
26
26
|
// @ts-ignore: Decorator is valid here
|
|
27
|
-
export const CHAR_E = 101;
|
|
27
|
+
@inline export const CHAR_E = 101;
|
|
28
28
|
// @ts-ignore: Decorator is valid here
|
|
29
|
-
export const CHAR_F = 102;
|
|
29
|
+
@inline export const CHAR_F = 102;
|
|
30
30
|
// @ts-ignore: Decorator is valid here
|
|
31
|
-
export const CHAR_A = 97;
|
|
31
|
+
@inline export const CHAR_A = 97;
|
|
32
32
|
// @ts-ignore: Decorator is valid here
|
|
33
|
-
export const CHAR_L = 108;
|
|
33
|
+
@inline export const CHAR_L = 108;
|
|
34
34
|
// @ts-ignore: Decorator is valid here
|
|
35
|
-
export const CHAR_S = 115;
|
|
35
|
+
@inline export const CHAR_S = 115;
|
|
36
36
|
// @ts-ignore = Decorator is valid here
|
|
37
|
-
export const CHAR_N = 110;
|
|
37
|
+
@inline export const CHAR_N = 110;
|
|
38
38
|
// @ts-ignore = Decorator is valid here
|
|
39
|
-
export const CHAR_B = 98;
|
|
39
|
+
@inline export const CHAR_B = 98;
|
|
40
40
|
// Strings
|
|
41
41
|
// @ts-ignore: Decorator is valid here
|
|
42
|
-
export const TRUE_WORD = "true";
|
|
42
|
+
@inline export const TRUE_WORD = "true";
|
|
43
43
|
// @ts-ignore: Decorator is valid here
|
|
44
|
-
export const FALSE_WORD = "false";
|
|
44
|
+
@inline export const FALSE_WORD = "false";
|
|
45
45
|
// @ts-ignore: Decorator is valid here
|
|
46
|
-
export const NULL_WORD = "null";
|
|
46
|
+
@inline export const NULL_WORD = "null";
|
|
47
47
|
// @ts-ignore: Decorator is valid here
|
|
48
|
-
export const BRACE_LEFT_WORD = "{";
|
|
48
|
+
@inline export const BRACE_LEFT_WORD = "{";
|
|
49
49
|
// @ts-ignore: Decorator is valid here
|
|
50
|
-
export const BRACKET_LEFT_WORD = "[";
|
|
50
|
+
@inline export const BRACKET_LEFT_WORD = "[";
|
|
51
51
|
// @ts-ignore: Decorator is valid here
|
|
52
|
-
export const EMPTY_BRACKET_WORD = "[]";
|
|
52
|
+
@inline export const EMPTY_BRACKET_WORD = "[]";
|
|
53
53
|
// @ts-ignore: Decorator is valid here
|
|
54
|
-
export const COLON_WORD = ":";
|
|
54
|
+
@inline export const COLON_WORD = ":";
|
|
55
55
|
// @ts-ignore: Decorator is valid here
|
|
56
|
-
export const COMMA_WORD = ",";
|
|
56
|
+
@inline export const COMMA_WORD = ",";
|
|
57
57
|
// @ts-ignore: Decorator is valid here
|
|
58
|
-
export const BRACE_RIGHT_WORD = "}";
|
|
58
|
+
@inline export const BRACE_RIGHT_WORD = "}";
|
|
59
59
|
// @ts-ignore: Decorator is valid here
|
|
60
|
-
export const BRACKET_RIGHT_WORD = "]";
|
|
60
|
+
@inline export const BRACKET_RIGHT_WORD = "]";
|
|
61
61
|
// @ts-ignore: Decorator is valid here
|
|
62
|
-
export const QUOTE_WORD = "\"";
|
|
62
|
+
@inline export const QUOTE_WORD = "\"";
|
|
63
63
|
// @ts-ignore: Decorator is valid here
|
|
64
|
-
export const EMPTY_QUOTE_WORD = "\"\"";
|
|
64
|
+
@inline export const EMPTY_QUOTE_WORD = "\"\"";
|
|
65
65
|
|
|
66
66
|
// Escape Codes
|
|
67
67
|
// @ts-ignore: Decorator is valid here
|
|
68
|
-
export const BACKSPACE = 8; // \b
|
|
68
|
+
@inline export const BACKSPACE = 8; // \b
|
|
69
69
|
// @ts-ignore: Decorator is valid here
|
|
70
|
-
export const TAB = 9; // \t
|
|
70
|
+
@inline export const TAB = 9; // \t
|
|
71
71
|
// @ts-ignore: Decorator is valid here
|
|
72
|
-
export const NEW_LINE = 10; // \n
|
|
72
|
+
@inline export const NEW_LINE = 10; // \n
|
|
73
73
|
// @ts-ignore: Decorator is valid here
|
|
74
|
-
export const FORM_FEED = 12; // \f
|
|
74
|
+
@inline export const FORM_FEED = 12; // \f
|
|
75
75
|
// @ts-ignore: Decorator is valid here
|
|
76
|
-
export const CARRIAGE_RETURN = 13; // \r
|
|
76
|
+
@inline export const CARRIAGE_RETURN = 13; // \r
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { BRACKET_LEFT, BRACKET_RIGHT } from "../../
|
|
1
|
+
import { BRACKET_LEFT, BRACKET_RIGHT } from "../../chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
|
-
import { unsafeCharCodeAt } from "../../
|
|
3
|
+
import { unsafeCharCodeAt } from "../../util";
|
|
4
4
|
|
|
5
|
-
// @ts-ignore: Decorator
|
|
6
|
-
export function deserializeArrayArray<T extends unknown[][]>(data: string): T {
|
|
5
|
+
// @ts-ignore: Decorator valid here
|
|
6
|
+
@inline export function deserializeArrayArray<T extends unknown[][]>(data: string): T {
|
|
7
7
|
const result = instantiate<T>();
|
|
8
8
|
let lastPos = 0;
|
|
9
9
|
let depth = 0;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { CHAR_E, CHAR_F, CHAR_T } from "../../
|
|
2
|
-
import { unsafeCharCodeAt } from "../../
|
|
1
|
+
import { CHAR_E, CHAR_F, CHAR_T } from "../../chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../util";
|
|
3
3
|
import { deserializeBoolean } from "../bool";
|
|
4
4
|
|
|
5
|
-
// @ts-ignore: Decorator
|
|
6
|
-
export function deserializeBooleanArray<T extends boolean[]>(data: string): T {
|
|
5
|
+
// @ts-ignore: Decorator valid here
|
|
6
|
+
@inline export function deserializeBooleanArray<T extends boolean[]>(data: string): T {
|
|
7
7
|
const result = instantiate<T>();
|
|
8
8
|
let lastPos = 1;
|
|
9
9
|
for (let i = 1; i < data.length - 1; i++) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isSpace } from "util/string";
|
|
2
|
-
import { unsafeCharCodeAt } from "../../
|
|
3
|
-
import { COMMA, BRACKET_RIGHT } from "../../
|
|
2
|
+
import { unsafeCharCodeAt } from "../../util";
|
|
3
|
+
import { COMMA, BRACKET_RIGHT } from "../../chars";
|
|
4
4
|
import { deserializeFloat } from "../float";
|
|
5
5
|
|
|
6
|
-
// @ts-ignore: Decorator
|
|
7
|
-
export function deserializeFloatArray<T extends number[]>(data: string): T {
|
|
6
|
+
// @ts-ignore: Decorator valid here
|
|
7
|
+
@inline export function deserializeFloatArray<T extends number[]>(data: string): T {
|
|
8
8
|
const result = instantiate<T>();
|
|
9
9
|
let lastPos = 0;
|
|
10
10
|
let i = 1;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isSpace } from "util/string";
|
|
2
|
-
import { unsafeCharCodeAt } from "../../
|
|
3
|
-
import { COMMA, BRACKET_RIGHT } from "../../
|
|
2
|
+
import { unsafeCharCodeAt } from "../../util";
|
|
3
|
+
import { COMMA, BRACKET_RIGHT } from "../../chars";
|
|
4
4
|
import { deserializeInteger } from "../integer";
|
|
5
5
|
|
|
6
|
-
// @ts-ignore: Decorator
|
|
7
|
-
export function deserializeIntegerArray<T extends number[]>(data: string): T {
|
|
6
|
+
// @ts-ignore: Decorator valid here
|
|
7
|
+
@inline export function deserializeIntegerArray<T extends number[]>(data: string): T {
|
|
8
8
|
const result = instantiate<T>();
|
|
9
9
|
let lastPos = 0;
|
|
10
10
|
let i = 1;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { BRACE_LEFT, BRACE_RIGHT } from "../../
|
|
1
|
+
import { BRACE_LEFT, BRACE_RIGHT } from "../../chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
|
-
import { unsafeCharCodeAt } from "../../
|
|
3
|
+
import { unsafeCharCodeAt } from "../../util";
|
|
4
4
|
|
|
5
|
-
// @ts-ignore: Decorator
|
|
6
|
-
export function deserializeMapArray<T extends unknown[]>(data: string): T {
|
|
5
|
+
// @ts-ignore: Decorator valid here
|
|
6
|
+
@inline export function deserializeMapArray<T extends unknown[]>(data: string): T {
|
|
7
7
|
const result = instantiate<T>();
|
|
8
8
|
let lastPos: u32 = 1;
|
|
9
9
|
let depth: u32 = 0;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { BRACE_LEFT, BRACE_RIGHT } from "../../
|
|
1
|
+
import { BRACE_LEFT, BRACE_RIGHT } from "../../chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
|
-
import { unsafeCharCodeAt } from "../../
|
|
3
|
+
import { unsafeCharCodeAt } from "../../util";
|
|
4
4
|
|
|
5
|
-
// @ts-ignore: Decorator
|
|
6
|
-
export function deserializeObjectArray<T extends unknown[]>(data: string): T {
|
|
5
|
+
// @ts-ignore: Decorator valid here
|
|
6
|
+
@inline export function deserializeObjectArray<T extends unknown[]>(data: string): T {
|
|
7
7
|
const result = instantiate<T>();
|
|
8
8
|
let lastPos: u32 = 1;
|
|
9
9
|
let depth: u32 = 0;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { BACK_SLASH, QUOTE } from "../../
|
|
2
|
-
import { unsafeCharCodeAt } from "../../
|
|
1
|
+
import { BACK_SLASH, QUOTE } from "../../chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../util";
|
|
3
3
|
import { deserializeString } from "../string";
|
|
4
4
|
|
|
5
|
-
// @ts-ignore: Decorator
|
|
6
|
-
export function deserializeStringArray(data: string): string[] {
|
|
5
|
+
// @ts-ignore: Decorator valid here
|
|
6
|
+
@inline export function deserializeStringArray(data: string): string[] {
|
|
7
7
|
const result: string[] = [];
|
|
8
8
|
let lastPos = 0;
|
|
9
9
|
let instr = false;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isMap } from "../
|
|
1
|
+
import { isMap } from "../util";
|
|
2
2
|
import { deserializeArrayArray } from "./array/array";
|
|
3
3
|
import { deserializeBooleanArray } from "./array/bool";
|
|
4
4
|
import { deserializeFloatArray } from "./array/float";
|
|
@@ -7,7 +7,7 @@ import { deserializeMapArray } from "./array/map";
|
|
|
7
7
|
import { deserializeObjectArray } from "./array/object";
|
|
8
8
|
import { deserializeStringArray } from "./array/string";
|
|
9
9
|
|
|
10
|
-
// @ts-ignore: Decorator
|
|
10
|
+
// @ts-ignore: Decorator valid here
|
|
11
11
|
export function deserializeArray<T extends unknown[]>(data: string): T {
|
|
12
12
|
if (isString<valueof<T>>()) {
|
|
13
13
|
return <T>deserializeStringArray(data);
|
|
@@ -31,7 +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
|
+
ERROR("Could not parse array of type " + nameof<T>() + "! Make sure to add the @json decorator over classes!");
|
|
35
|
+
} else {
|
|
36
|
+
ERROR("Could not parse array of type " + nameof<T>() + "!");
|
|
34
37
|
}
|
|
35
|
-
|
|
36
|
-
throw new Error("Could not parse array of type " + nameof<T>() + "!");
|
|
37
38
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { CHAR_F, CHAR_T } from "../
|
|
2
|
-
import { unsafeCharCodeAt } from "../
|
|
1
|
+
import { CHAR_F, CHAR_T } from "../chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../util";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Deserialize a string to type boolean
|
|
6
6
|
* @param data data to parse
|
|
7
7
|
* @returns boolean
|
|
8
8
|
*/
|
|
9
|
-
// @ts-ignore: Decorator
|
|
10
|
-
export function deserializeBoolean(data: string, start: i32 = 0, end: i32 = 0): boolean {
|
|
9
|
+
// @ts-ignore: Decorator valid here
|
|
10
|
+
@inline export function deserializeBoolean(data: string, start: i32 = 0, end: i32 = 0): boolean {
|
|
11
11
|
if (!end) end = data.length;
|
|
12
12
|
const len = end - start;
|
|
13
13
|
const ptr = changetype<usize>(data) + <usize>(start << 1);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
export function deserializeDate(dateTimeString: string): Date {
|
|
1
|
+
// @ts-ignore: Decorator valid here
|
|
2
|
+
@inline export function deserializeDate(dateTimeString: string): Date {
|
|
3
3
|
// Use AssemblyScript's date parser
|
|
4
4
|
const d = Date.fromString(dateTimeString);
|
|
5
5
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { snip_fast } from "../
|
|
1
|
+
import { snip_fast } from "../util";
|
|
2
2
|
|
|
3
|
-
// @ts-ignore: Decorator
|
|
4
|
-
export function deserializeInteger<T>(data: string): T {
|
|
3
|
+
// @ts-ignore: Decorator valid here
|
|
4
|
+
@inline export function deserializeInteger<T>(data: string): T {
|
|
5
5
|
// @ts-ignore
|
|
6
6
|
return snip_fast<T>(data);
|
|
7
7
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Virtual } from "as-virtual/assembly";
|
|
2
|
-
import { containsCodePoint, unsafeCharCodeAt } from "../
|
|
2
|
+
import { containsCodePoint, unsafeCharCodeAt } from "../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 "../
|
|
21
|
+
} from "../chars";
|
|
22
22
|
import { deserializeBoolean } from "./bool";
|
|
23
23
|
import { JSON } from "..";
|
|
24
24
|
import { deserializeString } from "./string";
|
|
@@ -26,8 +26,8 @@ import { isSpace } from "util/string";
|
|
|
26
26
|
import { deserializeInteger } from "./integer";
|
|
27
27
|
import { deserializeFloat } from "./float";
|
|
28
28
|
|
|
29
|
-
// @ts-ignore: Decorator
|
|
30
|
-
export function deserializeMap<T extends Map>(data: string): T {
|
|
29
|
+
// @ts-ignore: Decorator valid here
|
|
30
|
+
@inline export function deserializeMap<T extends Map>(data: string): T {
|
|
31
31
|
|
|
32
32
|
const map: nonnull<T> = changetype<nonnull<T>>(
|
|
33
33
|
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { MpZ } from "@hypercubed/as-mpz";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Deserialize a string to type MpZ
|
|
5
|
+
* @param data data to parse
|
|
6
|
+
* @returns MpZ
|
|
7
|
+
*/
|
|
8
|
+
// @ts-ignore: Decorator valid here
|
|
9
|
+
@inline export function deserializeMpZ(data: string, start: i32 = 0, end: i32 = 0): MpZ {
|
|
10
|
+
if (!end) end = data.length;
|
|
11
|
+
return MpZ.from(data.slice(start, end));
|
|
12
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
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 "../
|
|
1
|
+
import { unsafeCharCodeAt } from "../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 "../chars";
|
|
3
3
|
import { isSpace } from "util/string";
|
|
4
4
|
|
|
5
|
-
// @ts-ignore: Decorator
|
|
6
|
-
export function deserializeObject<T>(data: string): T {
|
|
5
|
+
// @ts-ignore: Decorator valid here
|
|
6
|
+
@inline export function deserializeObject<T>(data: string): T {
|
|
7
7
|
const schema: nonnull<T> = changetype<nonnull<T>>(
|
|
8
8
|
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
9
9
|
);
|
|
@@ -13,12 +13,12 @@ import {
|
|
|
13
13
|
CHAR_T,
|
|
14
14
|
TAB,
|
|
15
15
|
CHAR_U
|
|
16
|
-
} from "../
|
|
17
|
-
import { Sink } from "../
|
|
18
|
-
import { unsafeCharCodeAt } from "../
|
|
16
|
+
} from "../chars";
|
|
17
|
+
import { Sink } from "../sink";
|
|
18
|
+
import { unsafeCharCodeAt } from "../util";
|
|
19
19
|
|
|
20
|
-
// @ts-ignore: Decorator
|
|
21
|
-
export function deserializeString(data: string, start: i32 = 0, end: i32 = 0): string {
|
|
20
|
+
// @ts-ignore: Decorator valid here
|
|
21
|
+
@inline export function deserializeString(data: string, start: i32 = 0, end: i32 = 0): string {
|
|
22
22
|
end = end || data.length - 1;
|
|
23
23
|
let result = Sink.withCapacity(end - start - 1);
|
|
24
24
|
let last = start + 1;
|
package/assembly/index.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
/// <reference path="./index.d.ts" />
|
|
2
|
-
|
|
3
|
-
import { Box } from "as-container/assembly";
|
|
4
|
-
|
|
5
2
|
import { serializeString } from "./serialize/string";
|
|
6
3
|
import { serializeBool } from "./serialize/bool";
|
|
7
|
-
import { serializeBox } from "./serialize/box";
|
|
8
4
|
import { serializeInteger } from "./serialize/integer";
|
|
9
5
|
import { serializeFloat } from "./serialize/float";
|
|
10
6
|
import { serializeObject } from "./serialize/object";
|
|
@@ -14,15 +10,17 @@ import { serializeMap } from "./serialize/map";
|
|
|
14
10
|
import { deserializeBoolean } from "./deserialize/bool";
|
|
15
11
|
import { deserializeArray } from "./deserialize/array";
|
|
16
12
|
import { deserializeFloat } from "./deserialize/float";
|
|
17
|
-
import { deserializeBox } from "./deserialize/box";
|
|
18
13
|
import { deserializeObject } from "./deserialize/object";
|
|
19
14
|
import { deserializeMap } from "./deserialize/map";
|
|
20
15
|
import { deserializeDate } from "./deserialize/date";
|
|
21
|
-
import { FALSE_WORD, NULL_WORD, TRUE_WORD } from "./
|
|
16
|
+
import { FALSE_WORD, NULL_WORD, TRUE_WORD } from "./chars";
|
|
22
17
|
import { deserializeInteger } from "./deserialize/integer";
|
|
23
18
|
import { deserializeString } from "./deserialize/string";
|
|
24
|
-
import { Sink } from "./
|
|
19
|
+
import { Sink } from "./sink";
|
|
25
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";
|
|
26
24
|
|
|
27
25
|
/**
|
|
28
26
|
* Offset of the 'storage' property in the JSON.Value class.
|
|
@@ -46,13 +44,15 @@ export namespace JSON {
|
|
|
46
44
|
F64,
|
|
47
45
|
Bool,
|
|
48
46
|
String,
|
|
47
|
+
ManagedString,
|
|
49
48
|
Struct,
|
|
50
|
-
|
|
49
|
+
ManagedStruct,
|
|
50
|
+
Array,
|
|
51
|
+
ManagedArray
|
|
51
52
|
}
|
|
53
|
+
|
|
52
54
|
export class Value {
|
|
53
55
|
public type: i32;
|
|
54
|
-
public length: i32 = 0;
|
|
55
|
-
|
|
56
56
|
// @ts-ignore: storage is set directly through memory
|
|
57
57
|
private storage: u64;
|
|
58
58
|
|
|
@@ -102,7 +102,6 @@ export namespace JSON {
|
|
|
102
102
|
store<T>(changetype<usize>(this), value, STORAGE);
|
|
103
103
|
} else if (isString<T>()) {
|
|
104
104
|
this.type = JSON.Types.String;
|
|
105
|
-
this.length = String.UTF8.byteLength(value as string);
|
|
106
105
|
store<T>(changetype<usize>(this), value, STORAGE);
|
|
107
106
|
} else if (value instanceof Map) {
|
|
108
107
|
if (idof<T>() !== idof<Map<string, JSON.Value>>()) {
|
|
@@ -117,8 +116,6 @@ export namespace JSON {
|
|
|
117
116
|
} else if (isArray<T>()) {
|
|
118
117
|
// @ts-ignore: T satisfies constraints of any[]
|
|
119
118
|
this.type = JSON.Types.Array + getArrayDepth<T>(0);
|
|
120
|
-
// @ts-ignore
|
|
121
|
-
this.length = value.length;
|
|
122
119
|
store<T>(changetype<usize>(this), value, STORAGE);
|
|
123
120
|
}
|
|
124
121
|
}
|
|
@@ -207,6 +204,14 @@ export namespace JSON {
|
|
|
207
204
|
}
|
|
208
205
|
}
|
|
209
206
|
}
|
|
207
|
+
|
|
208
|
+
export class Box<T> {
|
|
209
|
+
constructor(public value: T) {}
|
|
210
|
+
@inline static from<T>(value: T): Box<T> {
|
|
211
|
+
return new Box(value);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
210
215
|
/**
|
|
211
216
|
* Stringifies valid JSON data.
|
|
212
217
|
* ```js
|
|
@@ -231,10 +236,6 @@ export namespace JSON {
|
|
|
231
236
|
// @ts-ignore
|
|
232
237
|
} else if (isString<nonnull<T>>()) {
|
|
233
238
|
return serializeString(changetype<string>(data));
|
|
234
|
-
} else if (data instanceof Box) {
|
|
235
|
-
// @ts-ignore
|
|
236
|
-
return serializeBox(changetype<nonnull<T>>(data));
|
|
237
|
-
// @ts-ignore
|
|
238
239
|
} else if (isDefined(data.__SERIALIZE)) {
|
|
239
240
|
// @ts-ignore
|
|
240
241
|
return serializeObject(changetype<nonnull<T>>(data));
|
|
@@ -247,6 +248,8 @@ export namespace JSON {
|
|
|
247
248
|
} else if (data instanceof Map) {
|
|
248
249
|
// @ts-ignore
|
|
249
250
|
return serializeMap(changetype<nonnull<T>>(data));
|
|
251
|
+
} else if (data instanceof MpZ) {
|
|
252
|
+
return serializeMpZ(data);
|
|
250
253
|
} else {
|
|
251
254
|
throw new Error(
|
|
252
255
|
`Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
|
|
@@ -281,17 +284,16 @@ export namespace JSON {
|
|
|
281
284
|
return deserializeArray<nonnull<T>>(data);
|
|
282
285
|
}
|
|
283
286
|
let type: nonnull<T> = changetype<nonnull<T>>(0);
|
|
284
|
-
if (type
|
|
285
|
-
// @ts-ignore
|
|
286
|
-
return deserializeBox<nonnull<T>>(data);
|
|
287
|
-
// @ts-ignore
|
|
288
|
-
} else if (isDefined(type.__DESERIALIZE)) {
|
|
287
|
+
if (isDefined(type.__DESERIALIZE)) {
|
|
289
288
|
// @ts-ignore
|
|
290
289
|
return deserializeObject<nonnull<T>>(data.trimStart());
|
|
291
290
|
} else if (type instanceof Map) {
|
|
292
291
|
// @ts-ignore
|
|
293
292
|
return deserializeMap<nonnull<T>>(data.trimStart());
|
|
294
|
-
} else if (type instanceof
|
|
293
|
+
} else if (type instanceof MpZ) {
|
|
294
|
+
// @ts-ignore
|
|
295
|
+
return deserializeMpZ(data);
|
|
296
|
+
}else if (type instanceof Date) {
|
|
295
297
|
// @ts-ignore
|
|
296
298
|
return deserializeDate(data);
|
|
297
299
|
} else {
|
|
@@ -6,14 +6,15 @@ import {
|
|
|
6
6
|
BRACKET_LEFT_WORD,
|
|
7
7
|
BRACKET_RIGHT,
|
|
8
8
|
BRACKET_RIGHT_WORD
|
|
9
|
-
} from "../
|
|
10
|
-
import { Sink } from "../
|
|
9
|
+
} from "../chars";
|
|
10
|
+
import { Sink } from "../sink";
|
|
11
11
|
import { serializeString } from "./string";
|
|
12
12
|
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
export function serializeArray<T extends any[]>(data: T): string {
|
|
13
|
+
// @ts-ignore: Decorator valid here
|
|
14
|
+
@inline export function serializeArray<T extends any[]>(data: T): string {
|
|
15
|
+
if (changetype<usize>(data) == <usize>0) return EMPTY_BRACKET_WORD;
|
|
15
16
|
// @ts-ignore
|
|
16
|
-
if (data.length == 0) {
|
|
17
|
+
else if (data.length == 0) {
|
|
17
18
|
return EMPTY_BRACKET_WORD;
|
|
18
19
|
// @ts-ignore
|
|
19
20
|
} else if (isString<valueof<T>>()) {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @param data data to serialize
|
|
4
4
|
* @returns string
|
|
5
5
|
*/
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
export function serializeBool(data: bool): string {
|
|
6
|
+
// @ts-ignore: Decorator valid here
|
|
7
|
+
@inline export function serializeBool(data: bool): string {
|
|
8
8
|
return data ? "true" : "false";
|
|
9
9
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
export function serializeInteger<T extends number>(data: T): string {
|
|
1
|
+
// @ts-ignore: Decorator valid here
|
|
2
|
+
@inline export function serializeInteger<T extends number>(data: T): string {
|
|
3
3
|
// I have a much faster implementation of itoa that I will port over later. Its ~4x faster
|
|
4
4
|
return data.toString();
|
|
5
5
|
}
|
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import { COLON, COMMA, BRACE_LEFT_WORD, BRACE_RIGHT } from "../
|
|
1
|
+
import { COLON, COMMA, BRACE_LEFT_WORD, BRACE_RIGHT } from "../chars";
|
|
2
2
|
import { JSON } from "..";
|
|
3
|
-
import { Sink } from "../
|
|
4
|
-
import { serializeString } from "./string";
|
|
3
|
+
import { Sink } from "../sink";
|
|
5
4
|
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
export function serializeMap<T extends Map<any, any>>(data: T): string {
|
|
5
|
+
// @ts-ignore: Decorator valid here
|
|
6
|
+
@inline export function serializeMap<T extends Map<any, any>>(data: T): string {
|
|
7
|
+
if (changetype<usize>(data) == <usize>0) return "{}";
|
|
8
8
|
let result = Sink.fromString(BRACE_LEFT_WORD);
|
|
9
|
+
if (!data.size) return "{}";
|
|
9
10
|
let keys = data.keys();
|
|
10
11
|
let values = data.values();
|
|
11
12
|
const end = data.size - 1;
|
|
12
13
|
for (let i = 0; i < end; i++) {
|
|
13
|
-
result.write(
|
|
14
|
+
result.write(JSON.stringify(unchecked(keys[i]).toString()));
|
|
14
15
|
result.writeCodePoint(COLON);
|
|
15
16
|
result.write(JSON.stringify(unchecked(values[i])));
|
|
16
17
|
result.writeCodePoint(COMMA);
|
|
17
18
|
}
|
|
18
|
-
result.write(
|
|
19
|
+
result.write(JSON.stringify(unchecked(keys[end]).toString()));
|
|
19
20
|
result.writeCodePoint(COLON);
|
|
20
21
|
result.write(JSON.stringify(unchecked(values[end])));
|
|
21
22
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
interface GeneratedInterface {
|
|
2
2
|
__SERIALIZE(): string;
|
|
3
3
|
}
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
export function serializeObject<T extends GeneratedInterface>(data: T): string {
|
|
4
|
+
// @ts-ignore: Decoraor valid here
|
|
5
|
+
@inline export function serializeObject<T extends GeneratedInterface>(data: T): string {
|
|
6
6
|
return changetype<nonnull<T>>(data).__SERIALIZE();
|
|
7
7
|
}
|
|
@@ -7,12 +7,12 @@ import {
|
|
|
7
7
|
QUOTE,
|
|
8
8
|
QUOTE_WORD,
|
|
9
9
|
TAB
|
|
10
|
-
} from "../
|
|
11
|
-
import { Sink } from "../
|
|
12
|
-
import { unsafeCharCodeAt } from "../
|
|
10
|
+
} from "../chars";
|
|
11
|
+
import { Sink } from "../sink";
|
|
12
|
+
import { unsafeCharCodeAt } from "../util";
|
|
13
13
|
|
|
14
|
-
// @ts-ignore: Decorator
|
|
15
|
-
export function serializeString(data: string): string {
|
|
14
|
+
// @ts-ignore: Decorator valid here
|
|
15
|
+
@inline export function serializeString(data: string): string {
|
|
16
16
|
if (data.length === 0) {
|
|
17
17
|
return QUOTE_WORD + QUOTE_WORD;
|
|
18
18
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { JSON } from "..";
|
|
2
|
-
import { Sink } from "../
|
|
3
|
-
import { __atoi_fast } from "../
|
|
4
|
-
import { serializeUnknownArray } from "./array/unknown";
|
|
2
|
+
import { Sink } from "../sink";
|
|
3
|
+
import { __atoi_fast } from "../util";
|
|
5
4
|
import { serializeBool } from "./bool";
|
|
6
5
|
import { serializeFloat } from "./float";
|
|
7
6
|
import { serializeInteger } from "./integer";
|
|
@@ -13,7 +12,8 @@ import { serializeString } from "./string";
|
|
|
13
12
|
* @param data - The JSON.Value to be serialized.
|
|
14
13
|
* @returns The serialized result.
|
|
15
14
|
*/
|
|
16
|
-
|
|
15
|
+
// @ts-ignore: Decorator valid here
|
|
16
|
+
@inline export function serializeUnknown(data: JSON.Value): string {
|
|
17
17
|
const type = data.type;
|
|
18
18
|
switch (type) {
|
|
19
19
|
case JSON.Types.String: {
|
|
@@ -41,5 +41,5 @@ export function serializeUnknown(data: JSON.Value): string {
|
|
|
41
41
|
return serializeFloat(data.get<f64>());
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
-
return serializeUnknownArray(data.get<JSON.Value[]>());
|
|
44
|
+
return "ERROR"//serializeUnknownArray(data.get<JSON.Value[]>());
|
|
45
45
|
}
|