json-as 0.5.7 → 0.5.8

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
@@ -90,6 +86,19 @@ const stringified = JSON.stringify<Player>(data);
90
86
  const parsed = JSON.parse<Player>(stringified);
91
87
  ```
92
88
 
89
+ # FAQ
90
+
91
+ **Does it support the JSON specification?**
92
+ Yes, it does. However, dynamic objects and arrays are not supported, but planned in the near future.
93
+
94
+ **Is it fast?**
95
+ Look below
96
+
97
+ **How does it compare to other librarys?**
98
+ Its pretty much the same as the other libraries out there (near/assemblyscript-json and @serial-as/json), but it focuses highly on performance
99
+
100
+ **Will it catch invalid JSON?**
101
+ No, it does not check for invalid JSON, but gives its best shot at parsing instead. Will probably throw an error.
93
102
 
94
103
  ## Performance
95
104
 
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
  }
@@ -152,15 +154,11 @@ export namespace JSON {
152
154
  throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Invalide data provided.`)
153
155
  }
154
156
  }
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
- }*/
157
+ // @ts-ignore
158
+ @unsafe
159
+ export function createObjectUnsafe<T>(): T {
160
+ return changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))
161
+ }
164
162
  }
165
163
 
166
164
  // @ts-ignore
package/assembly/test.ts CHANGED
@@ -1,15 +1,50 @@
1
- import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console"
2
- import { u128 } from "as-bignum/assembly";
1
+ import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console";
3
2
  import {
4
3
  JSON
5
4
  } from ".";
5
+ @json
6
+ class Player {
7
+ firstName: string;
8
+ lastName: string;
9
+ lastActive: i32[];
10
+ age: i32;
11
+ pos: Vec3 | null;
12
+ isVerified: boolean;
13
+ stats: Stats
14
+ }
15
+
16
+ @json
17
+ class Contacts {
18
+ type: string
19
+ player: string
20
+ }
6
21
 
22
+ const player = JSON.createObjectUnsafe<Player>()
23
+
24
+ player.firstName = "John";
25
+ player.lastName = "West";
26
+ player.age = 23;
27
+
28
+ const contact: Contacts = {
29
+ player: JSON.stringify(player),
30
+ type: "friends"
31
+ }
32
+
33
+ let stringifiedContact = JSON.stringify(contact);
34
+ console.log("Input (Should see backslashes logged): " + stringifiedContact);
35
+ const contacts = JSON.parse<Contacts>(stringifiedContact)
36
+ console.log("Player: " + contacts.player);
37
+ console.log("Type: " + contacts.type);
38
+ const parsedPlayer = JSON.parse<Player>(contacts.player);
39
+ console.log("Final Player: " + JSON.stringify(parsedPlayer));
40
+ console.log("Final Result (Contacts): " + JSON.stringify(contacts));/*
41
+ /*
7
42
  // @ts-ignore
8
43
  @json
9
44
  class Stats {
10
45
  wins: u128
11
46
  loss: u128
12
- }
47
+ }*/
13
48
  // @ts-ignore
14
49
  @json
15
50
  class Vec3 {
@@ -17,7 +52,20 @@ class Vec3 {
17
52
  y: f32;
18
53
  z: f32;
19
54
  }
20
-
55
+ // @ts-ignore
56
+ @json
57
+ class Test {
58
+ data: string
59
+ }
60
+ const vec: Vec3 = {
61
+ x: 3.4,
62
+ y: 1.2,
63
+ z: 8.3
64
+ }
65
+ const test: Test = {
66
+ data: JSON.stringify(vec)
67
+ }
68
+ /*
21
69
  // @ts-ignore
22
70
  @json
23
71
  class Player {
@@ -46,8 +94,10 @@ const player: Player = {
46
94
  loss: u128.fromString("693")
47
95
  }
48
96
  };
49
-
50
- const serializedPlayer = JSON.stringify<Player>(player);
97
+ *//*
98
+ const serializedPlayer = JSON.stringify<Test>(test);
51
99
  wasi_console.log("Serialized Player: " + serializedPlayer);
52
- const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
100
+ const deserializedPlayer = JSON.parse<Test>(serializedPlayer);
53
101
  wasi_console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
102
+ wasi_console.log("Deserialize Vec3: " + JSON.stringify(JSON.parse<Vec3>(deserializedPlayer.data)))
103
+ */
@@ -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.8",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -11,19 +11,24 @@
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",
18
18
  "test:wasm3": "wasm3 ./build/test.wasm",
19
- "prettier": "as-prettier -w ."
19
+ "prettier": "as-prettier -w .",
20
+ "asbuild:debug": "asc assembly/index.ts --target debug",
21
+ "asbuild:release": "asc assembly/index.ts --target release",
22
+ "asbuild": "yarn asbuild:debug && yarn asbuild:release",
23
+ "test": "node tests",
24
+ "start": "npx serve ."
20
25
  },
21
26
  "devDependencies": {
22
27
  "@as-pect/cli": "^7.0.7",
23
- "@as-tral/cli": "^1.1.1",
28
+ "@as-tral/cli": "^1.2.0",
24
29
  "@assemblyscript/loader": "^0.21.3",
25
30
  "@assemblyscript/wasi-shim": "^0.1.0",
26
- "@serial-as/json": "^2.0.0",
31
+ "as-bignum": "^0.2.23",
27
32
  "assemblyscript": "^0.24.1",
28
33
  "assemblyscript-prettier": "^1.0.2",
29
34
  "prettier": "^2.7.1",
@@ -49,5 +54,11 @@
49
54
  "url": "https://github.com/JairusSW/as-json/issues"
50
55
  },
51
56
  "homepage": "https://github.com/JairusSW/as-json#readme",
52
- "type": "module"
57
+ "type": "module",
58
+ "exports": {
59
+ ".": {
60
+ "import": "./build/release.js",
61
+ "types": "./build/release.d.ts"
62
+ }
63
+ }
53
64
  }
@@ -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.8",
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);