json-as 0.5.29 → 0.5.31

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
@@ -2,20 +2,20 @@
2
2
  ![AssemblyScript](https://img.shields.io/badge/AssemblyScript-blue)
3
3
  ![WebAssembly](https://img.shields.io/badge/WebAssemby-purple)
4
4
 
5
- Probably the fastest JSON parser for AssemblyScript with many more optimizations coming down the pipeline.
5
+ Probably the fastest JSON implementation for AssemblyScript with many more optimizations coming down the pipeline.
6
6
  ## Installation
7
7
 
8
8
  ```bash
9
- ~ npm install json-as
9
+ npm install json-as
10
10
  ```
11
11
  ```bash
12
- ~ npm install visitor-as
12
+ npm install visitor-as --save-dev
13
13
  ```
14
14
 
15
15
  For arbitrary-length numbers, use
16
16
 
17
17
  ```bash
18
- ~ npm install as-bignum
18
+ npm install as-bignum
19
19
  ```
20
20
 
21
21
  Add the transform to your `asc` command
@@ -40,14 +40,14 @@ Or, add it to `asconfig.json`
40
40
  import { JSON } from "json-as/assembly";
41
41
 
42
42
  // @json or @serializable work here
43
- @JSON
43
+ @json
44
44
  class Vec3 {
45
45
  x!: f32;
46
46
  y!: f32;
47
47
  z!: f32;
48
48
  }
49
49
 
50
- @JSON
50
+ @json
51
51
  class Player {
52
52
  firstName!: string;
53
53
  lastName!: string;
@@ -75,10 +75,30 @@ const stringified = JSON.stringify<Player>(player);
75
75
  const parsed = JSON.parse<Player>(stringified);
76
76
  ```
77
77
 
78
- # Notes
78
+ ## Notes
79
79
 
80
80
  Performance exceeds JavaScript JSON implementation by an average of 230% but this decreases with larger data packets.
81
81
 
82
+ ## Planned Features
83
+
84
+ - [x] Serialize
85
+ - [x] Objects
86
+ - [x] Other Types
87
+ - [ ] Dynamic Types
88
+ - [x] Deserialize
89
+ - [x] Objects
90
+ - [x] Other Types
91
+ - [ ] Dynamic Types
92
+ - [ ] Streaming API
93
+ - [ ] Whitespace support
94
+ - [ ] Integrate features from SIMDJson
95
+ - [x] Optimize
96
+ - [x] Strings
97
+ - [x] Int/Float
98
+ - [x] Bool
99
+ - [x] Object Serialization
100
+ - [ ] Object Parsing
101
+ - [ ] Arrays
82
102
  ## Performance
83
103
 
84
104
  **Serialize Object (Vec3):** ~11.1m ops/s
package/asconfig.json CHANGED
@@ -12,6 +12,8 @@
12
12
  "options": {
13
13
  "transform": [
14
14
  "./transform"
15
- ]
16
- }
15
+ ],
16
+ "bindings": "esm"
17
+ },
18
+ "extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
17
19
  }
@@ -40,6 +40,7 @@ export namespace JSON {
40
40
  export function stringify<T>(data: T): string {
41
41
  // String
42
42
  if (isString<T>() && data != null) {
43
+ // @ts-ignore
43
44
  return serializeString(data);
44
45
  }
45
46
  // Boolean
@@ -64,21 +65,28 @@ export namespace JSON {
64
65
  // @ts-ignore
65
66
  return data.__JSON_Serialize();
66
67
  }
68
+ else if (data instanceof Date) {
69
+ return data.toISOString();
70
+ }
67
71
  // ArrayLike
68
72
  else if (isArrayLike<T>()) {
69
73
  // @ts-ignore
70
74
  if (data.length == 0) {
71
75
  return emptyArrayWord;
76
+ // @ts-ignore
72
77
  } else if (isString<valueof<T>>()) {
73
78
  let result = "[";
79
+ // @ts-ignore
74
80
  for (let i = 0; i < data.length - 1; i++) {
75
81
  // @ts-ignore
76
82
  result += serializeString(unchecked(data[i]));
77
83
  result += commaWord;
78
84
  }
85
+ // @ts-ignore
79
86
  result += serializeString(unchecked(data[data.length - 1]));
80
87
  result += rightBracketWord;
81
88
  return result;
89
+ // @ts-ignore
82
90
  } else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
83
91
  let result = new StringSink(leftBracketWord);
84
92
  // @ts-ignore
@@ -140,6 +148,9 @@ export namespace JSON {
140
148
  // @ts-ignore
141
149
  } else if (isDefined(type.__JSON_Set_Key)) {
142
150
  return parseObject<T>(data.trimStart());
151
+ } else if (idof<nonnull<T>>() == idof<Date>()) {
152
+ // @ts-ignore
153
+ return Date.fromString(data);
143
154
  } else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
144
155
  // @ts-ignore
145
156
  return parseBigNum<T>(data);
@@ -169,6 +180,9 @@ export namespace JSON {
169
180
  // @ts-ignore
170
181
  } else if (isDefined(type.__JSON_Set_Key)) {
171
182
  return parseObject<T>(data.trimStart());
183
+ } else if (idof<nonnull<T>>() == idof<Date>()) {
184
+ // @ts-ignore
185
+ return Date.fromString(data);
172
186
  } else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
173
187
  // @ts-ignore
174
188
  return parseBigNum<T>(data);
package/assembly/test.ts CHANGED
@@ -1,129 +1,38 @@
1
- import { u128 } from "as-bignum/assembly";
2
- import {
3
- JSON
4
- } from ".";
5
- import { unsafeCharCodeAt } from "./src/util";
6
- import { backSlashCode, quoteCode } from "./src/chars";
1
+ import { JSON } from "./src/json";
7
2
 
8
- 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 \\""]`;
9
-
10
- /*console.log(JSON.stringify([
11
- "abcdefg",
12
- 'st"ring" w""ith quotes"',
13
- 'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
14
- 'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
15
- ]));*/
16
-
17
- const str = "\\\""
18
-
19
- function parseString(data: string): string {
20
- let result = "";
21
- let last = 1;
22
- let char = 0;
23
- for (let i = 1; i < data.length - 1; i++) {
24
- // \\"
25
- if (unsafeCharCodeAt(data, i) === backSlashCode) {
26
- char = unsafeCharCodeAt(data, ++i);
27
- result += data.slice(last, i - 1)
28
- if (char === 34) {
29
- result += "\"";
30
- last = ++i;
31
- } else if (char === 110) {
32
- result += "\n";
33
- last = ++i;
34
- // 92 98 114 116 102 117
35
- } else if (char >= 92 && char <= 117) {
36
- if (char === 92) {
37
- result += "\\";
38
- last = ++i;
39
- } else if (char === 98) {
40
- result += "\b";
41
- last = ++i;
42
- } else if (char === 102) {
43
- result += "\f";
44
- last = ++i;
45
- } else if (char === 114) {
46
- result += "\r";
47
- last = ++i;
48
- } else if (char === 116) {
49
- result += "\t";
50
- last = ++i;
51
- } else if (char === 117 && load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) === 27584753879220272) {
52
- result += "\u000b";
53
- i += 4;
54
- last = ++i;
55
- }
56
- }
57
- }
58
- }
59
- result += data.slice(last, data.length - 1);
60
- return result;
61
- }
62
-
63
- console.log(parseString("\"Hello W\\\"orld!\""));
64
- console.log(parseString("\"Hello New \\nWorld!\""));
65
- console.log(parseString("\"\\t TAB Hello World!\""));
66
- console.log(JSON.stringify(parseString("\"U000B \\u000b Hello World!\"")));
67
- console.log(load<u32>(changetype<usize>(str)).toString());
68
- console.log(load<u8>(changetype<usize>(str)).toString());
69
- console.log(load<u8>(changetype<usize>(str) + <usize>2).toString());
70
-
71
- console.log("abcdefg");
72
- console.log('st"ring" w""ith quotes"');
73
- console.log('string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n');
74
- console.log('string with colon : comma , brace [ ] bracket { } and quote " and other quote "');
75
-
76
- console.log(JSON.stringify("abcdefg"));
77
- console.log(JSON.stringify('st"ring" w""ith quotes"'));
78
- console.log(JSON.stringify('string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n'));
79
- console.log(JSON.stringify('string with colon : comma , brace [ ] bracket { } and quote " and other quote "'));
80
-
81
- console.log(JSON.stringify("Hello W\"orld!"));
82
-
83
- console.log("U000B: \u000b")
84
- // @ts-ignore
85
- @JSON
3
+ // @json or @serializable work here
4
+ @json
86
5
  class Vec3 {
87
- x: f32 = 3.4;
88
- y: f32 = 1.2;
89
- z: f32 = 8.3;
6
+ x!: f32;
7
+ y!: f32;
8
+ z!: f32;
90
9
  }
91
10
 
92
- // @ts-ignore
93
- @JSON
94
- class Stats extends Vec3 {
95
- wins: u128
96
- loss: u128
97
- }
98
-
99
- // @ts-ignore
100
11
  @json
101
12
  class Player {
102
- firstName: string;
103
- lastName: string;
104
- lastActive: i32[];
105
- age: i32;
106
- isVerified: boolean;
107
- stats: Stats
13
+ firstName!: string;
14
+ lastName!: string;
15
+ lastActive!: i32[];
16
+ age!: i32;
17
+ pos!: Vec3 | null;
18
+ isVerified!: boolean;
108
19
  }
109
20
 
110
21
  const player: Player = {
111
22
  firstName: "Emmet",
112
23
  lastName: "West",
113
- lastActive: [
114
- 8,
115
- 27,
116
- 2022
117
- ],
24
+ lastActive: [8, 27, 2022],
118
25
  age: 23,
119
- isVerified: true,
120
- stats: {
121
- wins: u128.fromString("443"),
122
- loss: u128.fromString("693")
123
- }
26
+ pos: {
27
+ x: 3.4,
28
+ y: 1.2,
29
+ z: 8.3
30
+ },
31
+ isVerified: true
124
32
  };
125
33
 
126
- const serializedPlayer = JSON.stringify<Player>(player);
127
- console.log("Serialized Player: " + serializedPlayer);
128
- const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
129
- console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
34
+ const stringified = JSON.stringify<Player>(player);
35
+ console.log(stringified);
36
+
37
+ const parsed = JSON.parse<Player>(stringified);
38
+ console.log(JSON.stringify(parsed));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.29",
3
+ "version": "0.5.31",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -125,8 +125,8 @@ class AsJSONTransform extends BaseVisitor {
125
125
  const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node);
126
126
  node.members.push(setDataMethod);
127
127
  this.schemasList.push(this.currentClass);
128
- console.log(serializeFunc);
129
- console.log(setKeyFunc);
128
+ //console.log(serializeFunc);
129
+ //console.log(setKeyFunc);
130
130
  }
131
131
  visitSource(node) {
132
132
  super.visitSource(node);
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.29",
3
+ "version": "0.5.31",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -148,8 +148,8 @@ class AsJSONTransform extends BaseVisitor {
148
148
 
149
149
  this.schemasList.push(this.currentClass);
150
150
 
151
- console.log(serializeFunc);
152
- console.log(setKeyFunc);
151
+ //console.log(serializeFunc);
152
+ //console.log(setKeyFunc);
153
153
  }
154
154
  visitSource(node: Source): void {
155
155
  super.visitSource(node);