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 CHANGED
@@ -1,6 +1,4 @@
1
- # AS-JSON
2
-
3
- JSON for AssemblyScript focused on performance, low-overhead, and ease-of-use.
1
+ <h1 align=center>JSON<h4 align=center>Implementation of JSON for modern AssemblyScript</h3></h1>
4
2
 
5
3
  ## Installation
6
4
 
@@ -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
+ });
@@ -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
- char === quoteCode &&
418
- unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
419
- ) {
420
- if (isKey === false) {
421
- key.reinst(data, outerLoopIndex, stringValueIndex);
422
- isKey = true;
423
- } else {
424
- // @ts-ignore
425
- schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, stringValueIndex);
426
- isKey = false;
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
- outerLoopIndex = ++stringValueIndex;
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
- if (unsafeCharCodeAt(data, i) === quoteCode) {
508
- if (instr === false) {
509
- instr = true;
510
- lastPos = i;
511
- } else if (unsafeCharCodeAt(data, i - 1) !== backSlashCode) {
512
- instr = false;
513
- result.push(parseString(data.slice(lastPos, i)));
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.61",
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": "^2.0.2",
37
+ "assemblyscript-prettier": "^3.0.1",
37
38
  "benchmark": "^2.1.4",
38
39
  "kati": "^0.6.2",
39
40
  "microtime": "^3.1.1",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.61",
3
+ "version": "0.5.62",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",