json-as 0.4.2 → 0.4.5

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/assembly/test.ts CHANGED
@@ -1,18 +1,29 @@
1
1
  import "wasi";
2
- import { JSON } from ".";
2
+ import {
3
+ JSON,
4
+ parseArrayArray,
5
+ parseNumberArray,
6
+ parseObject,
7
+ parseObjectArray,
8
+ } from ".";
9
+ import { removeWhitespace } from "./util";
3
10
 
11
+ // @ts-ignore
4
12
  @json
5
13
  class Vec2 {
6
- x: f32
7
- y: f32
14
+ x: f32;
15
+ y: f32;
8
16
  }
17
+
18
+ // @ts-ignore
9
19
  @json
10
20
  class Player {
11
- firstName: string
12
- lastName: string
13
- lastActive: i32[]
14
- age: i32
15
- pos: Vec2
21
+ firstName: string;
22
+ lastName: string;
23
+ lastActive: i32[];
24
+ age: i32;
25
+ pos: Vec2;
26
+ isVerified: boolean;
16
27
  }
17
28
 
18
29
  const data: Player = {
@@ -22,14 +33,53 @@ const data: Player = {
22
33
  age: 23,
23
34
  pos: {
24
35
  x: -3.4,
25
- y: 1.2
26
- }
27
- }
36
+ y: 1.2,
37
+ },
38
+ isVerified: true,
39
+ };
40
+
41
+ const serialized = JSON.stringify<Player>(data);
42
+ console.log("Serialized: " + serialized);
43
+ const deserialized = JSON.parse<Player>(serialized);
44
+ console.log("Deserialized: " + JSON.stringify(deserialized));
45
+ /*
46
+ const parsed = JSON.parse<Player>(stringified);
47
+ console.log("Vec2 Parse: " + JSON.stringify<Player>(parsed));
48
+ console.log(
49
+ `Parsed String Array: ${JSON.stringify(
50
+ JSON.parse<string[]>(`\n[ "hello" , "world" ] `)
51
+ )}`
52
+ );
53
+
54
+ console.log(
55
+ `Parsed Boolean Array: ${JSON.stringify(
56
+ JSON.parse<boolean[]>(`\n[ false , true ] `)
57
+ )}`
58
+ );
59
+
60
+ console.log(
61
+ `Parsed Number Array: ${JSON.stringify(
62
+ JSON.parse<i32[]>(`[ 1 , 2\n ,3\n\t ]`)
63
+ )}`
64
+ );
65
+
66
+ console.log(
67
+ JSON.stringify<Vec2>(
68
+ load<Vec2>(changetype<usize>(data), offsetof<Player>("pos"))
69
+ )
70
+ );
28
71
 
29
- const stringified = JSON.stringify<Player>(data);
30
- // '{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23}'
31
- console.log(`Stringified: ${stringified}`);
72
+ console.log(
73
+ JSON.stringify<string[][]>(
74
+ parseArrayArray<string[][]>('[["a","b","c"],["d","e","f"]]')
75
+ )
76
+ );
32
77
 
33
- const parsed = JSON.parse<Player>(stringified)
34
- // { firstName: "Emmet", lastName: "West", "lastActive": [8,27,2022], age: 23 }
35
- console.log(`Parsed: ${JSON.stringify(parsed)}`)
78
+ console.log(
79
+ JSON.stringify<Player[][]>(
80
+ parseObjectArray<Player[][]>(
81
+ '[{"firstName":"Emmet","lastName":"West","lastActive":[8,7],"age":23,"pos":{"x":-3.4000000953674318,"y":1.2000000476837159}}]'
82
+ )
83
+ )
84
+ );
85
+ */
File without changes
@@ -0,0 +1,30 @@
1
+ import { StringSink } from "as-string-sink/assembly";
2
+ import { isSpace } from "assemblyscript/std/assembly/util/string";
3
+ import { backSlashCode, quoteCode } from "./chars";
4
+ // @ts-ignore
5
+ @inline
6
+ export function unsafeCharCodeAt(data: string, pos: i32): i32 {
7
+ return load<u16>(changetype<usize>(data) + ((<usize>pos) << 1));
8
+ }
9
+
10
+ export function removeWhitespace(data: string): string {
11
+ const result = new StringSink();
12
+ let instr = false;
13
+ for (let i = 0; i < data.length; i++) {
14
+ const char = data.charCodeAt(i);
15
+ if (instr === false && char === quoteCode) instr = true;
16
+ else if (
17
+ instr === true &&
18
+ char === quoteCode &&
19
+ data.charCodeAt(i - 1) !== backSlashCode
20
+ )
21
+ instr = false;
22
+
23
+ if (instr === false) {
24
+ if (!isSpace(char)) result.write(data.charAt(i));
25
+ } else {
26
+ result.write(data.charAt(i));
27
+ }
28
+ }
29
+ return result.toString();
30
+ }
package/index.ts CHANGED
@@ -1 +0,0 @@
1
- export { JSON } from "./assembly/index";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.4.2",
3
+ "version": "0.4.5",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -14,20 +14,20 @@
14
14
  "build:transform": "tsc -p ./transform",
