json-as 0.2.6 → 0.4.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Jairus Tanaka <jairus.v.tanaka@outlook.com>
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,5 +1,6 @@
1
1
  # AS-JSON
2
- **JSON encoder/decoder for AssemblyScript**
2
+
3
+ **JSON serializer/deserializer for AssemblyScript**
3
4
 
4
5
  ## Installation
5
6
 
@@ -7,120 +8,73 @@
7
8
  ~ npm install json-as
8
9
  ```
9
10
  ```bash
10
- --transform json-as/transform
11
- ```
12
-
13
- ## Support
14
- - ✅ Objects
15
- - ✅ Arrays
16
- - ✅ Numbers
17
- - ✅ Integers
18
- - ✅ Null
19
- - ✅ Dynamic Arrays
20
- - ✅ Dynamic Types
21
- - ✅ Dynamic Objects
22
- - ✅ Whitespace
23
-
24
- ## Usage
25
-
26
- ```js
27
- import { JSON } from 'json-as'
11
+ ~ npm install visitor-as
28
12
  ```
29
13
 
30
- **Object**
31
- ```js
32
- @json
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
43
- }
14
+ Add the transform to your `asc` command
44
15
 
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 }
16
+ ```bash
17
+ --transform json-as/transform
50
18
  ```
51
19
 
52
- **Array**
53
-
54
- ```js
55
- const stringified = JSON.stringify(['Hello', 'World'])
56
- // '["Hello","World"]'
20
+ Or, add it to `asconfig.json`
57
21
 
58
- const parsed = JSON.parse<JSONSchema>(stringified)
59
- // ["Hello", "World"]
60
22
  ```
61
-
62
- **Float**
63
-
64
- ```js
65
- const stringified = JSON.stringify(3.14)
66
- // '3.14'
67
-
68
- const parsed = JSON.parse<f64>(stringified)
69
- // 3.14
23
+ {
24
+ "options": {
25
+ "transform": "json-as/transform"
26
+ }
27
+ }
70
28
  ```
71
29
 
72
- **Integer**
73
-
74
- ```js
75
- const stringified = JSON.stringify(14)
76
- // '14'
30
+ ## Support
77
31
 
78
- const parsed = JSON.parse<i32>(stringified)
79
- // 14
80
- ```
32
+ - Objects (Supported)
33
+ - ✅ Arrays (Supported)
34
+ - ✅ Numbers (Supported)
35
+ - ✅ Integers (Supported)
36
+ - ✅ Null (Supported)
37
+ - ❌ Dynamic Variants (Not supported)
81
38
 
82
- **Boolean**
39
+ ## Usage
83
40
 
84
41
  ```js
85
- const stringified = JSON.stringify(true)
86
- // 'true'
42
+ import { JSON } from "json-as";
87
43
 
88
- const parsed = JSON.parse<boolean>(stringified)
89
- // true
90
- ```
44
+ @json
45
+ class Vec2 {
46
+ x: f32
47
+ y: f32
48
+ }
49
+ @json
50
+ class Player {
51
+ firstName: string
52
+ lastName: string
53
+ lastActive: i32[]
54
+ age: i32
55
+ pos: Vec2
56
+ }
91
57
 
92
- **Bool**
58
+ const data: Player = {
59
+ firstName: "Emmet",
60
+ lastName: "West",
61
+ lastActive: [8, 27, 2022],
62
+ age: 23,
63
+ pos: {
64
+ x: -3.4,
65
+ y: 1.2
66
+ }
67
+ }
93
68
 
94
- ```js
95
- const stringified = JSON.stringify(true)
96
- // 'true'
69
+ const stringified = JSON.stringify<Player>(data);
70
+ // '{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23}'
71
+ console.log(`Stringified: ${stringified}`);
97
72
 
98
- const parsed = JSON.parse<bool>(stringified)
99
- // true
73
+ const parsed = JSON.parse<Player>(stringified)
74
+ // { firstName: "Emmet", lastName: "West", "lastActive": [8,27,2022], age: 23 }
75
+ console.log(`Parsed: ${JSON.stringify(parsed)}`)
100
76
  ```
101
77
 
102
- **Null**
78
+ ## Issues
103
79
 
104
- ```js
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
- ```
80
+ 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
- "binaryFile": "build/untouched.wasm",
5
- "textFile": "build/untouched.wat",
4
+ "outFile": "build/debug.wasm",
5
+ "textFile": "build/debug.wat",
6
6
  "sourceMap": true,
7
7
  "debug": true
8
8
  },
9
9
  "release": {
10
- "binaryFile": "build/optimized.wasm",
11
- "textFile": "build/optimized.wat",
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
- "dynamic:test": {
46
- "binaryFile": "dynamic/test/output/test.wasm",
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);