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 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
- No, as-json does not support whitespace yet. That will come once we find a performant way to work around whitespace.
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
- data.age = 16
45
- console.log(`Stringified2: ${JSON.stringify<Player>(data)}`);
46
- const parsed = JSON.parse<Player>(stringified);
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
- export function isNumberCode(data: i32): boolean {
2
- if (data == "0".charCodeAt(0)) return true;
3
- else if (data == "1".charCodeAt(0)) return true;
4
- else if (data == "2".charCodeAt(0)) return true;
5
- else if (data == "3".charCodeAt(0)) return true;
6
- else if (data == "4".charCodeAt(0)) return true;
7
- else if (data == "5".charCodeAt(0)) return true;
8
- else if (data == "6".charCodeAt(0)) return true;
9
- else if (data == "7".charCodeAt(0)) return true;
10
- else if (data == "8".charCodeAt(0)) return true;
11
- else if (data == "9".charCodeAt(0)) return true;
12
- else if (data == "-".charCodeAt(0)) return true;
13
- else return false;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -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);
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -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