json-as 0.5.25 → 0.5.27
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/LICENSE +17 -17
- package/asconfig.json +2 -5
- package/assembly/__benches__/as-json.ts +11 -9
- package/assembly/src/json.ts +62 -49
- package/assembly/test.ts +15 -3
- package/package.json +1 -1
- package/transform/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2023 Jairus Tanaka <jairus.v.tanaka@outlook.com>
|
|
3
|
+
Copyright (c) 2023 Jairus Tanaka <jairus.v.tanaka@outlook.com>
|
|
4
4
|
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
11
|
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
14
|
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/asconfig.json
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { JSON } from "..";
|
|
2
2
|
|
|
3
3
|
@json
|
|
4
|
-
class
|
|
4
|
+
class Vec3 {
|
|
5
5
|
x: f32;
|
|
6
6
|
y: f32;
|
|
7
|
+
z: f32;
|
|
7
8
|
}
|
|
8
9
|
|
|
9
|
-
const vec:
|
|
10
|
+
const vec: Vec3 = blackbox<Vec3>({
|
|
10
11
|
x: 0.0,
|
|
11
12
|
y: 0.0,
|
|
13
|
+
z: 0.0
|
|
12
14
|
});
|
|
13
15
|
|
|
14
|
-
bench("Stringify Object (
|
|
16
|
+
bench("Stringify Object (Vec3)", () => {
|
|
15
17
|
blackbox(JSON.stringify(vec));
|
|
16
|
-
})
|
|
18
|
+
});/*
|
|
17
19
|
|
|
18
|
-
bench("Parse Object (
|
|
19
|
-
blackbox(JSON.parse<
|
|
20
|
+
bench("Parse Object (Vec3)", () => {
|
|
21
|
+
blackbox(JSON.parse<Vec3>(blackbox('{"x":0.0,"y":0.0,"z":0.0}')));
|
|
20
22
|
});
|
|
21
23
|
|
|
22
24
|
bench("Stringify Array", () => {
|
|
@@ -39,14 +41,14 @@ bench("Stringify Nested Array", () => {
|
|
|
39
41
|
|
|
40
42
|
bench("Parse Nested Array", () => {
|
|
41
43
|
blackbox(JSON.parse<string[][]>(blackbox('[["a","b","c"]]')));
|
|
42
|
-
})
|
|
44
|
+
});*/
|
|
43
45
|
|
|
44
46
|
bench("Stringify String", () => {
|
|
45
|
-
blackbox(JSON.stringify(blackbox("Hello")));
|
|
47
|
+
blackbox(JSON.stringify(blackbox("Hello \"World!")));
|
|
46
48
|
});
|
|
47
49
|
|
|
48
50
|
bench("Parse String", () => {
|
|
49
|
-
blackbox(JSON.parse<string>(blackbox('"Hello"')));
|
|
51
|
+
blackbox(JSON.parse<string>(blackbox('"Hello \"World!"')));
|
|
50
52
|
});
|
|
51
53
|
/*
|
|
52
54
|
bench("Stringify Boolean", () => {
|
package/assembly/src/json.ts
CHANGED
|
@@ -40,52 +40,65 @@ export namespace JSON {
|
|
|
40
40
|
export function stringify<T>(data: T): string {
|
|
41
41
|
// String
|
|
42
42
|
if (isString<T>() && data != null) {
|
|
43
|
-
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
if (data.length === 0) return "\"\"";
|
|
45
|
+
|
|
46
|
+
let result = "\"";
|
|
47
|
+
|
|
48
|
+
let char: i32 = 0;
|
|
49
|
+
let last: i32 = 0;
|
|
50
|
+
let found: boolean = false;
|
|
44
51
|
// @ts-ignore
|
|
45
52
|
for (let i = 0; i < data.length; i++) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
53
|
+
char = unsafeCharCodeAt(<string>data, i);
|
|
54
|
+
if (char === 34 || char === 92) {
|
|
55
|
+
result += (<string>data).slice(last, i) + "\\";
|
|
56
|
+
last = i;
|
|
57
|
+
found = true;
|
|
58
|
+
i++;
|
|
59
|
+
} else if (char <= 13 && char >= 8) {
|
|
60
|
+
result += (<string>data).slice(last, i);
|
|
61
|
+
last = ++i;
|
|
62
|
+
found = true;
|
|
63
|
+
switch (char) {
|
|
64
|
+
case 0x22: {
|
|
65
|
+
result += "\\\"";
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case 0x5C: {
|
|
69
|
+
result += "\\\\";
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case 0x08: {
|
|
73
|
+
result += "\\b";
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case 0x0A: {
|
|
77
|
+
result += "\\n";
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case 0x0D: {
|
|
81
|
+
result += "\\r";
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
case 0x09: {
|
|
85
|
+
result += "\\t";
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case 0x0C: {
|
|
89
|
+
result += "\\f";
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
case 0x0B: {
|
|
93
|
+
result += "\\u000b";
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
84
96
|
}
|
|
85
97
|
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
98
|
+
}// 8 10 13 9 12
|
|
99
|
+
if (!found) return "\"" + data + "\"";
|
|
100
|
+
else result += (<string>data).slice(last);
|
|
101
|
+
return result + "\"";
|
|
89
102
|
}
|
|
90
103
|
// Boolean
|
|
91
104
|
else if (isBoolean<T>()) {
|
|
@@ -205,17 +218,17 @@ export namespace JSON {
|
|
|
205
218
|
function parseBigNum<T>(data: string): T {
|
|
206
219
|
// @ts-ignore
|
|
207
220
|
if (idof<T>() == idof<u128>()) return u128.fromString(data);
|
|
208
|
-
|
|
221
|
+
// @ts-ignore
|
|
209
222
|
if (idof<T>() == idof<u128Safe>()) return u128Safe.fromString(data);
|
|
210
|
-
|
|
223
|
+
// @ts-ignore
|
|
211
224
|
if (idof<T>() == idof<u256>()) return u128Safe.fromString(data);
|
|
212
|
-
|
|
225
|
+
// @ts-ignore
|
|
213
226
|
if (idof<T>() == idof<u256Safe>()) return u256Safe.fromString(data);
|
|
214
|
-
|
|
227
|
+
// @ts-ignore
|
|
215
228
|
if (idof<T>() == idof<i128>()) return i128.fromString(data);
|
|
216
|
-
|
|
229
|
+
// @ts-ignore
|
|
217
230
|
if (idof<T>() == idof<i128Safe>()) return i128Safe.fromString(data);
|
|
218
|
-
|
|
231
|
+
// @ts-ignore
|
|
219
232
|
//if (idof<T>() == idof<i256Safe>()) return data.
|
|
220
233
|
}
|
|
221
234
|
|
|
@@ -394,7 +407,7 @@ function parseArray<T extends unknown[]>(data: string): T {
|
|
|
394
407
|
return parseArrayArray<T>(data);
|
|
395
408
|
// @ts-ignore
|
|
396
409
|
} else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
|
|
397
|
-
const type = changetype<nonnull<valueof<T>>>(__new(offsetof<nonnull<valueof<T>>>(), idof
|
|
410
|
+
const type = changetype<nonnull<valueof<T>>>(__new(offsetof<nonnull<valueof<T>>>(), idof<nonnull<valueof<T>>>()));
|
|
398
411
|
// @ts-ignore
|
|
399
412
|
if (isDefined(type.__JSON_Set_Key)) {
|
|
400
413
|
// @ts-ignore
|
package/assembly/test.ts
CHANGED
|
@@ -5,13 +5,25 @@ import {
|
|
|
5
5
|
|
|
6
6
|
const exp = `["abcdefg","st\\"ring\\" w\\"\\"ith quotes\\"","string \\t\\r\\"with ran\\tdom spa\\nces and \\nnewlines\\n\\n\\n","string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\""]`;
|
|
7
7
|
|
|
8
|
-
console.log(exp);
|
|
9
|
-
console.log(JSON.stringify([
|
|
8
|
+
///console.log(exp);
|
|
9
|
+
/*console.log(JSON.stringify([
|
|
10
10
|
"abcdefg",
|
|
11
11
|
'st"ring" w""ith quotes"',
|
|
12
12
|
'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
|
|
13
13
|
'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
|
|
14
|
-
]))
|
|
14
|
+
]));*/
|
|
15
|
+
|
|
16
|
+
console.log("abcdefg");
|
|
17
|
+
console.log('st"ring" w""ith quotes"');
|
|
18
|
+
console.log('string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n');
|
|
19
|
+
console.log('string with colon : comma , brace [ ] bracket { } and quote " and other quote "');
|
|
20
|
+
|
|
21
|
+
console.log(JSON.stringify("abcdefg"));
|
|
22
|
+
console.log(JSON.stringify('st"ring" w""ith quotes"'));
|
|
23
|
+
console.log(JSON.stringify('string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n'));
|
|
24
|
+
console.log(JSON.stringify('string with colon : comma , brace [ ] bracket { } and quote " and other quote "'));
|
|
25
|
+
|
|
26
|
+
console.log(JSON.stringify("Hello W\"orld!"));
|
|
15
27
|
// @ts-ignore
|
|
16
28
|
@JSON
|
|
17
29
|
class Vec3 {
|
package/package.json
CHANGED