json-as 0.9.19 → 0.9.21

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/CHANGELOG CHANGED
@@ -27,6 +27,8 @@ v0.9.16 - JSON.Raw should be completely untouched
27
27
  v0.9.17 - A schema's parent's fields should be included properly
28
28
  v0.9.18 - Should be able to use @alias and @omit*** or JSON.Raw
29
29
  v0.9.19 - Fix arguments in @omitif declarations not working properly
30
+ v0.9.20 - Strings were being received with quotes attached via the toString functionality. Removed that.
31
+ v0.9.21 - Fix #89
30
32
 
31
33
  [UNRELEASED] v1.0.0
32
34
  - Allow nullable primitives
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  __| || __|| || | | ___ | _ || __|
4
4
  | | ||__ || | || | | ||___|| ||__ |
5
5
  |_____||_____||_____||_|___| |__|__||_____|
6
- v0.9.19
6
+ v0.9.21
7
7
  </pre>
8
8
  </h5>
9
9
 
@@ -107,25 +107,16 @@ const serialized = JSON.stringify(arr);
107
107
  const parsed = JSON.parse<Base[]>(serialized);
108
108
  ```
109
109
 
110
- Classes can even have inheritance. Here's a nasty example
111
-
112
- ```js
113
- @json
114
- class Base {}
115
-
116
- const serialized = JSON.stringify(arr);
117
- // [{"x":1.0},{"x":1.0,"y":2.0},{"y":2.0,"x":1.0,"z":3.0}]
118
- const parsed = JSON.parse<Base[]>(serialized);
119
- ```
120
-
121
110
  You can also add it to your `asconfig.json`
122
111
 
112
+ ```json
123
113
  {
124
- // ...
125
- "options": {
126
- "transform": ["json-as/transform"]
127
- }
114
+ // ...
115
+ "options": {
116
+ "transform": ["json-as/transform"]
117
+ }
128
118
  }
119
+ ````
129
120
 
