json-as 0.5.26 → 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/asconfig.json CHANGED
@@ -12,9 +12,6 @@
12
12
  "options": {
13
13
  "transform": [
14
14
  "./transform"
15
- ],
16
- "bindings": "esm",
17
- "exportStart": "_start"
18
- },
19
- "extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
15
+ ]
16
+ }
20
17
  }
@@ -1,22 +1,24 @@
1
1
  import { JSON } from "..";
2
2
 
3
3
  @json
4
- class Vec2 {
4
+ class Vec3 {
5
5
  x: f32;
6
6
  y: f32;
7
+ z: f32;
7
8
  }
8
9
 
9
- const vec: Vec2 = blackbox<Vec2>({
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 (Vec2)", () => {
16
+ bench("Stringify Object (Vec3)", () => {
15
17
  blackbox(JSON.stringify(vec));
16
- });
18
+ });/*
17
19
 
18
- bench("Parse Object (Vec2)", () => {
19
- blackbox(JSON.parse<Vec2>(blackbox('{"x":0.0,"y":0.0}')));
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", () => {
@@ -41,53 +41,64 @@ export namespace JSON {
41
41
  // String
42
42
  if (isString<T>() && data != null) {
43
43
  // @ts-ignore
44
- if (data.length === 0) return "\"\"";
45
- let result = new StringSink("\"");
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;
46
51
  // @ts-ignore
47
52
  for (let i = 0; i < data.length; i++) {
48
- // @ts-ignore
49
- switch (unsafeCharCodeAt(data, i)) {
50
- case 0x22: {
51
- result.write("\\\"");
52
- break;
53
- }
54
- case 0x5C: {
55
- result.write("\\\\");
56
- break;
57
- }
58
- case 0x08: {
59
- result.write("\\b");
60
- break;
61
- }
62
- case 0x0A: {
63
- result.write("\\n");
64
- break;
65
- }
66
- case 0x0D: {
67
- result.write("\\r");
68
- break;
69
- }
70
- case 0x09: {
71
- result.write("\\t");
72
- break;
73
- }
74
- case 0x0C: {
75
- result.write("\\f");
76
- break;
77
- }
78
- case 0x0B: {
79
- result.write("\\u000b");
80
- break;
81
- }
82
- default: {
83
- // @ts-ignore
84
- result.write(data.charAt(i));
85
- break;
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
+ }
86
96
  }
87
97
  }
88
- }
89
- result.write("\"");
90
- return result.toString();
98
+ }// 8 10 13 9 12
99
+ if (!found) return "\"" + data + "\"";
100
+ else result += (<string>data).slice(last);
101
+ return result + "\"";
91
102
  }
92
103
  // Boolean
93
104
  else if (isBoolean<T>()) {
@@ -207,17 +218,17 @@ export namespace JSON {
207
218
  function parseBigNum<T>(data: string): T {
208
219
  // @ts-ignore
209
220
  if (idof<T>() == idof<u128>()) return u128.fromString(data);
210
- // @ts-ignore
221
+ // @ts-ignore
211
222
  if (idof<T>() == idof<u128Safe>()) return u128Safe.fromString(data);
212
- // @ts-ignore
223
+ // @ts-ignore
213
224
  if (idof<T>() == idof<u256>()) return u128Safe.fromString(data);
214
- // @ts-ignore
225
+ // @ts-ignore
215
226
  if (idof<T>() == idof<u256Safe>()) return u256Safe.fromString(data);
216
- // @ts-ignore
227
+ // @ts-ignore
217
228
  if (idof<T>() == idof<i128>()) return i128.fromString(data);
218
- // @ts-ignore
229
+ // @ts-ignore
219
230
  if (idof<T>() == idof<i128Safe>()) return i128Safe.fromString(data);
220
- // @ts-ignore
231
+ // @ts-ignore
221
232
  //if (idof<T>() == idof<i256Safe>()) return data.
222
233
  }
223
234
 
@@ -396,7 +407,7 @@ function parseArray<T extends unknown[]>(data: string): T {
396
407
  return parseArrayArray<T>(data);
397
408
  // @ts-ignore
398
409
  } else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
399
- const type = changetype<nonnull<valueof<T>>>(__new(offsetof<nonnull<valueof<T>>>(), idof <nonnull<valueof<T>>>()));
410
+ const type = changetype<nonnull<valueof<T>>>(__new(offsetof<nonnull<valueof<T>>>(), idof<nonnull<valueof<T>>>()));
400
411
  // @ts-ignore
401
412
  if (isDefined(type.__JSON_Set_Key)) {
402
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.26",
3
+ "version": "0.5.27",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.26",
3
+ "version": "0.5.27",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",