json-as 0.9.1 → 0.9.3
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/CHANGELOG +3 -1
- package/README.md +1 -1
- package/assembly/__tests__/deserialize.spec.ts +1 -1
- package/assembly/__tests__/serialize.spec.ts +21 -1
- package/assembly/deserialize/array/array.ts +4 -4
- package/assembly/deserialize/array/bool.ts +4 -4
- package/assembly/deserialize/array/float.ts +3 -3
- package/assembly/deserialize/array/integer.ts +3 -3
- 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 +1 -1
- package/assembly/deserialize/bool.ts +4 -4
- package/assembly/deserialize/box.ts +2 -2
- package/assembly/deserialize/date.ts +1 -1
- package/assembly/deserialize/float.ts +1 -1
- package/assembly/deserialize/integer.ts +1 -1
- package/assembly/deserialize/map.ts +43 -43
- package/assembly/deserialize/object.ts +25 -25
- package/assembly/deserialize/string.ts +33 -33
- package/assembly/index.ts +218 -36
- package/assembly/serialize/array.ts +16 -16
- package/assembly/serialize/bool.ts +6 -1
- package/assembly/serialize/box.ts +4 -3
- package/assembly/serialize/date.ts +1 -1
- package/assembly/serialize/float.ts +1 -1
- package/assembly/serialize/integer.ts +1 -1
- package/assembly/serialize/map.ts +7 -7
- package/assembly/serialize/object.ts +1 -1
- package/assembly/serialize/string.ts +19 -10
- package/assembly/src/chars.ts +37 -35
- package/assembly/src/sink.ts +17 -17
- package/assembly/src/util.ts +15 -15
- package/assembly/test.ts +2 -1
- package/package.json +3 -3
- package/transform/lib/index.js +31 -28
- package/transform/package.json +1 -1
- package/transform/src/index.ts +28 -24
package/CHANGELOG
CHANGED
|
@@ -4,6 +4,8 @@ v0.8.4 - Fix #71. Classes with the extending class overriding a property cause t
|
|
|
4
4
|
v0.8.5 - Fix #73. Support for nullable primatives with Box<T> from as-container
|
|
5
5
|
v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have been v0.8.5
|
|
6
6
|
|
|
7
|
-
v0.9.0 - Large update. Refactor all the code, nullable primitives, rewrite the transform, allow extensibility with @omit keywords, and fix a plethora of bugs
|
|
7
|
+
v0.9.0 - BREAKING CHANGE - API changed from JSON.parse(data, defaultValues) to JSON.parse(data). Default Values defaults to true. Large update. Refactor all the code, nullable primitives, rewrite the transform, allow extensibility with @omit keywords, and fix a plethora of bugs
|
|
8
8
|
v0.9.1 - Fix #71
|
|
9
|
+
v0.9.2 - Fix #75 + Build sizes significantly reduced
|
|
10
|
+
|
|
9
11
|
[UNRELEASED] v0.9.x - Port JSON.Value from the `develop` branch to allow for union types, parsing of arbitrary data, and whatever the hell you want.
|
package/README.md
CHANGED
|
@@ -48,6 +48,16 @@ class Player {
|
|
|
48
48
|
isVerified: boolean;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
@json
|
|
52
|
+
class OmitIf {
|
|
53
|
+
x: i32 = 1;
|
|
54
|
+
@omitif("this.y == -1")
|
|
55
|
+
y: i32 = -1;
|
|
56
|
+
z: i32 = 1;
|
|
57
|
+
@omitnull()
|
|
58
|
+
foo: string | null = null
|
|
59
|
+
}
|
|
60
|
+
|
|
51
61
|
class Nullable { }
|
|
52
62
|
type Null = Nullable | null;
|
|
53
63
|
|
|
@@ -297,7 +307,7 @@ describe("Should serialize object arrays", () => {
|
|
|
297
307
|
|
|
298
308
|
});
|
|
299
309
|
|
|
300
|
-
describe("
|
|
310
|
+
describe("Should serialize objects", () => {
|
|
301
311
|
|
|
302
312
|
expect(
|
|
303
313
|
JSON.stringify<Vec3>({
|
|
@@ -348,6 +358,16 @@ describe("Ser/de Objects", () => {
|
|
|
348
358
|
|
|
349
359
|
});
|
|
350
360
|
|
|
361
|
+
describe("Should serialize @omit'ed objects", () => {
|
|
362
|
+
|
|
363
|
+
expect(
|
|
364
|
+
JSON.stringify(<OmitIf>{
|
|
365
|
+
y: 1
|
|
366
|
+
})
|
|
367
|
+
).toBe('{"x":1,"y":1,"z":1}');
|
|
368
|
+
|
|
369
|
+
});
|
|
370
|
+
|
|
351
371
|
@json
|
|
352
372
|
class ObjWithString {
|
|
353
373
|
s!: string;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BRACKET_LEFT, BRACKET_RIGHT } from "../../src/chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
3
|
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator
|
|
6
|
-
|
|
6
|
+
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;
|
|
@@ -13,13 +13,13 @@ import { unsafeCharCodeAt } from "../../src/util";
|
|
|
13
13
|
//i++;
|
|
14
14
|
for (; i < data.length - 1; i++) {
|
|
15
15
|
const char = unsafeCharCodeAt(data, i);
|
|
16
|
-
if (char ===
|
|
16
|
+
if (char === BRACKET_LEFT) {
|
|
17
17
|
if (depth === 0) {
|
|
18
18
|
lastPos = i;
|
|
19
19
|
}
|
|
20
20
|
// Shifting is 6% faster than incrementing
|
|
21
21
|
depth++;
|
|
22
|
-
} else if (char ===
|
|
22
|
+
} else if (char === BRACKET_RIGHT) {
|
|
23
23
|
depth--;
|
|
24
24
|
if (depth === 0) {
|
|
25
25
|
i++;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CHAR_E, CHAR_F, CHAR_T } from "../../src/chars";
|
|
2
2
|
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
3
|
import { deserializeBoolean } from "../bool";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator
|
|
6
|
-
|
|
6
|
+
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++) {
|
|
10
10
|
const char = unsafeCharCodeAt(data, i);
|
|
11
|
-
if (char ===
|
|
11
|
+
if (char === CHAR_T || char === CHAR_F) {
|
|
12
12
|
lastPos = i;
|
|
13
|
-
} else if (char ===
|
|
13
|
+
} else if (char === CHAR_E) {
|
|
14
14
|
i++;
|
|
15
15
|
result.push(deserializeBoolean(data.slice(lastPos, i)));
|
|
16
16
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isSpace } from "util/string";
|
|
2
2
|
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
-
import {
|
|
3
|
+
import { COMMA, BRACKET_RIGHT } from "../../src/chars";
|
|
4
4
|
import { deserializeFloat } from "../float";
|
|
5
5
|
|
|
6
6
|
// @ts-ignore: Decorator
|
|
7
|
-
|
|
7
|
+
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;
|
|
@@ -14,7 +14,7 @@ import { deserializeFloat } from "../float";
|
|
|
14
14
|
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
|
|
15
15
|
awaitingParse = true;
|
|
16
16
|
lastPos = i;
|
|
17
|
-
} else if (awaitingParse && (isSpace(char) || char ==
|
|
17
|
+
} else if (awaitingParse && (isSpace(char) || char == COMMA || char == BRACKET_RIGHT) && lastPos > 0) {
|
|
18
18
|
awaitingParse = false;
|
|
19
19
|
result.push(deserializeFloat<valueof<T>>(data.slice(lastPos, i)));
|
|
20
20
|
lastPos = 0;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isSpace } from "util/string";
|
|
2
2
|
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
-
import {
|
|
3
|
+
import { COMMA, BRACKET_RIGHT } from "../../src/chars";
|
|
4
4
|
import { deserializeInteger } from "../integer";
|
|
5
5
|
|
|
6
6
|
// @ts-ignore: Decorator
|
|
7
|
-
|
|
7
|
+
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;
|
|
@@ -14,7 +14,7 @@ import { deserializeInteger } from "../integer";
|
|
|
14
14
|
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
|
|
15
15
|
awaitingParse = true;
|
|
16
16
|
lastPos = i;
|
|
17
|
-
} else if (awaitingParse && (isSpace(char) || char ==
|
|
17
|
+
} else if (awaitingParse && (isSpace(char) || char == COMMA || char == BRACKET_RIGHT) && lastPos > 0) {
|
|
18
18
|
awaitingParse = false;
|
|
19
19
|
result.push(deserializeInteger<valueof<T>>(data.slice(lastPos, i)));
|
|
20
20
|
lastPos = 0;
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BRACE_LEFT, BRACE_RIGHT } from "../../src/chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
3
|
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator
|
|
6
|
-
|
|
6
|
+
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;
|
|
10
10
|
for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
|
|
11
11
|
const char = unsafeCharCodeAt(data, pos);
|
|
12
|
-
if (char ===
|
|
12
|
+
if (char === BRACE_LEFT) {
|
|
13
13
|
if (depth === 0) {
|
|
14
14
|
lastPos = pos;
|
|
15
15
|
}
|
|
16
16
|
depth++;
|
|
17
|
-
} else if (char ===
|
|
17
|
+
} else if (char === BRACE_RIGHT) {
|
|
18
18
|
depth--;
|
|
19
19
|
if (depth === 0) {
|
|
20
20
|
pos++;
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BRACE_LEFT, BRACE_RIGHT } from "../../src/chars";
|
|
2
2
|
import { JSON } from "../..";
|
|
3
3
|
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator
|
|
6
|
-
|
|
6
|
+
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;
|
|
10
10
|
for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
|
|
11
11
|
const char = unsafeCharCodeAt(data, pos);
|
|
12
|
-
if (char ===
|
|
12
|
+
if (char === BRACE_LEFT) {
|
|
13
13
|
if (depth === 0) {
|
|
14
14
|
lastPos = pos;
|
|
15
15
|
}
|
|
16
16
|
depth++;
|
|
17
|
-
} else if (char ===
|
|
17
|
+
} else if (char === BRACE_RIGHT) {
|
|
18
18
|
depth--;
|
|
19
19
|
if (depth === 0) {
|
|
20
20
|
pos++;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BACK_SLASH, QUOTE } from "../../src/chars";
|
|
2
2
|
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
3
|
import { deserializeString } from "../string";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator
|
|
6
|
-
|
|
6
|
+
export function deserializeStringArray(data: string): string[] {
|
|
7
7
|
const result: string[] = [];
|
|
8
8
|
let lastPos = 0;
|
|
9
9
|
let instr = false;
|
|
10
10
|
let escaping = false;
|
|
11
11
|
for (let i = 1; i < data.length - 1; i++) {
|
|
12
12
|
const char = unsafeCharCodeAt(data, i);
|
|
13
|
-
if (char ===
|
|
13
|
+
if (char === BACK_SLASH && !escaping) {
|
|
14
14
|
escaping = true;
|
|
15
15
|
} else {
|
|
16
|
-
if (char ===
|
|
16
|
+
if (char === QUOTE && !escaping) {
|
|
17
17
|
if (instr === false) {
|
|
18
18
|
instr = true;
|
|
19
19
|
lastPos = i;
|
|
@@ -8,7 +8,7 @@ import { deserializeObjectArray } from "./array/object";
|
|
|
8
8
|
import { deserializeStringArray } from "./array/string";
|
|
9
9
|
|
|
10
10
|
// @ts-ignore: Decorator
|
|
11
|
-
|
|
11
|
+
export function deserializeArray<T extends unknown[]>(data: string): T {
|
|
12
12
|
if (isString<valueof<T>>()) {
|
|
13
13
|
return <T>deserializeStringArray(data);
|
|
14
14
|
} else if (isBoolean<valueof<T>>()) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CHAR_F, CHAR_T } from "../src/chars";
|
|
2
2
|
import { unsafeCharCodeAt } from "../src/util";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -7,12 +7,12 @@ import { unsafeCharCodeAt } from "../src/util";
|
|
|
7
7
|
* @returns boolean
|
|
8
8
|
*/
|
|
9
9
|
// @ts-ignore: Decorator
|
|
10
|
-
|
|
10
|
+
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);
|
|
14
14
|
const firstChar = unsafeCharCodeAt(data, start);
|
|
15
|
-
if (len === 4 && firstChar ===
|
|
16
|
-
else if (len === 5 && firstChar ===
|
|
15
|
+
if (len === 4 && firstChar === CHAR_T && load<u64>(ptr) === 28429475166421108) return true;
|
|
16
|
+
else if (len === 5 && firstChar === CHAR_F && load<u64>(ptr, 2) === 28429466576093281) return false;
|
|
17
17
|
return false//ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
|
|
18
18
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { JSON } from "..";
|
|
2
2
|
|
|
3
3
|
// @ts-ignore: Decorator
|
|
4
|
-
|
|
4
|
+
export function deserializeBox<T extends Box<any>>(data: string): T {
|
|
5
5
|
if (isNullable<T>() && data == "null") {
|
|
6
6
|
return null;
|
|
7
7
|
}
|
|
@@ -12,6 +12,6 @@ import { JSON } from "..";
|
|
|
12
12
|
return changetype<T>(instance);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
function parseDirectInference<T>(type: T, data: string, initializeDefaultValues: boolean = false): T {
|
|
16
16
|
return JSON.parse<T>(data, initializeDefaultValues)
|
|
17
17
|
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import { Virtual } from "as-virtual/assembly";
|
|
2
2
|
import { containsCodePoint, unsafeCharCodeAt } from "../src/util";
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
4
|
+
CHAR_A,
|
|
5
|
+
BACK_SLASH,
|
|
6
|
+
COLON,
|
|
7
|
+
COMMA,
|
|
8
|
+
CHAR_E,
|
|
9
|
+
CHAR_F,
|
|
10
|
+
CHAR_L,
|
|
11
|
+
BRACE_LEFT,
|
|
12
|
+
BRACKET_LEFT,
|
|
13
|
+
CHAR_N,
|
|
14
|
+
QUOTE,
|
|
15
|
+
CHAR_R,
|
|
16
|
+
BRACE_RIGHT,
|
|
17
|
+
BRACKET_RIGHT,
|
|
18
|
+
CHAR_S,
|
|
19
|
+
CHAR_T,
|
|
20
|
+
CHAR_U
|
|
21
21
|
} from "../src/chars";
|
|
22
22
|
import { deserializeBoolean } from "./bool";
|
|
23
23
|
import { JSON } from "..";
|
|
@@ -27,7 +27,7 @@ import { deserializeInteger } from "./integer";
|
|
|
27
27
|
import { deserializeFloat } from "./float";
|
|
28
28
|
|
|
29
29
|
// @ts-ignore: Decorator
|
|
30
|
-
|
|
30
|
+
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>>())
|
|
@@ -39,16 +39,16 @@ import { deserializeFloat } from "./float";
|
|
|
39
39
|
let outerLoopIndex = 1;
|
|
40
40
|
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
41
41
|
const char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
42
|
-
if (char ===
|
|
42
|
+
if (char === BRACKET_LEFT) {
|
|
43
43
|
for (
|
|
44
44
|
let arrayValueIndex = outerLoopIndex;
|
|
45
45
|
arrayValueIndex < data.length - 1;
|
|
46
46
|
arrayValueIndex++
|
|
47
47
|
) {
|
|
48
48
|
const char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
49
|
-
if (char ===
|
|
49
|
+
if (char === BRACKET_LEFT) {
|
|
50
50
|
depth++;
|
|
51
|
-
} else if (char ===
|
|
51
|
+
} else if (char === BRACKET_RIGHT) {
|
|
52
52
|
depth--;
|
|
53
53
|
if (depth === 0) {
|
|
54
54
|
++arrayValueIndex;
|
|
@@ -59,16 +59,16 @@ import { deserializeFloat } from "./float";
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
-
} else if (char ===
|
|
62
|
+
} else if (char === BRACE_LEFT) {
|
|
63
63
|
for (
|
|
64
64
|
let objectValueIndex = outerLoopIndex;
|
|
65
65
|
objectValueIndex < data.length - 1;
|
|
66
66
|
objectValueIndex++
|
|
67
67
|
) {
|
|
68
68
|
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
69
|
-
if (char ===
|
|
69
|
+
if (char === BRACE_LEFT) {
|
|
70
70
|
depth++;
|
|
71
|
-
} else if (char ===
|
|
71
|
+
} else if (char === BRACE_RIGHT) {
|
|
72
72
|
depth--;
|
|
73
73
|
if (depth === 0) {
|
|
74
74
|
++objectValueIndex;
|
|
@@ -79,7 +79,7 @@ import { deserializeFloat } from "./float";
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
-
} else if (char ===
|
|
82
|
+
} else if (char === QUOTE) {
|
|
83
83
|
let escaping = false;
|
|
84
84
|
for (
|
|
85
85
|
let stringValueIndex = ++outerLoopIndex;
|
|
@@ -87,15 +87,15 @@ import { deserializeFloat } from "./float";
|
|
|
87
87
|
stringValueIndex++
|
|
88
88
|
) {
|
|
89
89
|
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
90
|
-
if (char ===
|
|
90
|
+
if (char === BACK_SLASH && !escaping) {
|
|
91
91
|
escaping = true;
|
|
92
92
|
} else {
|
|
93
93
|
if (
|
|
94
|
-
char ===
|
|
94
|
+
char === QUOTE && !escaping
|
|
95
95
|
) {
|
|
96
96
|
if (isKey === false) {
|
|
97
97
|
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
|
|
98
|
-
if (containsCodePoint(data,
|
|
98
|
+
if (containsCodePoint(data, BACK_SLASH, outerLoopIndex, stringValueIndex)) {
|
|
99
99
|
key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex));
|
|
100
100
|
} else {
|
|
101
101
|
key.reinst(data, outerLoopIndex, stringValueIndex);
|
|
@@ -115,30 +115,30 @@ import { deserializeFloat } from "./float";
|
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
} else if (
|
|
118
|
-
char ==
|
|
119
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
120
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
121
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
118
|
+
char == CHAR_N &&
|
|
119
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U &&
|
|
120
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L &&
|
|
121
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L) {
|
|
122
122
|
if (isNullable<valueof<T>>()) {
|
|
123
123
|
map.set(deserializeMapKey<indexof<T>>(key), null);
|
|
124
124
|
}
|
|
125
125
|
isKey = false;
|
|
126
126
|
} else if (
|
|
127
|
-
char ===
|
|
128
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
129
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
130
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
127
|
+
char === CHAR_T &&
|
|
128
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_R &&
|
|
129
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U &&
|
|
130
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E
|
|
131
131
|
) {
|
|
132
132
|
if (isBoolean<valueof<T>>()) {
|
|
133
133
|
map.set(deserializeMapKey<indexof<T>>(key), true);
|
|
134
134
|
}
|
|
135
135
|
isKey = false;
|
|
136
136
|
} else if (
|
|
137
|
-
char ===
|
|
138
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
139
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
140
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
141
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) ===
|
|
137
|
+
char === CHAR_F &&
|
|
138
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_A &&
|
|
139
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L &&
|
|
140
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_S &&
|
|
141
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E
|
|
142
142
|
) {
|
|
143
143
|
if (isBoolean<valueof<T>>()) {
|
|
144
144
|
map.set(deserializeMapKey<indexof<T>>(key), false);
|
|
@@ -148,7 +148,7 @@ import { deserializeFloat } from "./float";
|
|
|
148
148
|
let numberValueIndex = ++outerLoopIndex;
|
|
149
149
|
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
150
150
|
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
151
|
-
if (char ===
|
|
151
|
+
if (char === COLON || char === COMMA || char === BRACE_RIGHT || isSpace(char)) {
|
|
152
152
|
if (isInteger<valueof<T>>()) {
|
|
153
153
|
map.set(deserializeMapKey<indexof<T>>(key), deserializeInteger<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
|
|
154
154
|
} else if (isFloat<valueof<T>>()) {
|
|
@@ -166,7 +166,7 @@ import { deserializeFloat } from "./float";
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
//@ts-ignore: Decorator
|
|
169
|
-
|
|
169
|
+
function deserializeMapKey<T>(key: Virtual<string>): T {
|
|
170
170
|
const k = key.copyOut();
|
|
171
171
|
if (isString<T>()) {
|
|
172
172
|
return k as T;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { containsCodePoint, unsafeCharCodeAt } from "../src/util";
|
|
2
|
-
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 "../src/chars";
|
|
3
3
|
import { isSpace } from "util/string";
|
|
4
4
|
|
|
5
5
|
// @ts-ignore: Decorator
|
|
6
|
-
|
|
6
|
+
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
|
);
|
|
@@ -18,16 +18,16 @@ import { isSpace } from "util/string";
|
|
|
18
18
|
let outerLoopIndex = 1;
|
|
19
19
|
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
20
20
|
const char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
21
|
-
if (char ===
|
|
21
|
+
if (char === BRACKET_LEFT) {
|
|
22
22
|
for (
|
|
23
23
|
let arrayValueIndex = outerLoopIndex;
|
|
24
24
|
arrayValueIndex < data.length - 1;
|
|
25
25
|
arrayValueIndex++
|
|
26
26
|
) {
|
|
27
27
|
const char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
28
|
-
if (char ===
|
|
28
|
+
if (char === BRACKET_LEFT) {
|
|
29
29
|
depth++;
|
|
30
|
-
} else if (char ===
|
|
30
|
+
} else if (char === BRACKET_RIGHT) {
|
|
31
31
|
depth--;
|
|
32
32
|
if (depth === 0) {
|
|
33
33
|
++arrayValueIndex;
|
|
@@ -39,16 +39,16 @@ import { isSpace } from "util/string";
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
} else if (char ===
|
|
42
|
+
} else if (char === BRACE_LEFT) {
|
|
43
43
|
for (
|
|
44
44
|
let objectValueIndex = outerLoopIndex;
|
|
45
45
|
objectValueIndex < data.length - 1;
|
|
46
46
|
objectValueIndex++
|
|
47
47
|
) {
|
|
48
48
|
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
49
|
-
if (char ===
|
|
49
|
+
if (char === BRACE_LEFT) {
|
|
50
50
|
depth++;
|
|
51
|
-
} else if (char ===
|
|
51
|
+
} else if (char === BRACE_RIGHT) {
|
|
52
52
|
depth--;
|
|
53
53
|
if (depth === 0) {
|
|
54
54
|
++objectValueIndex;
|
|
@@ -60,7 +60,7 @@ import { isSpace } from "util/string";
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
} else if (char ===
|
|
63
|
+
} else if (char === QUOTE) {
|
|
64
64
|
let escaping = false;
|
|
65
65
|
for (
|
|
66
66
|
let stringValueIndex = ++outerLoopIndex;
|
|
@@ -68,10 +68,10 @@ import { isSpace } from "util/string";
|
|
|
68
68
|
stringValueIndex++
|
|
69
69
|
) {
|
|
70
70
|
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
71
|
-
if (char ===
|
|
71
|
+
if (char === BACK_SLASH && !escaping) {
|
|
72
72
|
escaping = true;
|
|
73
73
|
} else {
|
|
74
|
-
if (char ===
|
|
74
|
+
if (char === QUOTE && !escaping) {
|
|
75
75
|
if (isKey === false) {
|
|
76
76
|
key_start = outerLoopIndex;
|
|
77
77
|
key_end = stringValueIndex;
|
|
@@ -88,31 +88,31 @@ import { isSpace } from "util/string";
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
} else if (
|
|
91
|
-
char ==
|
|
92
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) ===
|
|
93
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) ===
|
|
94
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) ===
|
|
91
|
+
char == CHAR_N &&
|
|
92
|
+
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_U &&
|
|
93
|
+
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
|
|
94
|
+
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_L
|
|
95
95
|
) {
|
|
96
96
|
// @ts-ignore
|
|
97
97
|
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
|
|
98
98
|
outerLoopIndex += 3;
|
|
99
99
|
isKey = false;
|
|
100
100
|
} else if (
|
|
101
|
-
char ===
|
|
102
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) ===
|
|
103
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) ===
|
|
104
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) ===
|
|
101
|
+
char === CHAR_T &&
|
|
102
|
+
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_R &&
|
|
103
|
+
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_U &&
|
|
104
|
+
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_E
|
|
105
105
|
) {
|
|
106
106
|
// @ts-ignore
|
|
107
107
|
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
|
|
108
108
|
outerLoopIndex += 3;
|
|
109
109
|
isKey = false;
|
|
110
110
|
} else if (
|
|
111
|
-
char ===
|
|
112
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) ===
|
|
113
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) ===
|
|
114
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) ===
|
|
115
|
-
unsafeCharCodeAt(data, outerLoopIndex + 4) ===
|
|
111
|
+
char === CHAR_F &&
|
|
112
|
+
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_A &&
|
|
113
|
+
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
|
|
114
|
+
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_S &&
|
|
115
|
+
unsafeCharCodeAt(data, outerLoopIndex + 4) === CHAR_E
|
|
116
116
|
) {
|
|
117
117
|
// @ts-ignore
|
|
118
118
|
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 5);
|
|
@@ -122,7 +122,7 @@ import { isSpace } from "util/string";
|
|
|
122
122
|
let numberValueIndex = ++outerLoopIndex;
|
|
123
123
|
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
124
124
|
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
125
|
-
if (char ===
|
|
125
|
+
if (char === COMMA || char === BRACE_RIGHT || isSpace(char)) {
|
|
126
126
|
// @ts-ignore
|
|
127
127
|
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, numberValueIndex);
|
|
128
128
|
outerLoopIndex = numberValueIndex;
|