json-as 0.4.3 → 0.4.4
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 +1 -1
- package/assembly/index.ts +2 -0
- package/assembly/test.ts +21 -3
- package/assembly/util.ts +20 -13
- package/package.json +1 -1
- package/transform/lib/index.js +1 -1
- package/transform/package.json +1 -1
- package/transform/src/index.ts +1 -1
package/README.md
CHANGED
|
@@ -102,7 +102,7 @@ console.log(`Parsed: ${JSON.stringify(parsed)}`);
|
|
|
102
102
|
- Does this support nested structures?
|
|
103
103
|
Yes, as-json supports nested structures
|
|
104
104
|
- Does this support whitespace?
|
|
105
|
-
|
|
105
|
+
Yes, as-json supports whitespace although the current implementation is deathly slow
|
|
106
106
|
- How fast is it?
|
|
107
107
|
Really fast. For example, here are some benchmarks for ser/de a Vec2 with as-json
|
|
108
108
|
## Issues
|
package/assembly/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
rightBraceCode,
|
|
11
11
|
rightBracketCode
|
|
12
12
|
} from "./chars";
|
|
13
|
+
import { removeWhitespace } from "./util";
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* JSON Encoder/Decoder for AssemblyScript
|
|
@@ -77,6 +78,7 @@ export namespace JSON {
|
|
|
77
78
|
* @returns T
|
|
78
79
|
*/
|
|
79
80
|
export function parse<T = Variant>(data: string): T {
|
|
81
|
+
data = removeWhitespace(data);
|
|
80
82
|
let type!: T;
|
|
81
83
|
if (isString<T>()) {
|
|
82
84
|
// @ts-ignore
|
package/assembly/test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import "wasi";
|
|
2
2
|
import { JSON } from ".";
|
|
3
|
+
import { removeWhitespace } from "./util";
|
|
3
4
|
|
|
4
5
|
// @ts-ignore
|
|
5
6
|
@json
|
|
@@ -41,9 +42,26 @@ const stringified = JSON.stringify<Player>(data);
|
|
|
41
42
|
// }
|
|
42
43
|
// }
|
|
43
44
|
console.log(`Stringified: ${stringified}`);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
console.log(`Whitespace: ${removeWhitespace(`{
|
|
46
|
+
"firstName": "Emmet",
|
|
47
|
+
"lastName": "West",
|
|
48
|
+
"lastActive": [8, 27, 2022],
|
|
49
|
+
"age": 23,
|
|
50
|
+
"pos": {
|
|
51
|
+
"x": -3.4000000953674318,
|
|
52
|
+
"y": 1.2000000476837159
|
|
53
|
+
}
|
|
54
|
+
}`)}`)
|
|
55
|
+
const parsed = JSON.parse<Player>(`{
|
|
56
|
+
"firstName": "Emmet",
|
|
57
|
+
"lastName": "West",
|
|
58
|
+
"lastActive": [8, 27, 2022],
|
|
59
|
+
"age": 23,
|
|
60
|
+
"pos": {
|
|
61
|
+
"x": -3.4000000953674318,
|
|
62
|
+
"y": 1.2000000476837159
|
|
63
|
+
}
|
|
64
|
+
}`);
|
|
47
65
|
// Player {
|
|
48
66
|
// firstName: "Emmet",
|
|
49
67
|
// lastName: "West",
|
package/assembly/util.ts
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { StringSink } from "as-string-sink/assembly";
|
|
2
|
+
import { isSpace } from "assemblyscript/std/assembly/util/string";
|
|
3
|
+
import { backSlashCode, quoteCode } from "./chars";
|
|
4
|
+
|
|
5
|
+
export function removeWhitespace(data: string): string {
|
|
6
|
+
const result = new StringSink()
|
|
7
|
+
let instr = false;
|
|
8
|
+
for (let i = 0; i < data.length; i++) {
|
|
9
|
+
const char = data.charCodeAt(i);
|
|
10
|
+
if (instr === false && char === quoteCode) instr = true
|
|
11
|
+
else if (instr === true && char === quoteCode && data.charCodeAt(i - 1) !== backSlashCode) instr = false;
|
|
12
|
+
|
|
13
|
+
if (instr === false) {
|
|
14
|
+
if (!isSpace(char)) result.write(data.charAt(i))
|
|
15
|
+
} else {
|
|
16
|
+
result.write(data.charAt(i))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
return result.toString()
|
|
14
21
|
}
|
package/package.json
CHANGED
package/transform/lib/index.js
CHANGED
|
@@ -58,7 +58,7 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
58
58
|
`;
|
|
59
59
|
this.encodeStmts = [];
|
|
60
60
|
this.decodeStmts = [];
|
|
61
|
-
console.log(serializeFunc, deserializeFunc)
|
|
61
|
+
//console.log(serializeFunc, deserializeFunc)
|
|
62
62
|
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
|
|
63
63
|
node.members.push(serializedProperty);
|
|
64
64
|
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -77,7 +77,7 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
77
77
|
`;
|
|
78
78
|
this.encodeStmts = [];
|
|
79
79
|
this.decodeStmts = [];
|
|
80
|
-
console.log(serializeFunc, deserializeFunc)
|
|
80
|
+
//console.log(serializeFunc, deserializeFunc)
|
|
81
81
|
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
|
|
82
82
|
node.members.push(serializedProperty);
|
|
83
83
|
|