json-as 0.5.7 → 0.5.9

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,10 +1,6 @@
1
1
  # AS-JSON
2
2
  ![AssemblyScript](https://img.shields.io/badge/AssemblyScript-blue)
3
3
  ![WebAssembly](https://img.shields.io/badge/WebAssemby-purple)
4
-
5
- ## Features
6
-
7
- Full
8
4
  ## Installation
9
5
 
10
6
  ```bash
@@ -31,7 +27,7 @@ Or, add it to `asconfig.json`
31
27
  ```
32
28
  {
33
29
  "options": {
34
- "transform": "json-as/transform"
30
+ "transform": ["json-as/transform"]
35
31
  }
36
32
  }
37
33
  ```
@@ -40,14 +36,7 @@ Or, add it to `asconfig.json`
40
36
 
41
37
  ```js
42
38
  import { JSON } from "json-as/assembly";
43
- import { u128 } from "as-bignum/assembly";
44
39
 
45
- // @ts-ignore
46
- @json
47
- class Stats {
48
- wins: u128
49
- loss: u128
50
- }
51
40
  // @ts-ignore
52
41
  @json
53
42
  class Vec3 {
@@ -65,7 +54,6 @@ class Player {
65
54
  age: i32;
66
55
  pos: Vec3 | null;
67
56
  isVerified: boolean;
68
- stats: Stats
69
57
  }
70
58
 
71
59
  const player: Player = {
@@ -78,19 +66,36 @@ const player: Player = {
78
66
  y: 1.2,
79
67
  z: 8.3
80
68
  },
81
- isVerified: true,
82
- stats: {
83
- wins: u128.fromString("443"),
84
- loss: u128.fromString("693")
85
- }
69
+ isVerified: true
86
70
  };
87
71
 
88
- const stringified = JSON.stringify<Player>(data);
72
+ const stringified = JSON.stringify<Player>(player);
89
73
 
90
74
  const parsed = JSON.parse<Player>(stringified);
91
75
  ```
92
76
 
77
+ # FAQ
78
+
79
+ **Does it support the JSON specification?**
80
+
81
+ Yes, it does. However, dynamic objects and arrays are not supported, but planned in the near future.
82
+
83
+ **Is it fast?**
84
+
85
+ Look below
86
+
87
+ **How does it compare to other libs?**
88
+
89
+ Its pretty much the same as the other libraries out there (near/assemblyscript-json and @serial-as/json), but it focuses highly on performance
90
+
91
+ **Will it catch invalid JSON?**
92
+
93
+ No, it does not check for invalid JSON, but gives its best shot at parsing instead. Will probably throw an error.
94
+
95
+ **How does it compare performance-wise to other libraries?**
93
96
 
97
+ In my testing, parsing a Vector 2 runs at 2.2m ops/s with as-json and around 10,000 ops/s with assemblyscript-json and @serial-as/json.
98
+ Both are great libraries however.
94
99
  ## Performance
95
100
 
96
101
  **Serialize Object (Vec2):** ~7.20m ops/s
package/asconfig.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "options": {
13
13
  "transform": ["./transform"],
14
14
  "bindings": "esm",
15
- "extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
15
+ "exportStart": "_start"
16
16
  },
17
17
  "extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
18
18
  }
@@ -44,7 +44,7 @@ describe("Ser/de Numbers", () => {
44
44
  canSerde<u128>(u128.from("0"))
45
45
  canSerde<u128>(u128.from("100"))
46
46
  canSerde<u128>(u128.from("101"))
47
-
47
+ `
48
48
  canSerde<u128Safe>(u128Safe.from("0"))
49
49
  canSerde<u128Safe>(u128Safe.from("100"))
50
50
  canSerde<u128Safe>(u128Safe.from("101"))
@@ -81,6 +81,8 @@ export namespace JSON {
81
81
  } else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
82
82
  // @ts-ignore
83
83
  return data.toString();
84
+ } else if (data instanceof Date) {
85
+ return data.toISOString();
84
86
  } else {
85
87
  throw new Error(`Could not serialize data of type ${nameof<T>()}. Invalid data provided.`);
86
88
  }
@@ -116,6 +118,9 @@ export namespace JSON {
116
118
  } else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
117
119
  // @ts-ignore
118
120
  return parseBigNum<T>(data);
121
+ } else if (type instanceof Date) {
122
+ // @ts-ignore
123
+ return Date.fromString(data);
119
124
  } else {
120
125
  // @ts-ignore
121
126
  throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Invalide data provided.`);
@@ -152,15 +157,11 @@ export namespace JSON {
152
157
  throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Invalide data provided.`)
153
158
  }
154
159
  }
155
- /*export class Arr extends Array<Variant> {
156
- public data: Variant[] = [];
157
- push<T>(data: T): i32 {
158
- return this.data.push(Variant.from<T>(data));
159
- }
160
- at<T>(index: i32): T {
161
- return this.data.at(index).get<T>();
162
- }
163
- }*/
160
+ // @ts-ignore
161
+ @unsafe
162
+ export function createObjectUnsafe<T>(): T {
163
+ return changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))
164
+ }
164
165
  }
165
166
 
166
167
  // @ts-ignore
package/assembly/test.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console"
1
+ import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console";
2
2
  import { u128 } from "as-bignum/assembly";
