json-as 0.8.7 → 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/.github/workflows/nodejs.yml +1 -7
- package/CHANGELOG +1 -3
- package/README.md +16 -26
- package/assembly/__benches__/as-json.ts +88 -0
- package/assembly/__benches__/as-tral.d.ts +1 -0
- package/assembly/__tests__/as-json.spec.ts +673 -0
- package/assembly/__tests__/as-pect.d.ts +1 -0
- package/assembly/deserialize/array/array.ts +1 -1
- package/assembly/deserialize/array/map.ts +1 -1
- package/assembly/deserialize/array/object.ts +1 -1
- package/assembly/deserialize/array.ts +1 -1
- package/assembly/deserialize/box.ts +1 -4
- package/assembly/deserialize/map.ts +1 -1
- package/assembly/deserialize/object.ts +125 -122
- package/assembly/index.d.ts +1 -7
- package/assembly/index.ts +1 -129
- package/assembly/serialize/array.ts +2 -3
- package/assembly/serialize/box.ts +1 -1
- package/assembly/serialize/map.ts +1 -1
- package/assembly/src/json.ts +124 -0
- package/assembly/test.ts +22 -33
- package/bench/benchmark.ts +3 -7
- package/bench.js +3 -14
- package/index.ts +1 -1
- package/package.json +8 -6
- package/transform/lib/index.js +183 -301
- package/transform/package.json +1 -1
- package/transform/src/index.ts +215 -301
- package/transform/tsconfig.json +2 -2
- package/tsconfig.json +102 -94
- package/assembly/__tests__/deserialize.spec.ts +0 -298
- package/assembly/__tests__/serialize.spec.ts +0 -375
- package/transform/lib/index.old.js +0 -257
- package/transform/lib/types.js +0 -17
- package/transform/src/index.old.ts +0 -312
|
@@ -28,7 +28,7 @@ import { deserializeStringArray } from "./array/string";
|
|
|
28
28
|
} else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
|
|
29
29
|
const type = changetype<nonnull<valueof<T>>>(0);
|
|
30
30
|
// @ts-ignore
|
|
31
|
-
if (isDefined(type.
|
|
31
|
+
if (isDefined(type.__JSON_Set_Key)) {
|
|
32
32
|
return deserializeObjectArray<T>(data);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import { JSON } from "
|
|
1
|
+
import { JSON } from "../src/json";
|
|
2
2
|
|
|
3
3
|
// @ts-ignore: Decorator
|
|
4
4
|
@inline export function deserializeBox<T extends Box<any>>(data: string): T {
|
|
5
|
-
if (isNullable<T>() && data == "null") {
|
|
6
|
-
return null;
|
|
7
|
-
}
|
|
8
5
|
const instance = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))// as Box<usize>;
|
|
9
6
|
const val = instance._val;
|
|
10
7
|
instance._val = parseDirectInference(val, data);
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
uCode
|
|
21
21
|
} from "../src/chars";
|
|
22
22
|
import { deserializeBoolean } from "./bool";
|
|
23
|
-
import { JSON } from "
|
|
23
|
+
import { JSON } from "../src/json";
|
|
24
24
|
import { deserializeString } from "./string";
|
|
25
25
|
import { isSpace } from "util/string";
|
|
26
26
|
import { deserializeInteger } from "./integer";
|
|
@@ -1,136 +1,139 @@
|
|
|
1
|
+
import { Virtual } from "as-virtual/assembly";
|
|
1
2
|
import { containsCodePoint, unsafeCharCodeAt } from "../src/util";
|
|
2
|
-
import { aCode, backSlashCode, commaCode, eCode, fCode, lCode, leftBraceCode, leftBracketCode, nCode, quoteCode, rCode, rightBraceCode, rightBracketCode, sCode, tCode, uCode } from "../src/chars";
|
|
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";
|
|
3
5
|
import { isSpace } from "util/string";
|
|
4
6
|
|
|
5
7
|
// @ts-ignore: Decorator
|
|
6
|
-
@inline export function deserializeObject<T>(data: string): T {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
} else if (char === leftBraceCode) {
|
|
43
|
-
for (
|
|
44
|
-
let objectValueIndex = outerLoopIndex;
|
|
45
|
-
objectValueIndex < data.length - 1;
|
|
46
|
-
objectValueIndex++
|
|
47
|
-
) {
|
|
48
|
-
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
49
|
-
if (char === leftBraceCode) {
|
|
50
|
-
depth++;
|
|
51
|
-
} else if (char === rightBraceCode) {
|
|
52
|
-
depth--;
|
|
53
|
-
if (depth === 0) {
|
|
54
|
-
++objectValueIndex;
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, objectValueIndex);
|
|
57
|
-
outerLoopIndex = objectValueIndex;
|
|
58
|
-
isKey = false;
|
|
59
|
-
break;
|
|
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
|
+
}
|
|
60
41
|
}
|
|
61
42
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (isKey === false) {
|
|
76
|
-
key_start = outerLoopIndex;
|
|
77
|
-
key_end = stringValueIndex;
|
|
78
|
-
isKey = true;
|
|
79
|
-
} else {
|
|
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;
|
|
80
56
|
// @ts-ignore
|
|
81
|
-
schema.
|
|
57
|
+
schema.__JSON_Set_Key(key, data, outerLoopIndex, objectValueIndex, initializeDefaultValues);
|
|
58
|
+
outerLoopIndex = objectValueIndex;
|
|
82
59
|
isKey = false;
|
|
60
|
+
break;
|
|
83
61
|
}
|
|
84
|
-
outerLoopIndex = ++stringValueIndex;
|
|
85
|
-
break;
|
|
86
62
|
}
|
|
87
|
-
escaping = false;
|
|
88
63
|
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
+
}
|
|
131
135
|
}
|
|
132
136
|
}
|
|
133
137
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
138
|
+
return schema;
|
|
139
|
+
}
|
package/assembly/index.d.ts
CHANGED
|
@@ -13,16 +13,10 @@ declare function serializable(target: any): void;
|
|
|
13
13
|
*/
|
|
14
14
|
declare function alias(name: string): Function;
|
|
15
15
|
|
|
16
|
-
/**
|
|
17
|
-
* Property decorator that allows omits a field, making it be ignored.
|
|
18
|
-
*/
|
|
19
|
-
declare function omit(): Function;
|
|
20
|
-
|
|
21
|
-
|
|
22
16
|
/**
|
|
23
17
|
* Property decorator that allows a field to be omitted when equal to an Expression.
|
|
24
18
|
*/
|
|
25
|
-
declare function
|
|
19
|
+
declare function omitwhen(condition: string): Function;
|
|
26
20
|
|
|
27
21
|
/**
|
|
28
22
|
* Property decorator that allows a field to be omitted when a property is null.
|
package/assembly/index.ts
CHANGED
|
@@ -1,131 +1,3 @@
|
|
|
1
1
|
/// <reference path="./index.d.ts" />
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { serializeString } from "./serialize/string";
|
|
6
|
-
import { serializeBool } from "./serialize/bool";
|
|
7
|
-
import { serializeBox } from "./serialize/box";
|
|
8
|
-
import { serializeInteger } from "./serialize/integer";
|
|
9
|
-
import { serializeFloat } from "./serialize/float";
|
|
10
|
-
import { serializeObject } from "./serialize/object";
|
|
11
|
-
import { serializeDate } from "./serialize/date";
|
|
12
|
-
import { serializeArray } from "./serialize/array";
|
|
13
|
-
import { serializeMap } from "./serialize/map";
|
|
14
|
-
import { deserializeBoolean } from "./deserialize/bool";
|
|
15
|
-
import { deserializeArray } from "./deserialize/array";
|
|
16
|
-
import { deserializeFloat } from "./deserialize/float";
|
|
17
|
-
import { deserializeBox } from "./deserialize/box";
|
|
18
|
-
import { deserializeObject } from "./deserialize/object";
|
|
19
|
-
import { deserializeMap } from "./deserialize/map";
|
|
20
|
-
import { deserializeDate } from "./deserialize/date";
|
|
21
|
-
import { nullWord } from "./src/chars";
|
|
22
|
-
import { deserializeInteger } from "./deserialize/integer";
|
|
23
|
-
import { deserializeString } from "./deserialize/string";
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* JSON Encoder/Decoder for AssemblyScript
|
|
27
|
-
*/
|
|
28
|
-
export namespace JSON {
|
|
29
|
-
/**
|
|
30
|
-
* Stringifies valid JSON data.
|
|
31
|
-
* ```js
|
|
32
|
-
* JSON.stringify<T>(data)
|
|
33
|
-
* ```
|
|
34
|
-
* @param data T
|
|
35
|
-
* @returns string
|
|
36
|
-
*/
|
|
37
|
-
// @ts-ignore: Decorator
|
|
38
|
-
@inline export function stringify<T>(data: T): string {
|
|
39
|
-
if (isNullable<T>() && changetype<usize>(data) == <usize>0) {
|
|
40
|
-
return nullWord;
|
|
41
|
-
// @ts-ignore
|
|
42
|
-
} else if (isString<T>()) {
|
|
43
|
-
return serializeString(data as string);
|
|
44
|
-
} else if (isBoolean<T>()) {
|
|
45
|
-
return serializeBool(data as bool);
|
|
46
|
-
} else if (data instanceof Box) {
|
|
47
|
-
return serializeBox(data);
|
|
48
|
-
} else if (isInteger<T>()) {
|
|
49
|
-
// @ts-ignore
|
|
50
|
-
return serializeInteger<T>(data);
|
|
51
|
-
} else if (isFloat<T>(data)) {
|
|
52
|
-
// @ts-ignore
|
|
53
|
-
return serializeFloat<T>(data);
|
|
54
|
-
// @ts-ignore: Function is generated by transform
|
|
55
|
-
} else if (isDefined(data.__SERIALIZE)) {
|
|
56
|
-
// @ts-ignore
|
|
57
|
-
return serializeObject(data);
|
|
58
|
-
} else if (data instanceof Date) {
|
|
59
|
-
return serializeDate(data);
|
|
60
|
-
} else if (data instanceof Array) {
|
|
61
|
-
return serializeArray(data);
|
|
62
|
-
} else if (data instanceof Map) {
|
|
63
|
-
return serializeMap(data);
|
|
64
|
-
} else {
|
|
65
|
-
throw new Error(
|
|
66
|
-
`Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Parses valid JSON strings into their original format.
|
|
72
|
-
* ```js
|
|
73
|
-
* JSON.parse<T>(data)
|
|
74
|
-
* ```
|
|
75
|
-
* @param data string
|
|
76
|
-
* @returns T
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
// @ts-ignore: Decorator
|
|
80
|
-
@inline export function parse<T>(data: string): T {
|
|
81
|
-
if (isString<T>()) {
|
|
82
|
-
// @ts-ignore
|
|
83
|
-
return deserializeString(data);
|
|
84
|
-
} else if (isBoolean<T>()) {
|
|
85
|
-
return deserializeBoolean(data) as T;
|
|
86
|
-
} else if (isInteger<T>()) {
|
|
87
|
-
return deserializeInteger<T>(data);
|
|
88
|
-
} else if (isFloat<T>()) {
|
|
89
|
-
return deserializeFloat<T>(data);
|
|
90
|
-
} else if (isArray<T>()) {
|
|
91
|
-
// @ts-ignore
|
|
92
|
-
return deserializeArray<T>(data);
|
|
93
|
-
}
|
|
94
|
-
let type: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
|
|
95
|
-
if (type instanceof Box) {
|
|
96
|
-
return deserializeBox<T>(data);
|
|
97
|
-
} else if (isNullable<T>() && data == nullWord) {
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
return null;
|
|
100
|
-
// @ts-ignore
|
|
101
|
-
} else if (isDefined(type.__DESERIALIZE)) {
|
|
102
|
-
return deserializeObject<T>(data.trimStart());
|
|
103
|
-
} else if (type instanceof Map) {
|
|
104
|
-
return deserializeMap<T>(data.trimStart());
|
|
105
|
-
} else if (type instanceof Date) {
|
|
106
|
-
// @ts-ignore
|
|
107
|
-
return deserializeDate(data);
|
|
108
|
-
} else {
|
|
109
|
-
throw new Error(
|
|
110
|
-
`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// @ts-ignore: Decorator
|
|
117
|
-
@global @inline function __parseObjectValue<T>(data: string, initializeDefaultValues: boolean): T {
|
|
118
|
-
// @ts-ignore
|
|
119
|
-
if (isString<T>()) return data;
|
|
120
|
-
return JSON.parse<T>(data, initializeDefaultValues);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Dirty fix
|
|
124
|
-
// @ts-ignore: Decorator
|
|
125
|
-
@global @inline function __SERIALIZE<T>(data: T): string {
|
|
126
|
-
return JSON.stringify(data);
|
|
127
|
-
}
|
|
128
|
-
// @ts-ignore: Decorator
|
|
129
|
-
@global @inline function __DESERIALIZE<T>(data: string): T {
|
|
130
|
-
return JSON.parse<T>(data);
|
|
131
|
-
}
|
|
3
|
+
export { JSON } from "./src/json";
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { JSON } from "..";
|
|
2
1
|
import {
|
|
3
2
|
commaCode,
|
|
4
3
|
commaWord,
|
|
@@ -41,11 +40,11 @@ import { serializeString } from "./string";
|
|
|
41
40
|
// @ts-ignore
|
|
42
41
|
for (let i = 0; i < data.length - 1; i++) {
|
|
43
42
|
// @ts-ignore
|
|
44
|
-
result.write(
|
|
43
|
+
result.write(__JSON_Stringify(unchecked(data[i])));
|
|
45
44
|
result.writeCodePoint(commaCode);
|
|
46
45
|
}
|
|
47
46
|
// @ts-ignore
|
|
48
|
-
result.write(
|
|
47
|
+
result.write(__JSON_Stringify(unchecked(data[data.length - 1])));
|
|
49
48
|
result.writeCodePoint(rightBracketCode);
|
|
50
49
|
return result.toString();
|
|
51
50
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Box } from "as-container/assembly";
|
|
2
|
+
import { serializeString } from "../serialize/string";
|
|
3
|
+
import { serializeBool } from "../serialize/bool";
|
|
4
|
+
import { serializeBox } from "../serialize/box";
|
|
5
|
+
import { serializeInteger } from "../serialize/integer";
|
|
6
|
+
import { serializeFloat } from "../serialize/float";
|
|
7
|
+
import { serializeObject } from "../serialize/object";
|
|
8
|
+
import { serializeDate } from "../serialize/date";
|
|
9
|
+
import { serializeArray } from "../serialize/array";
|
|
10
|
+
import { serializeMap } from "../serialize/map";
|
|
11
|
+
import { deserializeBoolean } from "../deserialize/bool";
|
|
12
|
+
import { deserializeArray } from "../deserialize/array";
|
|
13
|
+
import { deserializeFloat } from "../deserialize/float";
|
|
14
|
+
import { deserializeBox } from "../deserialize/box";
|
|
15
|
+
import { deserializeObject } from "../deserialize/object";
|
|
16
|
+
import { deserializeMap } from "../deserialize/map";
|
|
17
|
+
import { deserializeDate } from "../deserialize/date";
|
|
18
|
+
import { nullWord } from "./chars";
|
|
19
|
+
import { deserializeInteger } from "../deserialize/integer";
|
|
20
|
+
import { deserializeString } from "../deserialize/string";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* JSON Encoder/Decoder for AssemblyScript
|
|
24
|
+
*/
|
|
25
|
+
export namespace JSON {
|
|
26
|
+
/**
|
|
27
|
+
* Stringifies valid JSON data.
|
|
28
|
+
* ```js
|
|
29
|
+
* JSON.stringify<T>(data)
|
|
30
|
+
* ```
|
|
31
|
+
* @param data T
|
|
32
|
+
* @returns string
|
|
33
|
+
*/
|
|
34
|
+
// @ts-ignore: Decorator
|
|
35
|
+
@inline export function stringify<T>(data: T): string {
|
|
36
|
+
if (isNullable<T>() && changetype<usize>(data) == <usize>0) {
|
|
37
|
+
return nullWord;
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
} else if (isString<T>()) {
|
|
40
|
+
return serializeString(data as string);
|
|
41
|
+
} else if (isBoolean<T>()) {
|
|
42
|
+
return serializeBool(data as bool);
|
|
43
|
+
} else if (data instanceof Box) {
|
|
44
|
+
return serializeBox(data);
|
|
45
|
+
} else if (isInteger<T>()) {
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
return serializeInteger<T>(data);
|
|
48
|
+
} else if (isFloat<T>(data)) {
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
return serializeFloat<T>(data);
|
|
51
|
+
// @ts-ignore: Function is generated by transform
|
|
52
|
+
} else if (isDefined(data.__SERIALIZE())) {
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
return serializeObject(data);
|
|
55
|
+
} else if (data instanceof Date) {
|
|
56
|
+
return serializeDate(data);
|
|
57
|
+
} else if (data instanceof Array) {
|
|
58
|
+
return serializeArray(data);
|
|
59
|
+
} else if (data instanceof Map) {
|
|
60
|
+
return serializeMap(data);
|
|
61
|
+
} else {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parses valid JSON strings into their original format.
|
|
69
|
+
* ```js
|
|
70
|
+
* JSON.parse<T>(data)
|
|
71
|
+
* ```
|
|
72
|
+
* @param data string
|
|
73
|
+
* @returns T
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
// @ts-ignore: Decorator
|
|
77
|
+
@inline export function parse<T>(data: string, initializeDefaultValues: boolean = false): T {
|
|
78
|
+
if (isString<T>()) {
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
return deserializeString(data);
|
|
81
|
+
} else if (isBoolean<T>()) {
|
|
82
|
+
return deserializeBoolean(data) as T;
|
|
83
|
+
} else if (isInteger<T>()) {
|
|
84
|
+
return deserializeInteger<T>(data);
|
|
85
|
+
} else if (isFloat<T>()) {
|
|
86
|
+
return deserializeFloat<T>(data);
|
|
87
|
+
} else if (isArray<T>()) {
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
return deserializeArray<T>(data);
|
|
90
|
+
}
|
|
91
|
+
let type: nonnull<T> = changetype<nonnull<T>>(0);
|
|
92
|
+
if (type instanceof Box) {
|
|
93
|
+
return deserializeBox<T>(data);
|
|
94
|
+
} else if (isNullable<T>() && data == nullWord) {
|
|
95
|
+
// @ts-ignore
|
|
96
|
+
return null;
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
} else if (isDefined(type.__JSON_Set_Key)) {
|
|
99
|
+
return deserializeObject<T>(data.trimStart(), initializeDefaultValues);
|
|
100
|
+
} else if (type instanceof Map) {
|
|
101
|
+
return deserializeMap<T>(data.trimStart());
|
|
102
|
+
} else if (type instanceof Date) {
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
return deserializeDate(data);
|
|
105
|
+
} else {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// @ts-ignore: Decorator
|
|
114
|
+
@global @inline function __parseObjectValue<T>(data: string, initializeDefaultValues: boolean): T {
|
|
115
|
+
// @ts-ignore
|
|
116
|
+
if (isString<T>()) return data;
|
|
117
|
+
return JSON.parse<T>(data, initializeDefaultValues);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Dirty fix
|
|
121
|
+
// @ts-ignore: Decorator
|
|
122
|
+
@global @inline function __JSON_Stringify<T>(data: T): string {
|
|
123
|
+
return JSON.stringify(data);
|
|
124
|
+
}
|