130
121
  If you use this project in your codebase, consider dropping a [star](https://github.com/JairusSW/as-json). I would really appreciate it!
131
122
 
@@ -0,0 +1,20 @@
1
+ import { JSON } from "json-as";
2
+ import { describe, expect, run } from "as-test/assembly";
3
+
4
+ describe("Should serialize booleans", () => {
5
+ expect(JSON.stringify<bool>(true)).toBe("true");
6
+
7
+ expect(JSON.stringify<bool>(false)).toBe("false");
8
+
9
+ expect(JSON.stringify<boolean>(true)).toBe("true");
10
+
11
+ expect(JSON.stringify<boolean>(false)).toBe("false");
12
+ });
13
+
14
+ describe("Should deserialize booleans", () => {
15
+ expect(JSON.parse<boolean>("true")).toBe(true);
16
+
17
+ expect(JSON.parse<boolean>("false")).toBe(false);
18
+ });
19
+
20
+ run();
@@ -0,0 +1,52 @@
1
+ import { JSON } from "json-as";
2
+ import { describe, expect, run } from "as-test/assembly";
3
+
4
+ describe("Should serialize floats", () => {
5
+ expect(JSON.stringify<f64>(7.23)).toBe("7.23");
6
+
7
+ expect(JSON.stringify<f64>(10e2)).toBe("1000.0");
8
+
9
+ expect(JSON.stringify<f64>(123456e-5)).toBe("1.23456");
10
+
11
+ expect(JSON.stringify<f64>(0.0)).toBe("0.0");
12
+
13
+ expect(JSON.stringify<f64>(-7.23)).toBe("-7.23");
14
+
15
+ expect(JSON.stringify<f64>(1e-6)).toBe("0.000001");
16
+
17
+ expect(JSON.stringify<f64>(1e-7)).toBe("1e-7");
18
+
19
+ expect(JSON.parse<f64>("1E-7")).toBe(1e-7);
20
+
21
+ expect(JSON.stringify<f64>(1e20)).toBe("100000000000000000000.0");
22
+
23
+ expect(JSON.stringify<f64>(1e21)).toBe("1e+21");
24
+
25
+ expect(JSON.parse<f64>("1E+21")).toBe(1e21);
26
+
27
+ expect(JSON.parse<f64>("1e21")).toBe(1e21);
28
+
29
+ expect(JSON.parse<f64>("1E21")).toBe(1e21);
30
+ });
31
+
32
+ describe("Should deserialize floats", () => {
33
+ expect(JSON.parse<f64>("7.23")).toBe(7.23);
34
+
35
+ expect(JSON.parse<f64>("1000.0")).toBe(1000.0);
36
+
37
+ expect(JSON.parse<f64>("1.23456")).toBe(1.23456);
38
+
39
+ expect(JSON.parse<f64>("0.0")).toBe(0.0);
40
+
41
+ expect(JSON.parse<f64>("-7.23")).toBe(-7.23);
42
+
43
+ expect(JSON.parse<f64>("0.000001")).toBe(0.000001);
44
+
45
+ expect(JSON.parse<f64>("1e-7")).toBe(1e-7);
46
+
47
+ expect(JSON.parse<f64>("100000000000000000000.0")).toBe(1e20);
48
+
49
+ expect(JSON.parse<f64>("1e+21")).toBe(1e21);
50
+ });
51
+
52
+ run();
@@ -0,0 +1,28 @@
1
+ import { JSON } from "json-as";
2
+ import { describe, expect, run } from "as-test/assembly";
3
+
4
+ describe("Should serialize integers", () => {
5
+ expect(JSON.stringify(0)).toBe("0");
6
+
7
+ expect(JSON.stringify<u32>(100)).toBe("100");
8
+
9
+ expect(JSON.stringify<u64>(101)).toBe("101");
10
+
11
+ expect(JSON.stringify<i32>(-100)).toBe("-100");
12
+
13
+ expect(JSON.stringify<i64>(-101)).toBe("-101");
14
+ });
15
+
16
+ describe("Should deserialize integers", () => {
17
+ expect(JSON.parse<i32>("0")).toBe(<i32>0);
18
+
19
+ expect(JSON.parse<u32>("100")).toBe(<u32>100);
20
+
21
+ expect(JSON.parse<u64>("101")).toBe(<u64>101);
22
+
23
+ expect(JSON.parse<i32>("-100")).toBe(<i32>-100);
24
+
25
+ expect(JSON.parse<i64>("-101")).toBe(<i64>-101);
26
+ });
27
+
28
+ run();
@@ -0,0 +1,5 @@
1
+ import { JSON } from "json-as";
2
+ import { describe, expect, run } from "as-test/assembly";
3
+
4
+
5
+ run();
@@ -0,0 +1,48 @@
1
+ import { JSON } from "json-as";
2
+ import { describe, expect, run } from "as-test/assembly";
3
+
4
+ describe("Should serialize strings", () => {
5
+ expect(JSON.stringify("abcdefg")).toBe('"abcdefg"');
6
+
7
+ expect(JSON.stringify('st"ring" w""ith quotes"')).toBe(
8
+ '"st\\"ring\\" w\\"\\"ith quotes\\""',
9
+ );
10
+
11
+ expect(
12
+ JSON.stringify('string "with random spa\nces and \nnewlines\n\n\n'),
13
+ ).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
14
+
15
+ expect(
16
+ JSON.stringify(
17
+ 'string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"',
18
+ ),
19
+ ).toBe(
20
+ '"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\\\""',
21
+ );
22
+ });
23
+
24
+ describe("Should deserialize strings", () => {
25
+ expect(JSON.parse<string>('"abcdefg"')).toBe("abcdefg");
26
+
27
+ expect(
28
+ JSON.parse<string>(
29
+ '"\\"st\\\\\\"ring\\\\\\" w\\\\\\"\\\\\\"ith quotes\\\\\\"\\""',
30
+ ),
31
+ ).toBe('"st\\"ring\\" w\\"\\"ith quotes\\""');
32
+
33
+ expect(
34
+ JSON.parse<string>(
35
+ '"\\"string \\\\\\"with random spa\\\\nces and \\\\nnewlines\\\\n\\\\n\\\\n\\""',
36
+ ),
37
+ ).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
38
+
39
+ expect(
40
+ JSON.parse<string>(
41
+ '"\\"string with colon : comma , brace [ ] bracket { } and quote \\\\\\" and other quote \\\\\\\\\\"\\""',
42
+ ),
43
+ ).toBe(
44
+ '"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\""',
45
+ );
46
+ });
47
+
48
+ run();
@@ -10,76 +10,6 @@ import {
10
10
  Vec3,
11
11
  } from "./types";
12
12
 
13
- describe("Should serialize strings", () => {
14
- expect(JSON.stringify("abcdefg")).toBe('"abcdefg"');
15
-
16
- expect(JSON.stringify('st"ring" w""ith quotes"')).toBe(
17
- '"st\\"ring\\" w\\"\\"ith quotes\\""',
18
- );
19
-
20
- expect(
21
- JSON.stringify('string "with random spa\nces and \nnewlines\n\n\n'),
22
- ).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
23
-
24
- expect(
25
- JSON.stringify(
26
- 'string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"',
27
- ),
28
- ).toBe(
29
- '"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\\\""',
30
- );
31
- });
32
-
33
- describe("Should serialize integers", () => {
34
- expect(JSON.stringify(0)).toBe("0");
35
-
36
- expect(JSON.stringify<u32>(100)).toBe("100");
37
-
38
- expect(JSON.stringify<u64>(101)).toBe("101");
39
-
40
- expect(JSON.stringify<i32>(-100)).toBe("-100");
41
-
42
- expect(JSON.stringify<i64>(-101)).toBe("-101");
43
- });
44
-
45
- describe("Should serialize floats", () => {
46
- expect(JSON.stringify<f64>(7.23)).toBe("7.23");
47
-
48
- expect(JSON.stringify<f64>(10e2)).toBe("1000.0");
49
-
50
- expect(JSON.stringify<f64>(123456e-5)).toBe("1.23456");
51
-
52
- expect(JSON.stringify<f64>(0.0)).toBe("0.0");
53
-
54
- expect(JSON.stringify<f64>(-7.23)).toBe("-7.23");
55
-
56
- expect(JSON.stringify<f64>(1e-6)).toBe("0.000001");
57
-
58
- expect(JSON.stringify<f64>(1e-7)).toBe("1e-7");
59
-
60
- expect(JSON.parse<f64>("1E-7")).toBe(1e-7);
61
-
62
- expect(JSON.stringify<f64>(1e20)).toBe("100000000000000000000.0");
63
-
64
- expect(JSON.stringify<f64>(1e21)).toBe("1e+21");
65
-
66
- expect(JSON.parse<f64>("1E+21")).toBe(1e21);
67
-
68
- expect(JSON.parse<f64>("1e21")).toBe(1e21);
69
-
70
- expect(JSON.parse<f64>("1E21")).toBe(1e21);
71
- });
72
-
73
- describe("Should serialize booleans", () => {
74
- expect(JSON.stringify<bool>(true)).toBe("true");
75
-
76
- expect(JSON.stringify<bool>(false)).toBe("false");
77
-
78
- expect(JSON.stringify<boolean>(true)).toBe("true");
79
-
80
- expect(JSON.stringify<boolean>(false)).toBe("false");
81
- });
82
-
83
13
  describe("Should serialize class inheritance", () => {
84
14
  const obj = new DerivedObject("1", "2");
85
15
 
@@ -225,69 +155,6 @@ describe("Should serialize @omit'ed objects", () => {
225
155
  }),
226
156
  ).toBe('{"x":1,"y":1,"z":1}');
227
157
  });
