json-as 0.2.4 → 0.4.0
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/LICENSE +1 -1
- package/README.md +52 -99
- package/asconfig.json +10 -35
- package/assembly/__benches__/as-tral.d.ts +1 -0
- package/assembly/__benches__/benchmark.ts +69 -0
- package/assembly/chars.ts +16 -0
- package/assembly/index.ts +337 -757
- package/assembly/test.ts +35 -192
- package/assembly/tsconfig.json +9 -0
- package/index.ts +1 -0
- package/package.json +26 -37
- package/tests/index.js +3 -3
- package/tests/test.js +13 -6
- package/transform/lib/index.js +47 -150
- package/transform/package.json +30 -2
- package/transform/src/index.old.js +430 -0
- package/transform/src/index.ts +98 -0
- package/transform/tsconfig.json +68 -69
- package/assembly/DynamicObject.ts +0 -29
- package/assembly/bench.ts +0 -199
- package/assembly/unknown.ts +0 -175
- package/bench/bench.js +0 -102
- package/transform/index.ts +0 -223
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2022 Jairus Tanaka <jairus.v.tanaka@outlook.com>
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,126 +1,79 @@
|
|
|
1
1
|
# AS-JSON
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
**JSON serializer/deserializer for AssemblyScript**
|
|
3
4
|
|
|
4
5
|
## Installation
|
|
5
6
|
|
|
6
7
|
```bash
|
|
7
8
|
~ npm install json-as
|
|
9
|
+
~ yarn add json-as
|
|
10
|
+
~ bun install json-as
|
|
8
11
|
```
|
|
12
|
+
|
|
13
|
+
Add the transform to your `asc` command
|
|
14
|
+
|
|
9
15
|
```bash
|
|
10
16
|
--transform json-as/transform
|
|
11
17
|
```
|
|
12
18
|
|
|
13
|
-
|
|
14
|
-
- ✅ Objects
|
|
15
|
-
- ✅ Arrays
|
|
16
|
-
- ✅ Numbers
|
|
17
|
-
- ✅ Integers
|
|
18
|
-
- ✅ Null
|
|
19
|
-
- ✅ Dynamic Arrays
|
|
20
|
-
- ✅ Dynamic Types
|
|
21
|
-
- ✅ Dynamic Objects
|
|
22
|
-
- ✅ Whitespace
|
|
23
|
-
|
|
24
|
-
## Usage
|
|
19
|
+
Or, add it to `asconfig.json`
|
|
25
20
|
|
|
26
|
-
```js
|
|
27
|
-
import { JSON } from 'json-as'
|
|
28
21
|
```
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class JSONSchema {
|
|
34
|
-
firstName: string
|
|
35
|
-
lastName: string
|
|
36
|
-
age: i32
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const data: JSONSchema {
|
|
40
|
-
firstName: 'Jairus',
|
|
41
|
-
lastName: 'Tanaka',
|
|
42
|
-
age: 14
|
|
22
|
+
{
|
|
23
|
+
"options": {
|
|
24
|
+
"transform": "json-as/transform"
|
|
25
|
+
}
|
|
43
26
|
}
|
|
44
|
-
|
|
45
|
-
const stringified = JSON.stringify(data)
|
|
46
|
-
// '{"firstName":"Jairus","lastName":"Tanaka","age":14}'
|
|
47
|
-
|
|
48
|
-
const parsed = JSON.parse<JSONSchema>(stringified)
|
|
49
|
-
// { firstName: "Jairus", lastName: "Tanaka", age: 14 }
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
**Array**
|
|
53
|
-
|
|
54
|
-
```js
|
|
55
|
-
const stringified = JSON.stringify(['Hello', 'World'])
|
|
56
|
-
// '["Hello","World"]'
|
|
57
|
-
|
|
58
|
-
const parsed = JSON.parse<JSONSchema>(stringified)
|
|
59
|
-
// ["Hello", "World"]
|
|
60
27
|
```
|
|
61
28
|
|
|
62
|
-
|
|
29
|
+
## Support
|
|
63
30
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
31
|
+
- ✅ Objects (Supported)
|
|
32
|
+
- ✅ Arrays (Supported)
|
|
33
|
+
- ✅ Numbers (Supported)
|
|
34
|
+
- ✅ Integers (Supported)
|
|
35
|
+
- ✅ Null (Supported)
|
|
36
|
+
- ❌ Dynamic Variants (Not supported)
|
|
67
37
|
|
|
68
|
-
|
|
69
|
-
// 3.14
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
**Integer**
|
|
73
|
-
|
|
74
|
-
```js
|
|
75
|
-
const stringified = JSON.stringify(14)
|
|
76
|
-
// '14'
|
|
77
|
-
|
|
78
|
-
const parsed = JSON.parse<i32>(stringified)
|
|
79
|
-
// 14
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
**Boolean**
|
|
38
|
+
## Usage
|
|
83
39
|
|
|
84
40
|
```js
|
|
85
|
-
|
|
86
|
-
// 'true'
|
|
41
|
+
import { JSON } from "json-as";
|
|
87
42
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
43
|
+
@json
|
|
44
|
+
class Vec2 {
|
|
45
|
+
x: f32
|
|
46
|
+
y: f32
|
|
47
|
+
}
|
|
48
|
+
@json
|
|
49
|
+
class Player {
|
|
50
|
+
firstName: string
|
|
51
|
+
lastName: string
|
|
52
|
+
lastActive: i32[]
|
|
53
|
+
age: i32
|
|
54
|
+
pos: Vec2
|
|
55
|
+
}
|
|
91
56
|
|
|
92
|
-
|
|
57
|
+
const data: Player = {
|
|
58
|
+
firstName: "Emmet",
|
|
59
|
+
lastName: "West",
|
|
60
|
+
lastActive: [8, 27, 2022],
|
|
61
|
+
age: 23,
|
|
62
|
+
pos: {
|
|
63
|
+
x: -3.4,
|
|
64
|
+
y: 1.2
|
|
65
|
+
}
|
|
66
|
+
}
|
|
93
67
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
68
|
+
const stringified = JSON.stringify<Player>(data);
|
|
69
|
+
// '{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23}'
|
|
70
|
+
console.log(`Stringified: ${stringified}`);
|
|
97
71
|
|
|
98
|
-
const parsed = JSON.parse<
|
|
99
|
-
//
|
|
72
|
+
const parsed = JSON.parse<Player>(stringified)
|
|
73
|
+
// { firstName: "Emmet", lastName: "West", "lastActive": [8,27,2022], age: 23 }
|
|
74
|
+
console.log(`Parsed: ${JSON.stringify(parsed)}`)
|
|
100
75
|
```
|
|
101
76
|
|
|
102
|
-
|
|
77
|
+
## Issues
|
|
103
78
|
|
|
104
|
-
|
|
105
|
-
const stringified = JSON.stringify(null)
|
|
106
|
-
// 'null'
|
|
107
|
-
|
|
108
|
-
const parsed = JSON.parse(stringified)
|
|
109
|
-
// null
|
|
110
|
-
```
|
|
111
|
-
## Benchmarks
|
|
112
|
-
|
|
113
|
-
```
|
|
114
|
-
AS-JSON Stringify String: ~4191267.51 ops/s | 23.86ms
|
|
115
|
-
AS-JSON Parse String: ~6218119.99 ops/s | 16.08ms
|
|
116
|
-
AS-JSON Stringify Integer: ~13775012.61 ops/s | 7.26ms
|
|
117
|
-
AS-JSON Parse Integer: ~55061164.13 ops/s | 1.82ms
|
|
118
|
-
AS-JSON Stringify Float: ~7739399.89 ops/s | 12.92ms
|
|
119
|
-
AS-JSON Parse Float: ~37522902.16 ops/s | 2.67ms
|
|
120
|
-
AS-JSON Stringify Boolean: ~615015015.02 ops/s | 0.16ms
|
|
121
|
-
AS-JSON Parse Boolean: ~93901879.87 ops/s | 1.06ms
|
|
122
|
-
AS-JSON Stringify Array: ~2380329.74 ops/s | 42.01ms
|
|
123
|
-
AS-JSON Parse Array: ~6258786.14 ops/s | 15.98ms
|
|
124
|
-
AS-JSON Stringify Object: ~5245632.91 ops/s | 19.06ms
|
|
125
|
-
AS-JSON Parse Object: ~1328576.06 ops/s | 75.27ms
|
|
126
|
-
```
|
|
79
|
+
Please submit an issue to https://github.com/JairusSW/as-json/issues if you find anything wrong with this library
|
package/asconfig.json
CHANGED
|
@@ -1,50 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"targets": {
|
|
3
3
|
"debug": {
|
|
4
|
-
"
|
|
5
|
-
"textFile": "build/
|
|
4
|
+
"outFile": "build/debug.wasm",
|
|
5
|
+
"textFile": "build/debug.wat",
|
|
6
6
|
"sourceMap": true,
|
|
7
7
|
"debug": true
|
|
8
8
|
},
|
|
9
9
|
"release": {
|
|
10
|
-
"
|
|
11
|
-
"textFile": "build/
|
|
12
|
-
"sourceMap": true,
|
|
13
|
-
"optimizeLevel": 0,
|
|
14
|
-
"shrinkLevel": 0,
|
|
15
|
-
"converge": false,
|
|
16
|
-
"noAssert": false
|
|
17
|
-
},
|
|
18
|
-
"test": {
|
|
19
|
-
"binaryFile": "tests/output/test.wasm",
|
|
20
|
-
"textFile": "tests/output/test.wat",
|
|
21
|
-
"sourceMap": true,
|
|
22
|
-
"optimizeLevel": 0,
|
|
23
|
-
"shrinkLevel": 0,
|
|
24
|
-
"converge": false,
|
|
25
|
-
"noAssert": false
|
|
26
|
-
},
|
|
27
|
-
"bench": {
|
|
28
|
-
"binaryFile": "bench/output/bench.wasm",
|
|
29
|
-
"textFile": "bench/output/bench.wat",
|
|
30
|
-
"sourceMap": true,
|
|
31
|
-
"optimizeLevel": 3,
|
|
32
|
-
"shrinkLevel": 0,
|
|
33
|
-
"converge": false,
|
|
34
|
-
"noAssert": false
|
|
35
|
-
},
|
|
36
|
-
"dynamic:bench": {
|
|
37
|
-
"binaryFile": "dynamic/bench/output/bench.wasm",
|
|
38
|
-
"textFile": "dynamic/bench/output/bench.wat",
|
|
10
|
+
"outFile": "build/release.wasm",
|
|
11
|
+
"textFile": "build/release.wat",
|
|
39
12
|
"sourceMap": true,
|
|
40
13
|
"optimizeLevel": 3,
|
|
41
14
|
"shrinkLevel": 0,
|
|
42
15
|
"converge": false,
|
|
43
16
|
"noAssert": false
|
|
44
17
|
},
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"textFile": "dynamic/test/output/test.wat",
|
|
18
|
+
"test": {
|
|
19
|
+
"outFile": "build/test.wasm",
|
|
48
20
|
"sourceMap": true,
|
|
49
21
|
"optimizeLevel": 0,
|
|
50
22
|
"shrinkLevel": 0,
|
|
@@ -52,5 +24,8 @@
|
|
|
52
24
|
"noAssert": false
|
|
53
25
|
}
|
|
54
26
|
},
|
|
55
|
-
"options": {
|
|
27
|
+
"options": {
|
|
28
|
+
"transform": "./transform",
|
|
29
|
+
"bindings": "esm"
|
|
30
|
+
}
|
|
56
31
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference path="../../node_modules/@as-tral/cli/as-tral.d.ts" />
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { JSON, parseBooleanArray, parseMap, parseNumberArray } from "..";
|
|
2
|
+
@json
|
|
3
|
+
class Vector {
|
|
4
|
+
x: f32;
|
|
5
|
+
y: f32;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const vec: Vector = blackbox<Vector>({
|
|
9
|
+
x: 0.0,
|
|
10
|
+
y: 0.0,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
bench("Stringify String", () => {
|
|
14
|
+
blackbox(JSON.stringify(blackbox("Hello")));
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
bench("Stringify Boolean", () => {
|
|
18
|
+
blackbox(JSON.stringify(blackbox(true)));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
bench("Stringify Integer", () => {
|
|
22
|
+
blackbox(JSON.stringify(blackbox(314)));
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
bench("Stringify Float", () => {
|
|
26
|
+
blackbox(JSON.stringify(blackbox(3.14)));
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
bench("Stringify Vector", () => {
|
|
30
|
+
blackbox(JSON.stringify(vec));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
bench("Stringify Array", () => {
|
|
34
|
+
blackbox(JSON.stringify(blackbox([1, 2, 3, 4, 5])));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
bench("Parse String", () => {
|
|
38
|
+
blackbox(JSON.parse<string>(blackbox('"Hello"')));
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
bench("Parse Boolean", () => {
|
|
42
|
+
blackbox(JSON.parse<boolean>(blackbox("true")));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
bench("Parse Integer", () => {
|
|
46
|
+
blackbox(JSON.parse<i32>(blackbox("314")));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
bench("Parse Float", () => {
|
|
50
|
+
blackbox(JSON.parse<f32>(blackbox("3.14")));
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
bench("Parse Vector", () => {
|
|
54
|
+
blackbox(parseMap<Map<string, f32>>(blackbox('{"x":0.0,"y":0.0}')));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
bench("Parse Boolean Array", () => {
|
|
58
|
+
blackbox(
|
|
59
|
+
parseBooleanArray<boolean[]>(blackbox("[true,false,true,false,true]"))
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
bench("Parse Integer Array", () => {
|
|
64
|
+
blackbox(parseNumberArray<u32[]>(blackbox("[1,2,3,4,5]")));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
bench("Parse Float Array", () => {
|
|
68
|
+
blackbox(parseNumberArray<f32[]>(blackbox("[1.0,2.0,3.0,4.0,5.0]")));
|
|
69
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const commaCode = ",".charCodeAt(0);
|
|
2
|
+
export const quoteCode = "".charCodeAt(0);
|
|
3
|
+
export const backSlashCode = "\\".charCodeAt(0);
|
|
4
|
+
export const leftBraceCode = "{".charCodeAt(0);
|
|
5
|
+
export const rightBraceCode = "}".charCodeAt(0);
|
|
6
|
+
export const leftBracketCode = "[".charCodeAt(0);
|
|
7
|
+
export const rightBracketCode = "]".charCodeAt(0);
|
|
8
|
+
export const colonCode = ":".charCodeAt(0);
|
|
9
|
+
export const tCode = "t".charCodeAt(0);
|
|
10
|
+
export const rCode = "r".charCodeAt(0);
|
|
11
|
+
export const uCode = "u".charCodeAt(0);
|
|
12
|
+
export const eCode = "e".charCodeAt(0);
|
|
13
|
+
export const fCode = "f".charCodeAt(0);
|
|
14
|
+
export const aCode = "a".charCodeAt(0);
|
|
15
|
+
export const lCode = "l".charCodeAt(0);
|
|
16
|
+
export const sCode = "s".charCodeAt(0);
|