json-as 0.8.6 → 0.8.8
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 +2 -1
- package/assembly/__benches__/as-json.ts +1 -1
- package/assembly/deserialize/array/array.ts +31 -0
- package/assembly/deserialize/array/bool.ts +19 -0
- package/assembly/deserialize/array/float.ts +24 -0
- package/assembly/deserialize/array/integer.ts +24 -0
- package/assembly/deserialize/array/map.ts +27 -0
- package/assembly/deserialize/array/object.ts +27 -0
- package/assembly/deserialize/array/string.ts +29 -0
- package/assembly/deserialize/array.ts +37 -0
- package/assembly/deserialize/bool.ts +18 -0
- package/assembly/deserialize/box.ts +14 -0
- package/assembly/deserialize/date.ts +11 -0
- package/assembly/deserialize/float.ts +9 -0
- package/assembly/deserialize/integer.ts +7 -0
- package/assembly/deserialize/map.ts +182 -0
- package/assembly/deserialize/object.ts +139 -0
- package/assembly/deserialize/string.ts +88 -0
- package/assembly/serialize/array.ts +51 -0
- package/assembly/serialize/bool.ts +4 -0
- package/assembly/serialize/box.ts +10 -0
- package/assembly/serialize/date.ts +4 -0
- package/assembly/serialize/float.ts +4 -0
- package/assembly/serialize/integer.ts +5 -0
- package/assembly/serialize/map.ts +24 -0
- package/assembly/serialize/object.ts +7 -0
- package/assembly/serialize/string.ts +64 -0
- package/assembly/src/json.ts +55 -872
- package/assembly/src/sink.ts +286 -0
- package/assembly/src/util.ts +6 -0
- package/assembly/test.ts +8 -1
- package/package.json +1 -1
- package/transform/lib/index.js +3 -3
- package/transform/package.json +1 -1
- package/transform/src/index.ts +3 -3
package/CHANGELOG
CHANGED
|
@@ -2,4 +2,5 @@ v0.8.2 - Properties starting with `static` or `private` would be ignored
|
|
|
2
2
|
v0.8.3 - Dirty fix to issue #68. Add __JSON_Stringify callable to global scope.
|
|
3
3
|
v0.8.4 - Fix #71. Classes with the extending class overriding a property cause the property to be serialized twice.
|
|
4
4
|
v0.8.5 - Fix #73. Support for nullable primatives with Box<T> from as-container
|
|
5
|
-
v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have been v0.8.5
|
|
5
|
+
v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have been v0.8.5
|
|
6
|
+
v0.8.8 - Depreacte v0.8.7, roll 0.8.6 -> 0.8.8
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { leftBracketCode, rightBracketCode } from "../../src/chars";
|
|
2
|
+
import { JSON } from "../../src/json";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
6
|
+
@inline export function deserializeArrayArray<T extends unknown[][]>(data: string): T {
|
|
7
|
+
const result = instantiate<T>();
|
|
8
|
+
let lastPos = 0;
|
|
9
|
+
let depth = 0;
|
|
10
|
+
let i = 1;
|
|
11
|
+
// Find start of bracket
|
|
12
|
+
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
|
|
13
|
+
//i++;
|
|
14
|
+
for (; i < data.length - 1; i++) {
|
|
15
|
+
const char = unsafeCharCodeAt(data, i);
|
|
16
|
+
if (char === leftBracketCode) {
|
|
17
|
+
if (depth === 0) {
|
|
18
|
+
lastPos = i;
|
|
19
|
+
}
|
|
20
|
+
// Shifting is 6% faster than incrementing
|
|
21
|
+
depth++;
|
|
22
|
+
} else if (char === rightBracketCode) {
|
|
23
|
+
depth--;
|
|
24
|
+
if (depth === 0) {
|
|
25
|
+
i++;
|
|
26
|
+
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { eCode, fCode, tCode } from "../../src/chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
+
import { deserializeBoolean } from "../bool";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
6
|
+
@inline export function deserializeBooleanArray<T extends boolean[]>(data: string): T {
|
|
7
|
+
const result = instantiate<T>();
|
|
8
|
+
let lastPos = 1;
|
|
9
|
+
for (let i = 1; i < data.length - 1; i++) {
|
|
10
|
+
const char = unsafeCharCodeAt(data, i);
|
|
11
|
+
if (char === tCode || char === fCode) {
|
|
12
|
+
lastPos = i;
|
|
13
|
+
} else if (char === eCode) {
|
|
14
|
+
i++;
|
|
15
|
+
result.push(deserializeBoolean(data.slice(lastPos, i)));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isSpace } from "util/string";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
+
import { commaCode, rightBracketCode } from "../../src/chars";
|
|
4
|
+
import { deserializeFloat } from "../float";
|
|
5
|
+
|
|
6
|
+
// @ts-ignore: Decorator
|
|
7
|
+
@inline export function deserializeFloatArray<T extends number[]>(data: string): T {
|
|
8
|
+
const result = instantiate<T>();
|
|
9
|
+
let lastPos = 0;
|
|
10
|
+
let i = 1;
|
|
11
|
+
let awaitingParse = false;
|
|
12
|
+
for (; i < data.length; i++) {
|
|
13
|
+
const char = unsafeCharCodeAt(data, i);
|
|
14
|
+
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
|
|
15
|
+
awaitingParse = true;
|
|
16
|
+
lastPos = i;
|
|
17
|
+
} else if (awaitingParse && (isSpace(char) || char == commaCode || char == rightBracketCode) && lastPos > 0) {
|
|
18
|
+
awaitingParse = false;
|
|
19
|
+
result.push(deserializeFloat<valueof<T>>(data.slice(lastPos, i)));
|
|
20
|
+
lastPos = 0;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isSpace } from "util/string";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
+
import { commaCode, rightBracketCode } from "../../src/chars";
|
|
4
|
+
import { deserializeInteger } from "../integer";
|
|
5
|
+
|
|
6
|
+
// @ts-ignore: Decorator
|
|
7
|
+
@inline export function deserializeIntegerArray<T extends number[]>(data: string): T {
|
|
8
|
+
const result = instantiate<T>();
|
|
9
|
+
let lastPos = 0;
|
|
10
|
+
let i = 1;
|
|
11
|
+
let awaitingParse = false;
|
|
12
|
+
for (; i < data.length; i++) {
|
|
13
|
+
const char = unsafeCharCodeAt(data, i);
|
|
14
|
+
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
|
|
15
|
+
awaitingParse = true;
|
|
16
|
+
lastPos = i;
|
|
17
|
+
} else if (awaitingParse && (isSpace(char) || char == commaCode || char == rightBracketCode) && lastPos > 0) {
|
|
18
|
+
awaitingParse = false;
|
|
19
|
+
result.push(deserializeInteger<valueof<T>>(data.slice(lastPos, i)));
|
|
20
|
+
lastPos = 0;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { leftBraceCode, rightBraceCode } from "../../src/chars";
|
|
2
|
+
import { JSON } from "../../src/json";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
6
|
+
@inline export function deserializeMapArray<T extends unknown[]>(data: string): T {
|
|
7
|
+
const result = instantiate<T>();
|
|
8
|
+
let lastPos: u32 = 1;
|
|
9
|
+
let depth: u32 = 0;
|
|
10
|
+
for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
|
|
11
|
+
const char = unsafeCharCodeAt(data, pos);
|
|
12
|
+
if (char === leftBraceCode) {
|
|
13
|
+
if (depth === 0) {
|
|
14
|
+
lastPos = pos;
|
|
15
|
+
}
|
|
16
|
+
depth++;
|
|
17
|
+
} else if (char === rightBraceCode) {
|
|
18
|
+
depth--;
|
|
19
|
+
if (depth === 0) {
|
|
20
|
+
pos++;
|
|
21
|
+
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos)));
|
|
22
|
+
//lastPos = pos + 2;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { leftBraceCode, rightBraceCode } from "../../src/chars";
|
|
2
|
+
import { JSON } from "../../src/json";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
6
|
+
@inline export function deserializeObjectArray<T extends unknown[]>(data: string): T {
|
|
7
|
+
const result = instantiate<T>();
|
|
8
|
+
let lastPos: u32 = 1;
|
|
9
|
+
let depth: u32 = 0;
|
|
10
|
+
for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
|
|
11
|
+
const char = unsafeCharCodeAt(data, pos);
|
|
12
|
+
if (char === leftBraceCode) {
|
|
13
|
+
if (depth === 0) {
|
|
14
|
+
lastPos = pos;
|
|
15
|
+
}
|
|
16
|
+
depth++;
|
|
17
|
+
} else if (char === rightBraceCode) {
|
|
18
|
+
depth--;
|
|
19
|
+
if (depth === 0) {
|
|
20
|
+
pos++;
|
|
21
|
+
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos)));
|
|
22
|
+
//lastPos = pos + 2;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { backSlashCode, quoteCode } from "../../src/chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
+
import { deserializeString } from "../string";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
6
|
+
@inline export function deserializeStringArray(data: string): string[] {
|
|
7
|
+
const result: string[] = [];
|
|
8
|
+
let lastPos = 0;
|
|
9
|
+
let instr = false;
|
|
10
|
+
let escaping = false;
|
|
11
|
+
for (let i = 1; i < data.length - 1; i++) {
|
|
12
|
+
const char = unsafeCharCodeAt(data, i);
|
|
13
|
+
if (char === backSlashCode && !escaping) {
|
|
14
|
+
escaping = true;
|
|
15
|
+
} else {
|
|
16
|
+
if (char === quoteCode && !escaping) {
|
|
17
|
+
if (instr === false) {
|
|
18
|
+
instr = true;
|
|
19
|
+
lastPos = i;
|
|
20
|
+
} else {
|
|
21
|
+
instr = false;
|
|
22
|
+
result.push(deserializeString(data, lastPos, i));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
escaping = false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isMap } from "../src/util";
|
|
2
|
+
import { deserializeArrayArray } from "./array/array";
|
|
3
|
+
import { deserializeBooleanArray } from "./array/bool";
|
|
4
|
+
import { deserializeFloatArray } from "./array/float";
|
|
5
|
+
import { deserializeIntegerArray } from "./array/integer";
|
|
6
|
+
import { deserializeMapArray } from "./array/map";
|
|
7
|
+
import { deserializeObjectArray } from "./array/object";
|
|
8
|
+
import { deserializeStringArray } from "./array/string";
|
|
9
|
+
|
|
10
|
+
// @ts-ignore: Decorator
|
|
11
|
+
@inline export function deserializeArray<T extends unknown[]>(data: string): T {
|
|
12
|
+
if (isString<valueof<T>>()) {
|
|
13
|
+
return <T>deserializeStringArray(data);
|
|
14
|
+
} else if (isBoolean<valueof<T>>()) {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
return deserializeBooleanArray<T>(data);
|
|
17
|
+
} else if (isInteger<valueof<T>>()) {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
return deserializeIntegerArray<T>(data);
|
|
20
|
+
} else if (isFloat<valueof<T>>()) {
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
return deserializeFloatArray<T>(data);
|
|
23
|
+
} else if (isArrayLike<valueof<T>>()) {
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
return deserializeArrayArray<T>(data);
|
|
26
|
+
} else if (isMap<valueof<T>>()) {
|
|
27
|
+
return deserializeMapArray<T>(data);
|
|
28
|
+
} else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
|
|
29
|
+
const type = changetype<nonnull<valueof<T>>>(0);
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
if (isDefined(type.__JSON_Set_Key)) {
|
|
32
|
+
return deserializeObjectArray<T>(data);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
throw new Error("Could not parse array of type " + nameof<T>() + "!");
|
|
37
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { fCode, tCode } from "../src/chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../src/util";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Deserialize a string to type boolean
|
|
6
|
+
* @param data data to parse
|
|
7
|
+
* @returns boolean
|
|
8
|
+
*/
|
|
9
|
+
// @ts-ignore: Decorator
|
|
10
|
+
@inline export function deserializeBoolean(data: string, start: i32 = 0, end: i32 = 0): boolean {
|
|
11
|
+
if (!end) end = data.length;
|
|
12
|
+
const len = end - start;
|
|
13
|
+
const ptr = changetype<usize>(data) + <usize>(start << 1);
|
|
14
|
+
const firstChar = unsafeCharCodeAt(data, start);
|
|
15
|
+
if (len === 4 && firstChar === tCode && load<u64>(ptr) === 28429475166421108) return true;
|
|
16
|
+
else if (len === 5 && firstChar === fCode && load<u64>(ptr, 2) === 28429466576093281) return false;
|
|
17
|
+
return false//ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { JSON } from "../src/json";
|
|
2
|
+
|
|
3
|
+
// @ts-ignore: Decorator
|
|
4
|
+
@inline export function deserializeBox<T extends Box<any>>(data: string): T {
|
|
5
|
+
const instance = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))// as Box<usize>;
|
|
6
|
+
const val = instance._val;
|
|
7
|
+
instance._val = parseDirectInference(val, data);
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
return changetype<T>(instance);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@inline function parseDirectInference<T>(type: T, data: string, initializeDefaultValues: boolean = false): T {
|
|
13
|
+
return JSON.parse<T>(data, initializeDefaultValues)
|
|
14
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
@inline export function deserializeDate(dateTimeString: string): Date {
|
|
3
|
+
// Use AssemblyScript's date parser
|
|
4
|
+
const d = Date.fromString(dateTimeString);
|
|
5
|
+
|
|
6
|
+
// Return a new object instead of the one that the parser returned.
|
|
7
|
+
// This may seem redundant, but addreses the issue when Date
|
|
8
|
+
// is globally aliased to wasi_Date (or some other superclass).
|
|
9
|
+
return new Date(d.getTime());
|
|
10
|
+
}
|
|
11
|
+
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { Virtual } from "as-virtual/assembly";
|
|
2
|
+
import { containsCodePoint, unsafeCharCodeAt } from "../src/util";
|
|
3
|
+
import {
|
|
4
|
+
aCode,
|
|
5
|
+
backSlashCode,
|
|
6
|
+
colonCode,
|
|
7
|
+
commaCode,
|
|
8
|
+
eCode,
|
|
9
|
+
fCode,
|
|
10
|
+
lCode,
|
|
11
|
+
leftBraceCode,
|
|
12
|
+
leftBracketCode,
|
|
13
|
+
nCode,
|
|
14
|
+
quoteCode,
|
|
15
|
+
rCode,
|
|
16
|
+
rightBraceCode,
|
|
17
|
+
rightBracketCode,
|
|
18
|
+
sCode,
|
|
19
|
+
tCode,
|
|
20
|
+
uCode
|
|
21
|
+
} from "../src/chars";
|
|
22
|
+
import { deserializeBoolean } from "./bool";
|
|
23
|
+
import { JSON } from "../src/json";
|
|
24
|
+
import { deserializeString } from "./string";
|
|
25
|
+
import { isSpace } from "util/string";
|
|
26
|
+
import { deserializeInteger } from "./integer";
|
|
27
|
+
import { deserializeFloat } from "./float";
|
|
28
|
+
|
|
29
|
+
// @ts-ignore: Decorator
|
|
30
|
+
@inline export function deserializeMap<T extends Map>(data: string): T {
|
|
31
|
+
|
|
32
|
+
const map: nonnull<T> = changetype<nonnull<T>>(
|
|
33
|
+
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const key = Virtual.createEmpty<string>();
|
|
37
|
+
let isKey = false;
|
|
38
|
+
let depth = 0;
|
|
39
|
+
let outerLoopIndex = 1;
|
|
40
|
+
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
41
|
+
const char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
42
|
+
if (char === leftBracketCode) {
|
|
43
|
+
for (
|
|
44
|
+
let arrayValueIndex = outerLoopIndex;
|
|
45
|
+
arrayValueIndex < data.length - 1;
|
|
46
|
+
arrayValueIndex++
|
|
47
|
+
) {
|
|
48
|
+
const char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
49
|
+
if (char === leftBracketCode) {
|
|
50
|
+
depth++;
|
|
51
|
+
} else if (char === rightBracketCode) {
|
|
52
|
+
depth--;
|
|
53
|
+
if (depth === 0) {
|
|
54
|
+
++arrayValueIndex;
|
|
55
|
+
map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, arrayValueIndex)));
|
|
56
|
+
outerLoopIndex = arrayValueIndex;
|
|
57
|
+
isKey = false;
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
} else if (char === leftBraceCode) {
|
|
63
|
+
for (
|
|
64
|
+
let objectValueIndex = outerLoopIndex;
|
|
65
|
+
objectValueIndex < data.length - 1;
|
|
66
|
+
objectValueIndex++
|
|
67
|
+
) {
|
|
68
|
+
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
69
|
+
if (char === leftBraceCode) {
|
|
70
|
+
depth++;
|
|
71
|
+
} else if (char === rightBraceCode) {
|
|
72
|
+
depth--;
|
|
73
|
+
if (depth === 0) {
|
|
74
|
+
++objectValueIndex;
|
|
75
|
+
map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, objectValueIndex)));
|
|
76
|
+
outerLoopIndex = objectValueIndex;
|
|
77
|
+
isKey = false;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} else if (char === quoteCode) {
|
|
83
|
+
let escaping = false;
|
|
84
|
+
for (
|
|
85
|
+
let stringValueIndex = ++outerLoopIndex;
|
|
86
|
+
stringValueIndex < data.length - 1;
|
|
87
|
+
stringValueIndex++
|
|
88
|
+
) {
|
|
89
|
+
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
90
|
+
if (char === backSlashCode && !escaping) {
|
|
91
|
+
escaping = true;
|
|
92
|
+
} else {
|
|
93
|
+
if (
|
|
94
|
+
char === quoteCode && !escaping
|
|
95
|
+
) {
|
|
96
|
+
if (isKey === false) {
|
|
97
|
+
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
|
|
98
|
+
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) {
|
|
99
|
+
key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex));
|
|
100
|
+
} else {
|
|
101
|
+
key.reinst(data, outerLoopIndex, stringValueIndex);
|
|
102
|
+
}
|
|
103
|
+
isKey = true;
|
|
104
|
+
} else {
|
|
105
|
+
if (isString<valueof<T>>()) {
|
|
106
|
+
const value = deserializeString(data, outerLoopIndex - 1, stringValueIndex);
|
|
107
|
+
map.set(deserializeMapKey<indexof<T>>(key), value);
|
|
108
|
+
}
|
|
109
|
+
isKey = false;
|
|
110
|
+
}
|
|
111
|
+
outerLoopIndex = ++stringValueIndex;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
escaping = false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} else if (
|
|
118
|
+
char == nCode &&
|
|
119
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === uCode &&
|
|
120
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode &&
|
|
121
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode) {
|
|
122
|
+
if (isNullable<valueof<T>>()) {
|
|
123
|
+
map.set(deserializeMapKey<indexof<T>>(key), null);
|
|
124
|
+
}
|
|
125
|
+
isKey = false;
|
|
126
|
+
} else if (
|
|
127
|
+
char === tCode &&
|
|
128
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === rCode &&
|
|
129
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === uCode &&
|
|
130
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
131
|
+
) {
|
|
132
|
+
if (isBoolean<valueof<T>>()) {
|
|
133
|
+
map.set(deserializeMapKey<indexof<T>>(key), true);
|
|
134
|
+
}
|
|
135
|
+
isKey = false;
|
|
136
|
+
} else if (
|
|
137
|
+
char === fCode &&
|
|
138
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === aCode &&
|
|
139
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode &&
|
|
140
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === sCode &&
|
|
141
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
142
|
+
) {
|
|
143
|
+
if (isBoolean<valueof<T>>()) {
|
|
144
|
+
map.set(deserializeMapKey<indexof<T>>(key), false);
|
|
145
|
+
}
|
|
146
|
+
isKey = false;
|
|
147
|
+
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
148
|
+
let numberValueIndex = ++outerLoopIndex;
|
|
149
|
+
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
150
|
+
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
151
|
+
if (char === colonCode || char === commaCode || char === rightBraceCode || isSpace(char)) {
|
|
152
|
+
if (isInteger<valueof<T>>()) {
|
|
153
|
+
map.set(deserializeMapKey<indexof<T>>(key), deserializeInteger<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
|
|
154
|
+
} else if (isFloat<valueof<T>>()) {
|
|
155
|
+
map.set(deserializeMapKey<indexof<T>>(key), deserializeFloat<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
|
|
156
|
+
}
|
|
157
|
+
outerLoopIndex = numberValueIndex;
|
|
158
|
+
isKey = false;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return map;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
//@ts-ignore: Decorator
|
|
169
|
+
@inline function deserializeMapKey<T>(key: Virtual<string>): T {
|
|
170
|
+
const k = key.copyOut();
|
|
171
|
+
if (isString<T>()) {
|
|
172
|
+
return k as T;
|
|
173
|
+
} else if (isBoolean<T>()) {
|
|
174
|
+
return deserializeBoolean(k) as T;
|
|
175
|
+
} else if (isInteger<T>()) {
|
|
176
|
+
return deserializeInteger<T>(k);
|
|
177
|
+
} else if (isFloat<T>()) {
|
|
178
|
+
return deserializeFloat<T>(k);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
|
|
182
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Virtual } from "as-virtual/assembly";
|
|
2
|
+
import { containsCodePoint, unsafeCharCodeAt } from "../src/util";
|
|
3
|
+
import { nullWord,trueWord,falseWord,aCode, backSlashCode, commaCode, eCode, fCode, lCode, leftBraceCode, leftBracketCode, nCode, quoteCode, rCode, rightBraceCode, rightBracketCode, sCode, tCode, uCode } from "../src/chars";
|
|
4
|
+
import { deserializeString } from "./string";
|
|
5
|
+
import { isSpace } from "util/string";
|
|
6
|
+
|
|
7
|
+
// @ts-ignore: Decorator
|
|
8
|
+
@inline export function deserializeObject<T>(data: string, initializeDefaultValues: boolean): T {
|
|
9
|
+
const schema: nonnull<T> = changetype<nonnull<T>>(
|
|
10
|
+
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
if (initializeDefaultValues) schema.__JSON_Initialize();
|
|
15
|
+
|
|
16
|
+
const key = Virtual.createEmpty<string>();
|
|
17
|
+
let isKey = false;
|
|
18
|
+
let depth = 0;
|
|
19
|
+
let outerLoopIndex = 1;
|
|
20
|
+
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
21
|
+
const char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
22
|
+
if (char === leftBracketCode) {
|
|
23
|
+
for (
|
|
24
|
+
let arrayValueIndex = outerLoopIndex;
|
|
25
|
+
arrayValueIndex < data.length - 1;
|
|
26
|
+
arrayValueIndex++
|
|
27
|
+
) {
|
|
28
|
+
const char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
29
|
+
if (char === leftBracketCode) {
|
|
30
|
+
depth++;
|
|
31
|
+
} else if (char === rightBracketCode) {
|
|
32
|
+
depth--;
|
|
33
|
+
if (depth === 0) {
|
|
34
|
+
++arrayValueIndex;
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
schema.__JSON_Set_Key(key, data, outerLoopIndex, arrayValueIndex, initializeDefaultValues);
|
|
37
|
+
outerLoopIndex = arrayValueIndex;
|
|
38
|
+
isKey = false;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} else if (char === leftBraceCode) {
|
|
44
|
+
for (
|
|
45
|
+
let objectValueIndex = outerLoopIndex;
|
|
46
|
+
objectValueIndex < data.length - 1;
|
|
47
|
+
objectValueIndex++
|
|
48
|
+
) {
|
|
49
|
+
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
50
|
+
if (char === leftBraceCode) {
|
|
51
|
+
depth++;
|
|
52
|
+
} else if (char === rightBraceCode) {
|
|
53
|
+
depth--;
|
|
54
|
+
if (depth === 0) {
|
|
55
|
+
++objectValueIndex;
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
schema.__JSON_Set_Key(key, data, outerLoopIndex, objectValueIndex, initializeDefaultValues);
|
|
58
|
+
outerLoopIndex = objectValueIndex;
|
|
59
|
+
isKey = false;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
} else if (char === quoteCode) {
|
|
65
|
+
let escaping = false;
|
|
66
|
+
for (
|
|
67
|
+
let stringValueIndex = ++outerLoopIndex;
|
|
68
|
+
stringValueIndex < data.length - 1;
|
|
69
|
+
stringValueIndex++
|
|
70
|
+
) {
|
|
71
|
+
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
72
|
+
if (char === backSlashCode && !escaping) {
|
|
73
|
+
escaping = true;
|
|
74
|
+
} else {
|
|
75
|
+
if (char === quoteCode && !escaping) {
|
|
76
|
+
if (isKey === false) {
|
|
77
|
+
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
|
|
78
|
+
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) {
|
|
79
|
+
key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex));
|
|
80
|
+
} else {
|
|
81
|
+
key.reinst(data, outerLoopIndex, stringValueIndex);
|
|
82
|
+
}
|
|
83
|
+
isKey = true;
|
|
84
|
+
} else {
|
|
85
|
+
const value = deserializeString(data, outerLoopIndex - 1, stringValueIndex);
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
schema.__JSON_Set_Key(key, value, 0, value.length, initializeDefaultValues);
|
|
88
|
+
isKey = false;
|
|
89
|
+
}
|
|
90
|
+
outerLoopIndex = ++stringValueIndex;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
escaping = false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} else if (
|
|
97
|
+
char == nCode &&
|
|
98
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === uCode &&
|
|
99
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode &&
|
|
100
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode
|
|
101
|
+
) {
|
|
102
|
+
// @ts-ignore
|
|
103
|
+
schema.__JSON_Set_Key(key, nullWord, 0, 4, initializeDefaultValues);
|
|
104
|
+
isKey = false;
|
|
105
|
+
} else if (
|
|
106
|
+
char === tCode &&
|
|
107
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === rCode &&
|
|
108
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === uCode &&
|
|
109
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
110
|
+
) {
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
schema.__JSON_Set_Key(key, trueWord, 0, 4, initializeDefaultValues);
|
|
113
|
+
isKey = false;
|
|
114
|
+
} else if (
|
|
115
|
+
char === fCode &&
|
|
116
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === aCode &&
|
|
117
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode &&
|
|
118
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === sCode &&
|
|
119
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
120
|
+
) {
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
schema.__JSON_Set_Key(key, falseWord, 0, 5, initializeDefaultValues);
|
|
123
|
+
isKey = false;
|
|
124
|
+
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
125
|
+
let numberValueIndex = ++outerLoopIndex;
|
|
126
|
+
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
127
|
+
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
128
|
+
if (char === commaCode || char === rightBraceCode || isSpace(char)) {
|
|
129
|
+
// @ts-ignore
|
|
130
|
+
schema.__JSON_Set_Key(key, data, outerLoopIndex - 1, numberValueIndex, initializeDefaultValues);
|
|
131
|
+
outerLoopIndex = numberValueIndex;
|
|
132
|
+
isKey = false;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return schema;
|
|
139
|
+
}
|