228
-
229
- describe("Should deserialize strings", () => {
230
- expect(JSON.parse<string>('"abcdefg"')).toBe("abcdefg");
231
-
232
- expect(
233
- JSON.parse<string>(
234
- '"\\"st\\\\\\"ring\\\\\\" w\\\\\\"\\\\\\"ith quotes\\\\\\"\\""',
235
- ),
236
- ).toBe('"st\\"ring\\" w\\"\\"ith quotes\\""');
237
-
238
- expect(
239
- JSON.parse<string>(
240
- '"\\"string \\\\\\"with random spa\\\\nces and \\\\nnewlines\\\\n\\\\n\\\\n\\""',
241
- ),
242
- ).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
243
-
244
- expect(
245
- JSON.parse<string>(
246
- '"\\"string with colon : comma , brace [ ] bracket { } and quote \\\\\\" and other quote \\\\\\\\\\"\\""',
247
- ),
248
- ).toBe(
249
- '"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\""',
250
- );
251
- });
252
-
253
- describe("Should deserialize integers", () => {
254
- expect(JSON.parse<i32>("0")).toBe(<i32>0);
255
-
256
- expect(JSON.parse<u32>("100")).toBe(<u32>100);
257
-
258
- expect(JSON.parse<u64>("101")).toBe(<u64>101);
259
-
260
- expect(JSON.parse<i32>("-100")).toBe(<i32>-100);
261
-
262
- expect(JSON.parse<i64>("-101")).toBe(<i64>-101);
263
- });
264
-
265
- describe("Should deserialize floats", () => {
266
- expect(JSON.parse<f64>("7.23")).toBe(7.23);
267
-
268
- expect(JSON.parse<f64>("1000.0")).toBe(1000.0);
269
-
270
- expect(JSON.parse<f64>("1.23456")).toBe(1.23456);
271
-
272
- expect(JSON.parse<f64>("0.0")).toBe(0.0);
273
-
274
- expect(JSON.parse<f64>("-7.23")).toBe(-7.23);
275
-
276
- expect(JSON.parse<f64>("0.000001")).toBe(0.000001);
277
-
278
- expect(JSON.parse<f64>("1e-7")).toBe(1e-7);
279
-
280
- expect(JSON.parse<f64>("100000000000000000000.0")).toBe(1e20);
281
-
282
- expect(JSON.parse<f64>("1e+21")).toBe(1e21);
283
- });
284
-
285
- describe("Should deserialize booleans", () => {
286
- expect(JSON.parse<boolean>("true")).toBe(true);
287
-
288
- expect(JSON.parse<boolean>("false")).toBe(false);
289
- });
290
-
291
158
  describe("Should deserialize class inheritance", () => {
292
159
  const jsonStr = '{"a":"1","b":"2"}';
293
160
  const obj = JSON.parse<DerivedObject>(jsonStr);
package/assembly/index.ts CHANGED
@@ -156,6 +156,179 @@ export namespace JSON {
156
156
  @inline static from<T>(value: T): Box<T> {
157
157
  return new Box(value);
158
158
  }
159
+ @inline
160
+ @operator("==")
161
+ eq(other: this): bool {
162
+ if (isNullable<T>() && changetype<usize>(this) == <usize>0) {
163
+ if (changetype<usize>(other) == <usize>0) return true;
164
+ }
165
+ return this.value == other.value;
166
+ }
167
+
168
+ @inline
169
+ @operator("!=")
170
+ notEq(other: this): bool {
171
+ if (isNullable<T>() && changetype<usize>(this) == <usize>0) {
172
+ if (changetype<usize>(this) == changetype<usize>(other)) return true;
173
+ }
174
+ return this.value != other.value;
175
+ }
176
+
177
+ @inline
178
+ @operator(">")
179
+ gt(other: this): bool {
180
+ return this._val > other._val;
181
+ }
182
+
183
+ @inline
184
+ @operator(">=")
185
+ ge(other: this): bool {
186
+ return this._val >= other._val;
187
+ }
188
+
189
+ @inline
190
+ @operator("<")
191
+ lt(other: this): bool {
192
+ return this._val < other._val;
193
+ }
194
+
195
+ @inline
196
+ @operator("<=")
197
+ le(other: this): bool {
198
+ return this._val <= other._val;
199
+ }
200
+
201
+ @inline
202
+ @operator(">>")
203
+ shr(other: this): this {
204
+ // @ts-ignore
205
+ return instantiate<this>(this._val >> other._val);
206
+ }
207
+
208
+ @inline
209
+ @operator(">>>")
210
+ shr_u(other: this): this {
211
+ // @ts-ignore
212
+ return instantiate<this>(this._val >>> other._val);
213
+ }
214
+
215
+ @inline
216
+ @operator("<<")
217
+ shl(other: this): this {
218
+ // @ts-ignore
219
+ return instantiate<this>(this._val << other._val);
220
+ }
221
+
222
+ @inline
223
+ @operator("&")
224
+ and(other: this): this {
225
+ // @ts-ignore
226
+ return instantiate<this>(this._val & other._val);
227
+ }
228
+
229
+ @inline
230
+ @operator("|")
231
+ or(other: this): this {
232
+ // @ts-ignore
233
+ return instantiate<this>(this._val | other._val);
234
+ }
235
+
236
+ @inline
237
+ @operator("^")
238
+ xor(other: this): this {
239
+ // @ts-ignore
240
+ return instantiate<this>(this._val ^ other._val);
241
+ }
242
+
243
+ @inline
244
+ @operator("+")
245
+ add(other: this): this {
246
+ // @ts-ignore
247
+ return instantiate<this>(this._val + other._val);
248
+ }
249
+
250
+ @inline
251
+ @operator("-")
252
+ sub(other: this): this {
253
+ // @ts-ignore
254
+ return instantiate<this>(this._val - other._val);
255
+ }
256
+
257
+ @inline
258
+ @operator("*")
259
+ mul(other: this): this {
260
+ // @ts-ignore
261
+ return instantiate<this>(this._val * other._val);
262
+ }
263
+
264
+ @inline
265
+ @operator("/")
266
+ div(other: this): this {
267
+ // @ts-ignore
268
+ return instantiate<this>(this._val / other._val);
269
+ }
270
+
271
+ @inline
272
+ @operator("**")
273
+ pow(other: this): this {
274
+ // @ts-ignore
275
+ return instantiate<this>((this._val ** other._val) as T);
276
+ }
277
+
278
+ @inline
279
+ @operator("%")
280
+ rem(other: this): this {
281
+ // @ts-ignore
282
+ return instantiate<this>(this._val % other._val);
283
+ }
284
+
285
+ @inline
286
+ @operator.prefix("!")
287
+ isEmpty(): bool {
288
+ return !this._val;
289
+ }
290
+
291
+ @inline
292
+ @operator.prefix("~")
293
+ not(): this {
294
+ return instantiate<this>(~this._val);
295
+ }
296
+
297
+ @inline
298
+ @operator.prefix("+")
299
+ pos(): this {
300
+ return instantiate<this>(+this._val);
301
+ }
302
+
303
+ @inline
304
+ @operator.prefix("-")
305
+ neg(): this {
306
+ return instantiate<this>(-this._val);
307
+ }
308
+
309
+ @operator.prefix("++")
310
+ preInc(): this {
311
+ // @ts-ignore
312
+ ++this._val;
313
+ return this;
314
+ }
315
+
316
+ @operator.prefix("--")
317
+ preDec(): this {
318
+ // @ts-ignore
319
+ --this._val;
320
+ return this;
321
+ }
322
+
323
+ @operator.postfix("++")
324
+ postInc(): this {
325
+ return this.clone().preInc();
326
+ }
327
+
328
+ @operator.postfix("--")
329
+ postDec(): this {
330
+ return this.clone().preDec();
331
+ }
159
332
  }
160
333
 
161
334
  /**
package/assembly/test.ts CHANGED
@@ -1,29 +1,14 @@
1
1
  // import { JSON } from ".";
2
2
  import { JSON } from ".";
3
- @json
4
- class Message {
5
- @alias("raw_foo")
6
- public raw: JSON.Raw = "[1,2,3]";
7
- constructor(role: string, content: string) {
8
- this._role = role;
9
- this.content = content;
10
- }
11
-
12
- @alias("role")
13
- protected _role: string;
14
-
15
- get role(): string {
16
- return this._role;
17
- }
18
3
 
19
- content: string;
4
+ @json
5
+ class ContentBlock {
6
+ @omitnull()
7
+ input: JSON.Raw | null = null;
20
8
  }
21
9
 
22
- @json
23
- class UserMessage extends Message {
24
- constructor(content: string) {
25
- super("user", content);
26
- }
10
+ const foo: ContentBlock = {
11
+ input: "123"
27
12
  }
28
- console.log(JSON.stringify(new Message("user", "foo")));
29
- console.log(JSON.stringify(new UserMessage("foo")));
13
+
14
+ console.log(JSON.stringify(foo))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.9.19",
3
+ "version": "0.9.21",
4
4
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",