json-as 0.5.64 → 0.5.67

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,7 +1,4 @@
1
- <p align="center">
2
- <img width="800" src="https://raw.githubusercontent.com/JairusSW/as-json/master/assets/logo.svg" alt="logo">
3
- <br>
4
- </p>
1
+ <h1>JSON</h1>
5
2
 
6
3
  ## Installation
7
4
 
@@ -63,6 +60,7 @@ const player: Player = {
63
60
  };
64
61
 
65
62
  const stringified = JSON.stringify<Player>(player);
63
+ // Alternative: use JSON.serializeTo(player, out);
66
64
 
67
65
  const parsed = JSON.parse<Player>(stringified);
68
66
  ```
@@ -189,7 +189,7 @@ class HttpResp {
189
189
 
190
190
  describe("Deser externals", () => {
191
191
  it("should deserialize valid JSON strings", () => {
192
- canDeser<Map4>('\n{"a":\r"\\\\",\n\r"b":"}","c":"][","d"\t:\t"\\""}', { a: '\\', b: '}', c: '][', d: '"' })
192
+ canDeser<Map4>('{"a":\r"\\\\",\n\r"b":"}","c":"][","d"\t:\t"\\""}', { a: '\\', b: '}', c: '][', d: '"' })
193
193
  canDeser<Vec3>('{"x":0.4,"y":1.4,"z":0.0}', { x: 0.4, y: 1.4, z: 0 })
194
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
195
  {
@@ -101,6 +101,87 @@ export namespace JSON {
101
101
  );
102
102
  }
103
103
  }
104
+ /**
105
+ * Stringifies valid JSON data.
106
+ * ```js
107
+ * JSON.stringify<T>(data)
108
+ * ```
109
+ * @param data T
110
+ * @returns string
111
+ */
112
+ // @ts-ignore: Decorator
113
+ @inline export function stringifyTo<T>(data: T, out: string): void {
114
+ // String
115
+ if (isString<T>() && data != null) {
116
+ out = serializeString(data as string);
117
+ return;
118
+ } else if (isBoolean<T>()) {
119
+ out = data ? "true" : "false";
120
+ return;
121
+ } else if (isNullable<T>() && data == null) {
122
+ out = "null";
123
+ return;
124
+ // @ts-ignore
125
+ } else if ((isInteger<T>() || isFloat<T>()) && isFinite(data)) {
126
+ // @ts-ignore
127
+ out = data.toString();
128
+ return;
129
+ // @ts-ignore: Hidden function
130
+ } else if (isDefined(data.__JSON_Serialize)) {
131
+ // @ts-ignore: Hidden function
132
+ out = data.__JSON_Serialize();
133
+ return;
134
+ } else if (data instanceof Date) {
135
+ out = data.toISOString();
136
+ return;
137
+ } else if (isArrayLike<T>()) {
138
+ // @ts-ignore
139
+ if (data.length == 0) {
140
+ out = emptyArrayWord;
141
+ return;
142
+ // @ts-ignore
143
+ } else if (isString<valueof<T>>()) {
144
+ out = "[";
145
+ // @ts-ignore
146
+ for (let i = 0; i < data.length - 1; i++) {
147
+ // @ts-ignore
148
+ out += serializeString(unchecked(data[i]));
149
+ out += commaWord;
150
+ }
151
+ // @ts-ignore
152
+ out += serializeString(unchecked(data[data.length - 1]));
153
+ out += rightBracketWord;
154
+ return;
155
+ // @ts-ignore
156
+ } else if (isBoolean<valueof<T>>()) {
157
+ // @ts-ignore
158
+ out = leftBracketWord + data.join(commaWord) + rightBracketWord;
159
+ return;
160
+ // @ts-ignore
161
+ } else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
162
+ // @ts-ignore
163
+ out = leftBracketWord + data.join(commaWord) + rightBracketWord;
164
+ return;
165
+ } else {
166
+ let result = new StringSink(leftBracketWord);
167
+ // @ts-ignore
168
+ for (let i = 0; i < data.length - 1; i++) {
169
+ // @ts-ignore
170
+ result.write(JSON.stringify(unchecked(data[i])));
171
+ result.write(commaWord);
172
+ }
173
+ // @ts-ignore
174
+ result.write(JSON.stringify(unchecked(data[data.length - 1])));
175
+ result.write(rightBracketWord);
176
+ out = result.toString();
177
+ return;
178
+ }
179
+ } else {
180
+ throw new Error(
181
+ `Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
182
+ );
183
+ }
184
+ }
104
185
  /**
105
186
  * Parses valid JSON strings into their original format.
106
187
  * ```js
package/assembly/test.ts CHANGED
@@ -1,11 +1,10 @@
1
- import { bench, blackbox } from "as-bench/assembly/bench";
2
1
  import { JSON } from "./src/json";
3
2
  // @ts-ignore
4
3
  @json
5
4
  class Vec3 {
6
- x: f64;
7
- y: f64;
8
- z: f64;
5
+ x: f64 = 3.4;
6
+ y: f64 = 1.2;
7
+ z: f64 = 8.3;
9
8
  }
10
9
 
11
10
  // @ts-ignore
@@ -19,11 +18,7 @@ class Player {
19
18
  isVerified: boolean;
20
19
  }
21
20
 
22
- const vec: Vec3 = {
23
- x: 3.4,
24
- y: 1.2,
25
- z: 8.3,
26
- }
21
+ const vec = new Vec3();
27
22
 
28
23
  const player: Player = {
29
24
  firstName: "Emmet",
@@ -38,9 +33,13 @@ const player: Player = {
38
33
  isVerified: true,
39
34
  }
40
35
 
41
- console.log("Original: " + JSON.stringify(vec));
36
+ let out = "";
37
+
38
+ JSON.stringifyTo(vec, out);
39
+
40
+ console.log("Original: " + out);
42
41
  //console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
43
- console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{"x":3.4,"y":1.2,"z":8.3}')));
42
+ console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{}')));
44
43
 
45
44
  console.log("Original: " + JSON.stringify(player));
46
45
  //console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.64",
3
+ "version": "0.5.67",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -33,14 +33,14 @@
33
33
  "@as-tral/cli": "^2.0.1",
34
34
  "@assemblyscript/wasi-shim": "^0.1.0",
35
35
  "as-bench": "^0.0.0-alpha",
36
- "assemblyscript": "^0.27.9",
36
+ "assemblyscript": "^0.27.14",
37
37
  "assemblyscript-prettier": "^3.0.1",
38
38
  "benchmark": "^2.1.4",
39
39
  "kati": "^0.6.2",
40
40
  "microtime": "^3.1.1",
41
- "prettier": "^3.0.2",
42
- "tinybench": "^2.5.0",
43
- "typescript": "^5.1.6",
41
+ "prettier": "^3.0.3",
42
+ "tinybench": "^2.5.1",
43
+ "typescript": "^5.2.2",
44
44
  "visitor-as": "^0.11.4"
45
45
  },
46
46
  "dependencies": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.64",
3
+ "version": "0.5.67",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
package/assets/logo.svg DELETED
@@ -1,44 +0,0 @@
1
- <svg width="1111.5" height="321.5" viewBox="0 0 1111.5 321.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
2
- <style type="text/css">
3
- svg {
4
- dominant-baseline: hanging;
5
- }
6
-
7
- @keyframes anim {
8
- 0% {
9
- transform: rotate(45deg) translateY(-400%);
10
- }
11
- to {
12
- transform: rotate(45deg) translateY(-925%);
13
- }
14
- }
15
-
16
- #anim {
17
- animation: anim infinite linear 60s;
18
- }
19
- </style>
20
- <defs>
21
- <mask id="mask" x="0" y="0">
22
- <path d="M 479.5 321.5 L 338.5 321.5 L 338.5 276.5 L 293 276.5 L 293 226 L 343.5 226 L 343.5 271.5 L 474.5 271.5 L 474.5 185.5 L 338.5 185.5 L 338.5 140.5 L 293 140.5 L 293 45.5 L 338.5 45.5 L 338.5 0 L 479.5 0 L 479.5 45.5 L 525.5 45.5 L 525.5 95.5 L 474.5 95.5 L 474.5 50.5 L 343.5 50.5 L 343.5 136 L 479.5 136 L 479.5 180.5 L 525.5 180.5 L 525.5 276.5 L 479.5 276.5 L 479.5 321.5 Z M 929.5 321.5 L 879 321.5 L 879 0 L 929.5 0 L 929.5 45.5 L 974.5 45.5 L 974.5 90.5 L 1020.5 90.5 L 1020.5 226 L 1060.5 226 L 1060.5 0 L 1111.5 0 L 1111.5 321.5 L 1060.5 321.5 L 1060.5 276.5 L 1015.5 276.5 L 1015.5 231 L 969.5 231 L 969.5 95.5 L 929.5 95.5 L 929.5 321.5 Z M 186.5 321.5 L 45.5 321.5 L 45.5 276.5 L 0 276.5 L 0 180.5 L 50.5 180.5 L 50.5 271.5 L 181.5 271.5 L 181.5 50.5 L 45.5 50.5 L 45.5 0 L 186.5 0 L 186.5 45.5 L 232.5 45.5 L 232.5 276.5 L 186.5 276.5 L 186.5 321.5 Z M 772.5 321.5 L 631.5 321.5 L 631.5 276.5 L 586 276.5 L 586 45.5 L 631.5 45.5 L 631.5 0 L 772.5 0 L 772.5 45.5 L 818.5 45.5 L 818.5 276.5 L 772.5 276.5 L 772.5 321.5 Z M 636.5 50.5 L 636.5 271.5 L 767.5 271.5 L 767.5 50.5 L 636.5 50.5 Z" fill="#fff" vector-effect="non-scaling-stroke"/>
23
- </mask>
24
- </defs>
25
- <g mask="url(#mask)">
26
- <g id="anim">
27
- <rect width="1000%" height="100%" fill="#fb4934"/>
28
- <rect width="1000%" height="100%" y="75%" fill="#fe8019"/>
29
- <rect width="1000%" height="100%" y="150%" fill="#fabd2f"/>
30
- <rect width="1000%" height="100%" y="225%" fill="#b8bb26"/>
31
- <rect width="1000%" height="100%" y="300%" fill="#8ec07c"/>
32
- <rect width="1000%" height="100%" y="375%" fill="#83a598"/>
33
- <rect width="1000%" height="100%" y="450%" fill="#d3869b"/>
34
-
35
- <rect width="1000%" height="100%" y="525%" fill="#fb4934"/>
36
- <rect width="1000%" height="100%" y="600%" fill="#fe8019"/>
37
- <rect width="1000%" height="100%" y="675%" fill="#fabd2f"/>
38
- <rect width="1000%" height="100%" y="750%" fill="#b8bb26"/>
39
- <rect width="1000%" height="100%" y="825%" fill="#8ec07c"/>
40
- <rect width="1000%" height="100%" y="900%" fill="#83a598"/>
41
- <rect width="1000%" height="100%" y="975%" fill="#d3869b"/>
42
- </g>
43
- </g>
44
- </svg>