15
15
  "test:wasmtime": "wasmtime ./build/test.wasm",
16
16
  "test:lunatic": "lunatic ./build/test.wasm",
17
- "test:wasm3": "wasm3 ./build/test.wasm"
17
+ "test:wasm3": "wasm3 ./build/test.wasm",
18
+ "prettier": "as-prettier -w ."
18
19
  },
19
20
  "devDependencies": {
21
+ "@as-pect/cli": "^7.0.7",
20
22
  "@as-tral/cli": "^1.1.1",
21
23
  "as-console": "^6.0.2",
22
24
  "assemblyscript": "^0.20.7",
23
25
  "assemblyscript-prettier": "^1.0.2",
24
- "benchmark": "^2.1.4",
25
- "microtime": "^3.0.0",
26
26
  "typescript": "^4.7.2"
27
27
  },
28
28
  "dependencies": {
29
29
  "as-string-sink": "^0.5.0",
30
- "as-variant": "^0.3.0"
30
+ "as-variant": "^0.4.0"
31
31
  },
32
32
  "repository": {
33
33
  "type": "git",
@@ -38,7 +38,8 @@
38
38
  "json",
39
39
  "serialize",
40
40
  "deserialize",
41
- "dynamic"
41
+ "dynamic",
42
+ "serde"
42
43
  ],
