json-as 0.5.61 → 0.5.62
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/README.md +1 -3
- package/assembly/__tests__/as-json.spec.ts +51 -0
- package/assembly/src/json.ts +32 -20
- package/package.json +4 -3
- package/transform/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,6 +5,25 @@ function canSerde<T>(data: T, toBe: string = ""): void {
|
|
|
5
5
|
expect(deserialized).toBe(toBe);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
function canDeser<T>(data: string, toBe: T): void {
|
|
9
|
+
const deserialized = JSON.parse<T>(data);
|
|
10
|
+
expect(deserialized).toStrictEqual(toBe);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function canSer<T>(data: T, toBe: string): void {
|
|
14
|
+
const serialized = JSON.stringify<T>(data);
|
|
15
|
+
expect(serialized).toBe(toBe);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
@json
|
|
20
|
+
class Map4 {
|
|
21
|
+
a: string;
|
|
22
|
+
b: string;
|
|
23
|
+
c: string;
|
|
24
|
+
d: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
8
27
|
// @ts-ignore
|
|
9
28
|
@json
|
|
10
29
|
class Vec3 {
|
|
@@ -152,3 +171,35 @@ describe("Ser/de Objects", () => {
|
|
|
152
171
|
}, '{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}');
|
|
153
172
|
});
|
|
154
173
|
});
|
|
174
|
+
|
|
175
|
+
describe("Ser externals", () => {
|
|
176
|
+
it("should serialize valid objects", () => {
|
|
177
|
+
canSer<Map4>({ a: '\\', b: '}', c: '][', d: '"' }, '{"a":"\\\\","b":"}","c":"][","d":"\\""}')
|
|
178
|
+
canSer<Vec3>({ x: 0.4, y: 1.4, z: 0 }, '{"x":0.4,"y":1.4,"z":0.0}')
|
|
179
|
+
})
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// @ts-ignore
|
|
183
|
+
@json
|
|
184
|
+
class HttpResp {
|
|
185
|
+
statusCode: number;
|
|
186
|
+
headers: Array<Array<string>>;
|
|
187
|
+
body: string;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
describe("Deser externals", () => {
|
|
191
|
+
it("should deserialize valid JSON strings", () => {
|
|
192
|
+
canDeser<Map4>('\n{"a":\r"\\\\",\n\r"b":"}","c":"][","d"\t:\t"\\""}', { a: '\\', b: '}', c: '][', d: '"' })
|
|
193
|
+
canDeser<Vec3>('{"x":0.4,"y":1.4,"z":0.0}', { x: 0.4, y: 1.4, z: 0 })
|
|
194
|
+
canDeser<HttpResp>('{"statusCode":200,"headers":[["Conn\\\\ection","close"],["Content-Length","375"],["ETag","W/\\"177-/Ihew5Z+fiI8NLbTM2Wyphl/PFY\\""]],\n"body":"{\\n \\\"args\\\": {},\\n \\\"headers\\\": {\\n \\\"content-length\\\": \\\"0\\\",\\n \\\"accept\\\": \\\"*/*\\\" \\n}}"}',
|
|
195
|
+
{
|
|
196
|
+
statusCode: 200,
|
|
197
|
+
headers: [
|
|
198
|
+
['Conn\\ection', 'close'],
|
|
199
|
+
['Content-Length', '375'],
|
|
200
|
+
['ETag', 'W/\"177-/Ihew5Z+fiI8NLbTM2Wyphl/PFY\"']
|
|
201
|
+
],
|
|
202
|
+
body: '{\n "args": {},\n "headers": {\n "content-length": "0",\n "accept": "*/*" \n}}'
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
});
|
package/assembly/src/json.ts
CHANGED
|
@@ -407,26 +407,31 @@ export namespace JSON {
|
|
|
407
407
|
}
|
|
408
408
|
}
|
|
409
409
|
} else if (char === quoteCode) {
|
|
410
|
+
let escaping = false;
|
|
410
411
|
for (
|
|
411
412
|
let stringValueIndex = ++outerLoopIndex;
|
|
412
413
|
stringValueIndex < data.length - 1;
|
|
413
414
|
stringValueIndex++
|
|
414
415
|
) {
|
|
415
416
|
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
416
|
-
if (
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
isKey
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
417
|
+
if (char === backSlashCode && !escaping) {
|
|
418
|
+
escaping = true;
|
|
419
|
+
} else {
|
|
420
|
+
if (
|
|
421
|
+
char === quoteCode && !escaping
|
|
422
|
+
) {
|
|
423
|
+
if (isKey === false) {
|
|
424
|
+
key.reinst(data, outerLoopIndex, stringValueIndex);
|
|
425
|
+
isKey = true;
|
|
426
|
+
} else {
|
|
427
|
+
// @ts-ignore
|
|
428
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, stringValueIndex);
|
|
429
|
+
isKey = false;
|
|
430
|
+
}
|
|
431
|
+
outerLoopIndex = ++stringValueIndex;
|
|
432
|
+
break;
|
|
427
433
|
}
|
|
428
|
-
|
|
429
|
-
break;
|
|
434
|
+
escaping = false;
|
|
430
435
|
}
|
|
431
436
|
}
|
|
432
437
|
} else if (char == nCode) {
|
|
@@ -503,15 +508,22 @@ export namespace JSON {
|
|
|
503
508
|
const result: string[] = [];
|
|
504
509
|
let lastPos = 0;
|
|
505
510
|
let instr = false;
|
|
511
|
+
let escaping = false;
|
|
506
512
|
for (let i = 1; i < data.length - 1; i++) {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
instr
|
|
513
|
-
|
|
513
|
+
const char = unsafeCharCodeAt(data, i);
|
|
514
|
+
if (char === backSlashCode && !escaping) {
|
|
515
|
+
escaping = true;
|
|
516
|
+
} else {
|
|
517
|
+
if (char === quoteCode && !escaping) {
|
|
518
|
+
if (instr === false) {
|
|
519
|
+
instr = true;
|
|
520
|
+
lastPos = i;
|
|
521
|
+
} else {
|
|
522
|
+
instr = false;
|
|
523
|
+
result.push(parseString(data.slice(lastPos, i + 1)));
|
|
524
|
+
}
|
|
514
525
|
}
|
|
526
|
+
escaping = false;
|
|
515
527
|
}
|
|
516
528
|
}
|
|
517
529
|
return result;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.62",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"Romdotdog",
|
|
10
10
|
"Derek Barrera",
|
|
11
11
|
"Frankk Taylor",
|
|
12
|
-
"lekiano"
|
|
12
|
+
"lekiano",
|
|
13
|
+
"Florian Guitton"
|
|
13
14
|
],
|
|
14
15
|
"license": "MIT",
|
|
15
16
|
"scripts": {
|
|
@@ -33,7 +34,7 @@
|
|
|
33
34
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
34
35
|
"as-bench": "^0.0.0-alpha",
|
|
35
36
|
"assemblyscript": "^0.27.9",
|
|
36
|
-
"assemblyscript-prettier": "^
|
|
37
|
+
"assemblyscript-prettier": "^3.0.1",
|
|
37
38
|
"benchmark": "^2.1.4",
|
|
38
39
|
"kati": "^0.6.2",
|
|
39
40
|
"microtime": "^3.1.1",
|