json-as 0.5.41 → 0.5.42
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/asconfig.json +1 -4
- package/assembly/__tests__/as-json.spec.ts +14 -65
- package/assembly/src/json.ts +15 -18
- package/assembly/src/util.ts +21 -6
- package/assembly/test.ts +18 -9
- package/package.json +1 -1
- package/transform/package.json +1 -1
package/asconfig.json
CHANGED
|
@@ -33,13 +33,24 @@ class Player {
|
|
|
33
33
|
isVerified: boolean;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
class Nullable {}
|
|
36
|
+
class Nullable { }
|
|
37
37
|
type Null = Nullable | null;
|
|
38
38
|
|
|
39
39
|
describe("Ser/de Nulls", () => {
|
|
40
40
|
canSerde<Null>(null);
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
+
describe("Ser/de Strings", () => {
|
|
44
|
+
it("should ser/de strings", () => {
|
|
45
|
+
canSerde<string>("abcdefg");
|
|
46
|
+
canSerde<string>('st"ring" w""ith quotes"');
|
|
47
|
+
canSerde<string>('string \"with random spa\nces and \nnewlines\n\n\n');
|
|
48
|
+
canSerde<string>(
|
|
49
|
+
'string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"'
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
43
54
|
describe("Ser/de Numbers", () => {
|
|
44
55
|
it("should ser/de integers", () => {
|
|
45
56
|
canSerde<i32>(0);
|
|
@@ -48,34 +59,6 @@ describe("Ser/de Numbers", () => {
|
|
|
48
59
|
canSerde<u64>(101);
|
|
49
60
|
canSerde<i32>(-100);
|
|
50
61
|
canSerde<i64>(-101);
|
|
51
|
-
|
|
52
|
-
// canSerde<u128>(u128.from("0"))
|
|
53
|
-
// canSerde<u128>(u128.from("100"))
|
|
54
|
-
// canSerde<u128>(u128.from("101"))
|
|
55
|
-
|
|
56
|
-
/* canSerde<u128Safe>(u128Safe.from("0"))
|
|
57
|
-
canSerde<u128Safe>(u128Safe.from("100"))
|
|
58
|
-
canSerde<u128Safe>(u128Safe.from("101"))
|
|
59
|
-
|
|
60
|
-
canSerde<u256>(u256.fromU128(u128.from("0")))
|
|
61
|
-
canSerde<u256>(u256.fromU128(u128.from("100")))
|
|
62
|
-
canSerde<u256>(u256.fromU128(u128.from("101")))
|
|
63
|
-
|
|
64
|
-
canSerde<u256Safe>(u256Safe.fromU128(u128.from("0")))
|
|
65
|
-
canSerde<u256Safe>(u256Safe.fromU128(u128.from("100")))
|
|
66
|
-
canSerde<u256Safe>(u256Safe.fromU128(u128.from("101")))
|
|
67
|
-
|
|
68
|
-
canSerde<i128>(i128.from("0"))
|
|
69
|
-
canSerde<i128>(i128.from("100"))
|
|
70
|
-
canSerde<i128>(i128.from("101"))
|
|
71
|
-
|
|
72
|
-
canSerde<i128Safe>(i128Safe.from("0"))
|
|
73
|
-
canSerde<i128Safe>(i128Safe.from("100"))
|
|
74
|
-
canSerde<i128Safe>(i128Safe.from("101"))
|
|
75
|
-
canSerde<i128Safe>(i128Safe.from("-100"))
|
|
76
|
-
canSerde<i128Safe>(i128Safe.from("-101"))
|
|
77
|
-
*/
|
|
78
|
-
//canSerde<i256Safe>(new i256Safe(10, 11, 500, 501))
|
|
79
62
|
});
|
|
80
63
|
|
|
81
64
|
it("should ser/de floats", () => {
|
|
@@ -98,30 +81,13 @@ describe("Ser/de Numbers", () => {
|
|
|
98
81
|
canSerde<boolean>(false);
|
|
99
82
|
});
|
|
100
83
|
|
|
101
|
-
it("should ser/de strings", () => {
|
|
102
|
-
canSerde<string>("abcdefg");
|
|
103
|
-
canSerde<string>('st"ring" w""ith quotes"');
|
|
104
|
-
canSerde<string>(
|
|
105
|
-
'string \t\r\\"with ran\tdom spa\nces and \nnewlines\n\n\n'
|
|
106
|
-
);
|
|
107
|
-
canSerde<string>(
|
|
108
|
-
'string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"'
|
|
109
|
-
);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
84
|
it("should ser/de BigInt objects", () => {
|
|
113
|
-
|
|
85
|
+
canSerde<i32>(0);
|
|
114
86
|
|
|
115
87
|
canSerde<u32>(100);
|
|
116
88
|
canSerde<u64>(101);
|
|
117
89
|
canSerde<i32>(-100);
|
|
118
90
|
canSerde<i64>(-101);
|
|
119
|
-
canSerde<u128>(u128.from("0"))
|
|
120
|
-
canSerde<u128>(u128.from("100"))
|
|
121
|
-
canSerde<u128>(u128.from("101"))
|
|
122
|
-
canSerde<u128>(u128.from("-100"))
|
|
123
|
-
canSerde<u128>(u128.from("-101"))
|
|
124
|
-
*/
|
|
125
91
|
});
|
|
126
92
|
});
|
|
127
93
|
|
|
@@ -144,13 +110,7 @@ describe("Ser/de Array", () => {
|
|
|
144
110
|
});
|
|
145
111
|
|
|
146
112
|
it("should ser/de string arrays", () => {
|
|
147
|
-
|
|
148
|
-
canSerde<string[]>([
|
|
149
|
-
"abcdefg",
|
|
150
|
-
'st"ring" w""ith quotes"',
|
|
151
|
-
'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
|
|
152
|
-
'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
|
|
153
|
-
]);
|
|
113
|
+
canSerde<string[]>(['string \"with random spa\nces and \nnewlines\n\n\n']);
|
|
154
114
|
});
|
|
155
115
|
|
|
156
116
|
it("should ser/de nested integer arrays", () => {
|
|
@@ -174,17 +134,6 @@ describe("Ser/de Array", () => {
|
|
|
174
134
|
canSerde<boolean[][]>([[true], [false]]);
|
|
175
135
|
});
|
|
176
136
|
|
|
177
|
-
it("should ser/de string arrays", () => {
|
|
178
|
-
canSerde<string[][]>([
|
|
179
|
-
["abcdefg"],
|
|
180
|
-
['st"ring" w""ith quotes"'],
|
|
181
|
-
['string \t\r\\"with ran\tdom spa\nces and \nnewlines\n\n\n'],
|
|
182
|
-
[
|
|
183
|
-
'string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"',
|
|
184
|
-
],
|
|
185
|
-
]);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
137
|
it("should ser/de object arrays", () => {
|
|
189
138
|
canSerde<Vec3[]>([
|
|
190
139
|
{
|
package/assembly/src/json.ts
CHANGED
|
@@ -272,10 +272,10 @@ export namespace JSON {
|
|
|
272
272
|
if (char === 34 || char === 92) {
|
|
273
273
|
result += (<string>data).slice(last, i) + "\\";
|
|
274
274
|
last = i;
|
|
275
|
-
i++;
|
|
275
|
+
//i++;
|
|
276
276
|
} else if (char <= 13 && char >= 8) {
|
|
277
277
|
result += (<string>data).slice(last, i);
|
|
278
|
-
last =
|
|
278
|
+
last = i + 1;
|
|
279
279
|
switch (char) {
|
|
280
280
|
case 8: {
|
|
281
281
|
result += "\\b";
|
|
@@ -322,40 +322,37 @@ export namespace JSON {
|
|
|
322
322
|
result += data.slice(last, i - 1);
|
|
323
323
|
if (char === 34) {
|
|
324
324
|
result += '"';
|
|
325
|
-
last =
|
|
326
|
-
} else if (char === 110) {
|
|
327
|
-
result += "\n";
|
|
328
|
-
last = ++i;
|
|
329
|
-
// 92 98 114 116 102 117
|
|
325
|
+
last = i + 1;
|
|
330
326
|
} else if (char >= 92 && char <= 117) {
|
|
331
327
|
switch (char) {
|
|
332
328
|
case 92: {
|
|
333
329
|
result += "\\";
|
|
334
|
-
last =
|
|
330
|
+
last = i + 1;
|
|
335
331
|
break;
|
|
336
332
|
}
|
|
337
333
|
case 98: {
|
|
338
334
|
result += "\b";
|
|
339
|
-
last =
|
|
335
|
+
last = i + 1;
|
|
340
336
|
break;
|
|
341
337
|
}
|
|
342
|
-
case 110: {
|
|
343
|
-
result += "\n";
|
|
344
|
-
last = ++i;
|
|
345
|
-
}
|
|
346
338
|
case 102: {
|
|
347
339
|
result += "\f";
|
|
348
|
-
last =
|
|
340
|
+
last = i + 1;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case 110: {
|
|
344
|
+
result += "\n";
|
|
345
|
+
last = i + 1;
|
|
349
346
|
break;
|
|
350
347
|
}
|
|
351
348
|
case 114: {
|
|
352
349
|
result += "\r";
|
|
353
|
-
last =
|
|
350
|
+
last = i + 1;
|
|
354
351
|
break;
|
|
355
352
|
}
|
|
356
353
|
case 116: {
|
|
357
354
|
result += "\t";
|
|
358
|
-
last =
|
|
355
|
+
last = i + 1;
|
|
359
356
|
break;
|
|
360
357
|
}
|
|
361
358
|
default: {
|
|
@@ -366,7 +363,7 @@ export namespace JSON {
|
|
|
366
363
|
) {
|
|
367
364
|
result += "\u000b";
|
|
368
365
|
i += 4;
|
|
369
|
-
last =
|
|
366
|
+
last = i + 1;
|
|
370
367
|
}
|
|
371
368
|
break;
|
|
372
369
|
}
|
|
@@ -579,7 +576,7 @@ export namespace JSON {
|
|
|
579
576
|
lastPos = i;
|
|
580
577
|
} else if (unsafeCharCodeAt(data, i - 1) !== backSlashCode) {
|
|
581
578
|
instr = false;
|
|
582
|
-
result.push(data.slice(lastPos
|
|
579
|
+
result.push(parseString(data.slice(lastPos, i)));
|
|
583
580
|
}
|
|
584
581
|
}
|
|
585
582
|
}
|
package/assembly/src/util.ts
CHANGED
|
@@ -89,13 +89,28 @@ export function getArrayDepth<T>(depth: i32 = 1): i32 {
|
|
|
89
89
|
export function atoi_fast<T extends number>(str: string, offset: i32 = 0): T {
|
|
90
90
|
// @ts-ignore
|
|
91
91
|
let val: T = 0;
|
|
92
|
-
|
|
92
|
+
let firstChar = load<u16>(changetype<usize>(str) + <usize>offset);
|
|
93
|
+
if (firstChar === 45) {
|
|
94
|
+
offset += 2;
|
|
95
|
+
for (; offset < str.length << 1; offset += 2) {
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
val =
|
|
98
|
+
(val << 1) +
|
|
99
|
+
(val << 3) +
|
|
100
|
+
(load<u16>(changetype<usize>(str) + <usize>offset) - 48);
|
|
101
|
+
// We use load because in this case, there is no need to have bounds-checking
|
|
102
|
+
}
|
|
93
103
|
// @ts-ignore
|
|
94
|
-
val =
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
104
|
+
val = -val;
|
|
105
|
+
} else {
|
|
106
|
+
for (; offset < str.length << 1; offset += 2) {
|
|
107
|
+
// @ts-ignore
|
|
108
|
+
val =
|
|
109
|
+
(val << 1) +
|
|
110
|
+
(val << 3) +
|
|
111
|
+
(load<u16>(changetype<usize>(str) + <usize>offset) - 48);
|
|
112
|
+
// We use load because in this case, there is no need to have bounds-checking
|
|
113
|
+
}
|
|
99
114
|
}
|
|
100
115
|
return val;
|
|
101
116
|
}
|
package/assembly/test.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { JSON } from "./src/json";
|
|
2
2
|
import { atoi_fast, parseSciInteger } from "./src/util";
|
|
3
3
|
import * as a from "util/number";
|
|
4
|
+
// "st\"ring\" w\"\"ith quotes\""
|
|
5
|
+
|
|
4
6
|
@json
|
|
5
7
|
class Vec3 {
|
|
6
8
|
x!: f32;
|
|
@@ -53,18 +55,25 @@ console.log("123 - " + parseSciInteger<i32>("123").toString());
|
|
|
53
55
|
console.log("1230 - " + parseSciInteger<i32>("123e1").toString());
|
|
54
56
|
console.log("12300 - " + parseSciInteger<i32>("123e2").toString());
|
|
55
57
|
console.log("123000 - " + parseSciInteger<i32>("123e3").toString());
|
|
56
|
-
console.log("
|
|
58
|
+
console.log("12 - " + parseSciInteger<i32>("123e-1").toString());
|
|
57
59
|
console.log(parseSciInteger<i32>("100").toString());
|
|
58
60
|
console.log(parseSciInteger<i32>("-100").toString());
|
|
59
61
|
|
|
60
|
-
console.log(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
)
|
|
62
|
+
console.log(JSON.stringify("abcdefg"));
|
|
63
|
+
console.log('"abcdefg"')
|
|
64
|
+
console.log(JSON.stringify('st"ring" w""ith quotes"'));
|
|
65
|
+
console.log('"st\\"ring\\" w\\"\\"ith quotes\\""')
|
|
66
|
+
console.log(JSON.stringify(['string "with random spa\nces and \nnewlines\n\n\n']));
|
|
67
|
+
console.log(JSON.stringify(JSON.parse<string[]>(JSON.stringify(['string "with random spa\nces and \nnewlines\n\n\n']))));
|
|
68
|
+
console.log('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"')
|
|
69
|
+
console.log(JSON.stringify('string with colon : comma , brace [ ] bracket { } and quote " and other quote "'));
|
|
70
|
+
/*console.log(JSON.stringify(JSON.parse<string[]>(JSON.stringify([
|
|
71
|
+
"abcdefg",
|
|
72
|
+
'st"ring" w""ith quotes"',
|
|
73
|
+
'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
|
|
74
|
+
'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
|
|
75
|
+
]))));
|
|
76
|
+
console.log('["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 \""]');/*
|
|
68
77
|
console.log(
|
|
69
78
|
JSON.stringify(
|
|
70
79
|
JSON.parse<string[]>(
|
package/package.json
CHANGED