json-as 0.5.8 → 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 +12 -16
- package/assembly/src/json.ts +3 -0
- package/assembly/test.ts +21 -63
- package/package.json +3 -14
- package/transform/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ Or, add it to `asconfig.json`
|
|
|
27
27
|
```
|
|
28
28
|
{
|
|
29
29
|
"options": {
|
|
30
|
-
"transform": "json-as/transform"
|
|
30
|
+
"transform": ["json-as/transform"]
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
```
|
|
@@ -36,14 +36,7 @@ Or, add it to `asconfig.json`
|
|
|
36
36
|
|
|
37
37
|
```js
|
|
38
38
|
import { JSON } from "json-as/assembly";
|
|
39
|
-
import { u128 } from "as-bignum/assembly";
|
|
40
39
|
|
|
41
|
-
// @ts-ignore
|
|
42
|
-
@json
|
|
43
|
-
class Stats {
|
|
44
|
-
wins: u128
|
|
45
|
-
loss: u128
|
|
46
|
-
}
|
|
47
40
|
// @ts-ignore
|
|
48
41
|
@json
|
|
49
42
|
class Vec3 {
|
|
@@ -61,7 +54,6 @@ class Player {
|
|
|
61
54
|
age: i32;
|
|
62
55
|
pos: Vec3 | null;
|
|
63
56
|
isVerified: boolean;
|
|
64
|
-
stats: Stats
|
|
65
57
|
}
|
|
66
58
|
|
|
67
59
|
const player: Player = {
|
|
@@ -74,14 +66,10 @@ const player: Player = {
|
|
|
74
66
|
y: 1.2,
|
|
75
67
|
z: 8.3
|
|
76
68
|
},
|
|
77
|
-
isVerified: true
|
|
78
|
-
stats: {
|
|
79
|
-
wins: u128.fromString("443"),
|
|
80
|
-
loss: u128.fromString("693")
|
|
81
|
-
}
|
|
69
|
+
isVerified: true
|
|
82
70
|
};
|
|
83
71
|
|
|
84
|
-
const stringified = JSON.stringify<Player>(
|
|
72
|
+
const stringified = JSON.stringify<Player>(player);
|
|
85
73
|
|
|
86
74
|
const parsed = JSON.parse<Player>(stringified);
|
|
87
75
|
```
|
|
@@ -89,17 +77,25 @@ const parsed = JSON.parse<Player>(stringified);
|
|
|
89
77
|
# FAQ
|
|
90
78
|
|
|
91
79
|
**Does it support the JSON specification?**
|
|
80
|
+
|
|
92
81
|
Yes, it does. However, dynamic objects and arrays are not supported, but planned in the near future.
|
|
93
82
|
|
|
94
83
|
**Is it fast?**
|
|
84
|
+
|
|
95
85
|
Look below
|
|
96
86
|
|
|
97
|
-
**How does it compare to other
|
|
87
|
+
**How does it compare to other libs?**
|
|
88
|
+
|
|
98
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
|
|
99
90
|
|
|
100
91
|
**Will it catch invalid JSON?**
|
|
92
|
+
|
|
101
93
|
No, it does not check for invalid JSON, but gives its best shot at parsing instead. Will probably throw an error.
|
|
102
94
|
|
|
95
|
+
**How does it compare performance-wise to other libraries?**
|
|
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.
|
|
103
99
|
## Performance
|
|
104
100
|
|
|
105
101
|
**Serialize Object (Vec2):** ~7.20m ops/s
|
package/assembly/src/json.ts
CHANGED
|
@@ -118,6 +118,9 @@ export namespace JSON {
|
|
|
118
118
|
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
|
|
119
119
|
// @ts-ignore
|
|
120
120
|
return parseBigNum<T>(data);
|
|
121
|
+
} else if (type instanceof Date) {
|
|
122
|
+
// @ts-ignore
|
|
123
|
+
return Date.fromString(data);
|
|
121
124
|
} else {
|
|
122
125
|
// @ts-ignore
|
|
123
126
|
throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Invalide data provided.`);
|
package/assembly/test.ts
CHANGED
|
@@ -1,87 +1,47 @@
|
|
|
1
1
|
import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console";
|
|
2
|
+
import { u128 } from "as-bignum/assembly";
|
|
2
3
|
import {
|
|
3
4
|
JSON
|
|
4
5
|
} 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
|
-
}
|
|
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
6
|
|
|
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
|
-
/*
|
|
42
7
|
// @ts-ignore
|
|
43
8
|
@json
|
|
44
9
|
class Stats {
|
|
45
|
-
wins
|
|
46
|
-
loss
|
|
47
|
-
}*/
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
@json
|
|
50
|
-
class Vec3 {
|
|
51
|
-
x: f32;
|
|
52
|
-
y: f32;
|
|
53
|
-
z: f32;
|
|
10
|
+
wins!: u128
|
|
11
|
+
loss!: u128
|
|
54
12
|
}
|
|
55
13
|
// @ts-ignore
|
|
56
14
|
@json
|
|
57
|
-
class
|
|
58
|
-
|
|
15
|
+
class Vec3 {
|
|
16
|
+
x!: f32;
|
|
17
|
+
y!: f32;
|
|
18
|
+
z!: f32;
|
|
59
19
|
}
|
|
20
|
+
|
|
60
21
|
const vec: Vec3 = {
|
|
61
22
|
x: 3.4,
|
|
62
23
|
y: 1.2,
|
|
63
24
|
z: 8.3
|
|
64
25
|
}
|
|
65
|
-
|
|
66
|
-
data: JSON.stringify(vec)
|
|
67
|
-
}
|
|
68
|
-
/*
|
|
26
|
+
|
|
69
27
|
// @ts-ignore
|
|
70
28
|
@json
|
|
71
29
|
class Player {
|
|
72
|
-
firstName
|
|
73
|
-
lastName
|
|
74
|
-
lastActive
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
|
79
38
|
}
|
|
80
39
|
|
|
81
40
|
const player: Player = {
|
|
82
41
|
firstName: "Emmet",
|
|
83
42
|
lastName: "West",
|
|
84
43
|
lastActive: [8, 27, 2022],
|
|
44
|
+
createdAt: Date.fromString("2021-12-08T00:59:26.230Z"),
|
|
85
45
|
age: 23,
|
|
86
46
|
pos: {
|
|
87
47
|
x: 3.4,
|
|
@@ -94,10 +54,8 @@ const player: Player = {
|
|
|
94
54
|
loss: u128.fromString("693")
|
|
95
55
|
}
|
|
96
56
|
};
|
|
97
|
-
|
|
98
|
-
const serializedPlayer = JSON.stringify<
|
|
57
|
+
|
|
58
|
+
const serializedPlayer = JSON.stringify<Player>(player);
|
|
99
59
|
wasi_console.log("Serialized Player: " + serializedPlayer);
|
|
100
|
-
const deserializedPlayer = JSON.parse<
|
|
60
|
+
const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
|
|
101
61
|
wasi_console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
|
|
102
|
-
wasi_console.log("Deserialize Vec3: " + JSON.stringify(JSON.parse<Vec3>(deserializedPlayer.data)))
|
|
103
|
-
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.9",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -16,12 +16,7 @@
|
|
|
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 ."
|
|
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 ."
|
|
19
|
+
"prettier": "as-prettier -w ."
|
|
25
20
|
},
|
|
26
21
|
"devDependencies": {
|
|
27
22
|
"@as-pect/cli": "^7.0.7",
|
|
@@ -54,11 +49,5 @@
|
|
|
54
49
|
"url": "https://github.com/JairusSW/as-json/issues"
|
|
55
50
|
},
|
|
56
51
|
"homepage": "https://github.com/JairusSW/as-json#readme",
|
|
57
|
-
"type": "module"
|
|
58
|
-
"exports": {
|
|
59
|
-
".": {
|
|
60
|
-
"import": "./build/release.js",
|
|
61
|
-
"types": "./build/release.d.ts"
|
|
62
|
-
}
|
|
63
|
-
}
|
|
52
|
+
"type": "module"
|
|
64
53
|
}
|