43
44
  "bugs": {
44
45
  "url": "https://github.com/JairusSW/as-json/issues"
package/tests/index.js CHANGED
File without changes
package/tests/test.js CHANGED
File without changes
@@ -1,78 +1,74 @@
1
- import { ClassDecorator, registerDecorator } from "visitor-as/dist/decorator.js";
2
- import { getName } from "visitor-as/dist/utils.js";
3
- import { SimpleParser } from "visitor-as/dist/index.js";
4
- class AsJSONTransform extends ClassDecorator {
5
- constructor() {
6
- super(...arguments);
7
- this.sources = [];
8
- this.encodeStmts = new Map();
9
- this.decodeCode = new Map();
10
- }
11
- visitMethodDeclaration(node) { }
12
- visitFieldDeclaration(node) {
13
- const name = getName(node);
14
- if (!node.type) {
15
- throw new Error(`Field ${name} is missing a type declaration`);
16
- }
17
- const type = getName(node.type);
18
- const className = this.currentClass.name.text;
19
- if (!this.encodeStmts.has(className))
20
- this.encodeStmts.set(className, []);
21
- if (!this.decodeCode.has(className))
22
- this.decodeCode.set(className, []);
23
- // @ts-ignore
24
- this.encodeStmts.get(className).push(`this.__JSON_Serialized += '' + '"' + '${name}' + '"' + ':' + JSON.stringify<${type}>(this.${name}) + ',';`);
25
- // @ts-ignore
26
- this.decodeCode.get(className).push(`${name}: JSON.parse<${type}>(values.get("${name}")),\n`);
27
- }
28
- visitClassDeclaration(node) {
29
- if (!node.members) {
30
- return;
31
- }
32
- this.currentClass = node;
33
- const name = getName(node);
34
- this.encodeStmts.delete(name);
35
- this.decodeCode.delete(name);
36
- this.visit(node.members);
37
- const serializedProp = `__JSON_Serialized: string = "";`;
38
- let serializeFunc = ``;
39
- if (this.encodeStmts.has(name) && this.encodeStmts.get(name)) {
40
- serializeFunc = `
41
- __JSON_Serialize(): string {
42
- if (!this.__JSON_Serialized) {
43
- ${ // @ts-ignore
44
- this.encodeStmts.get(name).join("\n")};
45
- this.__JSON_Serialized = "{" + this.__JSON_Serialized.slice(0, this.__JSON_Serialized.length - 1) + "}";
46
- }
47
- return this.__JSON_Serialized;
48
- }
49
- `;
50
- }
51
- else {
52
- serializeFunc = `
53
- __JSON_Serialize(): string {
54
- return "{}";
55
- }
56
- `;
57
- }
58
- const deserializeFunc = `
59
- __JSON_Deserialize(values: Map<string, string>): ${name} {
60
- return {
61
- ${ // @ts-ignore
62
- this.decodeCode.get(name) ? this.decodeCode.get(name).join("") : ""}
63
- }
64
- }
65
- `;
66
- //console.log(serializedProp, serializeFunc, deserializeFunc)
67
- const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
68
- node.members.push(serializedProperty);
69
- const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
70
- node.members.push(serializeMethod);
71
- const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node);
72
- node.members.push(deserializeMethod);
73
- }
74
- get name() {
75
- return "json";
76
- }
77
- }
78
- export default registerDecorator(new AsJSONTransform());
1
+ import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js";
2
+ import { getName } from "visitor-as/dist/utils.js";
3
+ import { SimpleParser } from "visitor-as/dist/index.js";
4
+ class AsJSONTransform extends ClassDecorator {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.sources = [];
8
+ this.encodeStmts = [];
9
+ this.decodeStmts = [];
10
+ }
11
+ visitMethodDeclaration(node) { }
12
+ visitFieldDeclaration(node) {
13
+ const name = getName(node);
14
+ if (!node.type) {
15
+ throw new Error(`Field ${name} is missing a type declaration`);
16
+ }
17
+ const type = getName(node.type);
18
+ // @ts-ignore
19
+ this.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
20
+ // @ts-ignore
21
+ this.decodeStmts.push(`${name}: JSON.parse<${type}>(values.get("${name}")),\n`);
22
+ }
23
+ visitClassDeclaration(node) {
24
+ if (!node.members) {
25
+ return;
26
+ }
27
+ this.currentClass = node;
28
+ const name = getName(node);
29
+ this.visit(node.members);
30
+ const serializedProp = `__JSON_Serialized: string = "";`;
31
+ let serializeFunc = ``;
32
+ if (this.encodeStmts.length > 0) {
33
+ const stmt = this.encodeStmts[this.encodeStmts.length - 1];
34
+ this.encodeStmts[this.encodeStmts.length - 1] = stmt.slice(0, stmt.length - 1);
35
+ serializeFunc = `
36
+ @inline
37
+ __JSON_Serialize(): string {
38
+ return \`{${this.encodeStmts.join("")}}\`;
39
+ }
40
+ `;
41
+ }
42
+ else {
43
+ serializeFunc = `
44
+ @inline
45
+ __JSON_Serialize(): string {
46
+ return "{}";
47
+ }
48
+ `;
49
+ }
50
+ const deserializeFunc = `
51
+ @inline
52
+ __JSON_Deserialize(values: Map<string, string>): ${name} {
53
+ return {
54
+ ${
55
+ // @ts-ignore
56
+ this.decodeStmts.join("")}
57
+ }
58
+ }
59
+ `;
60
+ this.encodeStmts = [];
61
+ this.decodeStmts = [];
62
+ //console.log(serializeFunc, deserializeFunc)
63
+ const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
64
+ node.members.push(serializedProperty);
65
+ const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
66
+ node.members.push(serializeMethod);
67
+ const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node);
68
+ node.members.push(deserializeMethod);
69
+ }
70
+ get name() {
71
+ return "json";
72
+ }
73
+ }
74
+ export default registerDecorator(new AsJSONTransform());
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.4.2",
3
+ "version": "0.4.5",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -8,7 +8,6 @@
8
8
  "DogWhich"