3
3
  import {
4
4
  JSON
@@ -7,33 +7,41 @@ import {
7
7
  // @ts-ignore
8
8
  @json
9
9
  class Stats {
10
- wins: u128
11
- loss: u128
10
+ wins!: u128
11
+ loss!: u128
12
12
  }
13
13
  // @ts-ignore
14
14
  @json
15
15
  class Vec3 {
16
- x: f32;
17
- y: f32;
18
- z: f32;
16
+ x!: f32;
17
+ y!: f32;
18
+ z!: f32;
19
+ }
20
+
21
+ const vec: Vec3 = {
22
+ x: 3.4,
23
+ y: 1.2,
24
+ z: 8.3
19
25
  }
20
26
 
21
27
  // @ts-ignore
22
28
  @json
23
29
  class Player {
24
- firstName: string;
25
- lastName: string;
26
- lastActive: i32[];
27
- age: i32;
28
- pos: Vec3 | null;
29
- isVerified: boolean;
30
- stats: Stats
30
+ firstName!: string;
31
+ lastName!: string;
32
+ lastActive!: i32[];
33
+ createdAt!: Date;
34
+ age!: i32;
35
+ pos!: Vec3 | null;
36
+ isVerified!: boolean;
37
+ stats!: Stats
31
38
  }
32
39
 
33
40
  const player: Player = {
34
41
  firstName: "Emmet",
35
42
  lastName: "West",
36
43
  lastActive: [8, 27, 2022],
44
+ createdAt: Date.fromString("2021-12-08T00:59:26.230Z"),
37
45
  age: 23,
38
46
  pos: {
39
47
  x: 3.4,
@@ -1,9 +1,6 @@
1
- {
2
- "extends": "assemblyscript/std/assembly.json",
3
- "compilerOptions": {
4
- "experimentalDecorators": true
5
- },
6
- "include": [
7
- "./**/*.ts"
8
- ]
1
+ {
2
+ "extends": "assemblyscript/std/assembly.json",
3
+ "include": [
4
+ "./**/*.ts"
5
+ ]
9
6
  }
@@ -0,0 +1,2 @@
1
+ *
2
+ !.gitignore
package/index.html ADDED
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <script type="module">
5
+ import { add } from "./build/release.js";
6
+ document.body.innerText = add(1, 2);
7
+ </script>
8
+ </head>
9
+ <body></body>
10
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.7",
3
+ "version": "0.5.9",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -11,7 +11,7 @@
11
11
  "scripts": {
12
12
  "aspect": "asp",
13
13
  "bench:astral": "astral",
14
- "build:test": "asc assembly/test.ts --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --transform \"./transform\" --exportStart _start -o ./build/test.wasm",
14
+ "build:test": "asc assembly/test.ts --target test",
15
15
  "build:transform": "tsc -p ./transform",
16
16
  "test:wasmtime": "wasmtime ./build/test.wasm",
17
17
  "test:lunatic": "lunatic ./build/test.wasm",
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "devDependencies": {
22
22
  "@as-pect/cli": "^7.0.7",
23
- "@as-tral/cli": "^1.1.1",
23
+ "@as-tral/cli": "^1.2.0",
24
24
  "@assemblyscript/loader": "^0.21.3",
25
25
  "@assemblyscript/wasi-shim": "^0.1.0",
26
- "@serial-as/json": "^2.0.0",
26
+ "as-bignum": "^0.2.23",
27
27
  "assemblyscript": "^0.24.1",
28
28
  "assemblyscript-prettier": "^1.0.2",
29
29
  "prettier": "^2.7.1",
@@ -75,7 +75,7 @@ class AsJSONTransform extends ClassDecorator {
75
75
  }
76
76
  }
77
77
  this.visit(node.members);
78
- const serializedProp = '__JSON_Serialized: string = "";';
78
+ // const serializedProp = '__JSON_Serialized: string = "";';
79
79
  let serializeFunc = "";
80
80
  if (this.currentClass.encodeStmts.length > 0) {
81
81
  const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];
@@ -101,8 +101,11 @@ class AsJSONTransform extends ClassDecorator {
101
101
  }
102
102
  `;
103
103
  //console.log(serializeFunc)
104
- const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
105
- node.members.push(serializedProperty);
104
+ //const serializedProperty = SimpleParser.parseClassMember(
105
+ // serializedProp,
106
+ // node
107
+ //);
108
+ //node.members.push(serializedProperty);
106
109
  const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
107
110
  node.members.push(serializeMethod);
108
111
  const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node);
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.7",
3
+ "version": "0.5.9",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -91,7 +91,7 @@ class AsJSONTransform extends ClassDecorator {
91
91
 
92
92
  this.visit(node.members);
93
93
 
94
- const serializedProp = '__JSON_Serialized: string = "";';
94
+ // const serializedProp = '__JSON_Serialized: string = "";';
95
95
 
96
96
  let serializeFunc = "";
97
97
 
@@ -123,11 +123,11 @@ class AsJSONTransform extends ClassDecorator {
123
123
  }
124
124
  `
125
125
  //console.log(serializeFunc)
126
- const serializedProperty = SimpleParser.parseClassMember(
127
- serializedProp,
128
- node
129
- );
130
- node.members.push(serializedProperty);
126
+ //const serializedProperty = SimpleParser.parseClassMember(
127
+ // serializedProp,
128
+ // node
129
+ //);
130
+ //node.members.push(serializedProperty);
131
131
 
132
132
  const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
133
133
  node.members.push(serializeMethod);