9
9
  ],
10
10
  "license": "MIT",
11
- "scripts": {},
12
11
  "devDependencies": {},
13
12
  "dependencies": {
14
13
  "visitor-as": "^0.10.2"
File without changes
@@ -1,98 +1,109 @@
1
- import {
2
- ClassDeclaration,
3
- FieldDeclaration,
4
- MethodDeclaration,
5
- Source
6
- } from "assemblyscript/dist/assemblyscript";
7
- import { ClassDecorator, registerDecorator } from "visitor-as/dist/decorator.js";
8
- import { getName } from "visitor-as/dist/utils.js";
9
- import { SimpleParser } from "visitor-as/dist/index.js";
10
-
11
- class AsJSONTransform extends ClassDecorator {
12
- public currentClass!: ClassDeclaration;
13
- public sources: Source[] = [];
14
- public encodeStmts = new Map<string, string[]>();
15
- public decodeCode = new Map<string, string[]>();
16
-
17
- visitMethodDeclaration(node: MethodDeclaration): void {}
18
- visitFieldDeclaration(node: FieldDeclaration): void {
19
- const name = getName(node);
20
- if (!node.type) {
21
- throw new Error(`Field ${name} is missing a type declaration`);
22
- }
23
-
24
- const type = getName(node.type);
25
-
26
- const className = this.currentClass!.name.text
27
- if (!this.encodeStmts.has(className)) this.encodeStmts.set(className, [])
28
- if (!this.decodeCode.has(className)) this.decodeCode.set(className, [])
29
- // @ts-ignore
30
- this.encodeStmts.get(className).push(
31
- `this.__JSON_Serialized += '' + '"' + '${name}' + '"' + ':' + JSON.stringify<${type}>(this.${name}) + ',';`
32
- );
33
-
34
- // @ts-ignore
35
- this.decodeCode.get(className).push(
36
- `${name}: JSON.parse<${type}>(values.get("${name}")),\n`
37
- );
38
- }
39
- visitClassDeclaration(node: ClassDeclaration): void {
40
- if (!node.members) {
41
- return;
42
- }
43
-
44
- this.currentClass = node;
45
-
46
- const name = getName(node);
47
-
48
- this.encodeStmts.delete(name);
49
- this.decodeCode.delete(name);
50
- this.visit(node.members);
51
-
52
- const serializedProp = `__JSON_Serialized: string = "";`
53
-
54
- let serializeFunc = ``
55
-
56
- if (this.encodeStmts.has(name) && this.encodeStmts.get(name)) {
57
- serializeFunc = `
58
- __JSON_Serialize(): string {
59
- if (!this.__JSON_Serialized) {
60
- ${// @ts-ignore
61
- this.encodeStmts.get(name).join("\n")};
62
- this.__JSON_Serialized = "{" + this.__JSON_Serialized.slice(0, this.__JSON_Serialized.length - 1) + "}";
63
- }
64
- return this.__JSON_Serialized;
65
- }
66
- `
67
- } else {
68
- serializeFunc = `
69
- __JSON_Serialize(): string {
70
- return "{}";
71
- }
72
- `
73
- }
74
-
75
- const deserializeFunc = `
76
- __JSON_Deserialize(values: Map<string, string>): ${name} {
77
- return {
78
- ${// @ts-ignore
79
- this.decodeCode.get(name) ? this.decodeCode.get(name).join("") : ""}
80
- }
81
- }
82
- `;
83
- //console.log(serializedProp, serializeFunc, deserializeFunc)
84
- const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
85
- node.members.push(serializedProperty);
86
-
87
- const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
88
- node.members.push(serializeMethod);
89
-
90
- const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node);
91
- node.members.push(deserializeMethod);
92
- }
93
- get name(): string {
94
- return "json";
95
- }
96
- }
97
-
98
- export default registerDecorator(new AsJSONTransform());
1
+ import {
2
+ ClassDeclaration,
3
+ FieldDeclaration,
4
+ MethodDeclaration,
5
+ Source,
6
+ } from "assemblyscript/dist/assemblyscript";
7
+ import {
8
+ ClassDecorator,
9
+ registerDecorator,
10
+ } from "visitor-as/dist/decorator.js";
11
+ import { getName } from "visitor-as/dist/utils.js";
12
+ import { SimpleParser } from "visitor-as/dist/index.js";
13
+
14
+ class AsJSONTransform extends ClassDecorator {
15
+ public currentClass!: ClassDeclaration;
16
+ public sources: Source[] = [];
17
+ public encodeStmts: string[] = [];
18
+ public decodeStmts: string[] = [];
19
+
20
+ visitMethodDeclaration(node: MethodDeclaration): void {}
21
+ visitFieldDeclaration(node: FieldDeclaration): void {
22
+ const name = getName(node);
23
+ if (!node.type) {
24
+ throw new Error(`Field ${name} is missing a type declaration`);
25
+ }
26
+
27
+ const type = getName(node.type);
28
+
29
+ // @ts-ignore
30
+ this.encodeStmts.push(
31
+ `"${name}":\${JSON.stringify<${type}>(this.${name})},`
32
+ );
33
+
34
+ // @ts-ignore
35
+ this.decodeStmts.push(
36
+ `${name}: JSON.parse<${type}>(values.get("${name}")),\n`
37
+ );
38
+ }
39
+ visitClassDeclaration(node: ClassDeclaration): void {
40
+ if (!node.members) {
41
+ return;
42
+ }
43
+
44
+ this.currentClass = node;
45
+
46
+ const name = getName(node);
47
+
48
+ this.visit(node.members);
49
+
50
+ const serializedProp = `__JSON_Serialized: string = "";`;
51
+
52
+ let serializeFunc = ``;
53
+
54
+ if (this.encodeStmts.length > 0) {
55
+ const stmt = this.encodeStmts[this.encodeStmts.length - 1]!;
56
+ this.encodeStmts[this.encodeStmts.length - 1] = stmt!.slice(
57
+ 0,
58
+ stmt.length - 1
59
+ );
60
+ serializeFunc = `
61
+ @inline
62
+ __JSON_Serialize(): string {
63
+ return \`{${this.encodeStmts.join("")}}\`;
64
+ }
65
+ `;
66
+ } else {
67
+ serializeFunc = `
68
+ @inline
69
+ __JSON_Serialize(): string {
70
+ return "{}";
71
+ }
72
+ `;
73
+ }
74
+
75
+ const deserializeFunc = `
76
+ @inline
77
+ __JSON_Deserialize(values: Map<string, string>): ${name} {
78
+ return {
79
+ ${
80
+ // @ts-ignore
81
+ this.decodeStmts.join("")
82
+ }
83
+ }
84
+ }
85
+ `;
86
+ this.encodeStmts = [];
87
+ this.decodeStmts = [];
88
+ //console.log(serializeFunc, deserializeFunc)
89
+ const serializedProperty = SimpleParser.parseClassMember(
90
+ serializedProp,
91
+ node
92
+ );
93
+ node.members.push(serializedProperty);
94
+
95
+ const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
96
+ node.members.push(serializeMethod);
97
+
98
+ const deserializeMethod = SimpleParser.parseClassMember(
99
+ deserializeFunc,
100
+ node
101
+ );
102
+ node.members.push(deserializeMethod);
103
+ }
104
+ get name(): string {
105
+ return "json";
106
+ }
107
+ }
108
+
109
+ export default registerDecorator(new AsJSONTransform());
@@ -14,7 +14,7 @@
14
14
  // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15
15
  "sourceMap": false /* Generates corresponding '.map' file. */,
16
16
  // "outFile": "./", /* Concatenate and emit output to single file. */
17
- "outDir": "./lib/" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
17
+ "outDir": "./lib" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
18
18
  // "composite": true, /* Enable project compilation */
19
19
  // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
20
20
  // "removeComments": true, /* Do not emit comments to output. */