json-as 0.9.5 → 0.9.7
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/.github/workflows/release-package.yml +19 -5
- package/CHANGELOG +2 -0
- package/README.md +9 -11
- package/assembly/__tests__/deserialize.spec.ts +4 -1
- package/assembly/__tests__/serialize.spec.ts +4 -1
- package/assembly/test.ts +3 -2
- package/develop/assembly/serialize/unknown.ts +46 -0
- package/package.json +5 -3
- package/transform/lib/index.js +27 -26
- package/transform/package.json +1 -1
- package/transform/src/index.ts +30 -25
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
name:
|
|
1
|
+
name: Node.js Package
|
|
2
2
|
|
|
3
3
|
on:
|
|
4
4
|
release:
|
|
@@ -11,9 +11,23 @@ jobs:
|
|
|
11
11
|
- uses: actions/checkout@v4
|
|
12
12
|
- uses: actions/setup-node@v3
|
|
13
13
|
with:
|
|
14
|
-
node-version: 16
|
|
15
|
-
|
|
16
|
-
-
|
|
14
|
+
node-version: 16
|
|
15
|
+
|
|
16
|
+
- name: Install Wasmtime
|
|
17
|
+
uses: jcbhmr/setup-wasmtime@v2
|
|
18
|
+
|
|
19
|
+
- name: Setup Node.js
|
|
20
|
+
uses: actions/setup-node@v2
|
|
21
|
+
|
|
22
|
+
- name: Install dependencies
|
|
23
|
+
if: steps.node-cache.outputs.cache-hit != 'true'
|
|
24
|
+
run: yarn
|
|
25
|
+
|
|
26
|
+
- name: Build tests
|
|
27
|
+
run: yarn run tests:build
|
|
28
|
+
|
|
29
|
+
- name: Perform tests
|
|
30
|
+
run: yarn run test
|
|
17
31
|
|
|
18
32
|
publish-gpr:
|
|
19
33
|
needs: build
|
|
@@ -25,7 +39,7 @@ jobs:
|
|
|
25
39
|
- uses: actions/checkout@v4
|
|
26
40
|
- uses: actions/setup-node@v3
|
|
27
41
|
with:
|
|
28
|
-
node-version:
|
|
42
|
+
node-version: 22
|
|
29
43
|
registry-url: https://npm.pkg.github.com/
|
|
30
44
|
- run: npm ci
|
|
31
45
|
- run: npm publish
|
package/CHANGELOG
CHANGED
|
@@ -10,5 +10,7 @@ v0.9.2 - Fix #75 + Build sizes significantly reduced
|
|
|
10
10
|
v0.9.3 - Fix #76
|
|
11
11
|
v0.9.4 - Fix #77
|
|
12
12
|
v0.9.5 - Fix #46
|
|
13
|
+
v0.9.6 - Fix bugs
|
|
14
|
+
v0.9.7 - Update testing framework and readme logo
|
|
13
15
|
|
|
14
16
|
[UNRELEASED] v0.9.x - Port JSON.Value from the `develop` branch to allow for union types, parsing of arbitrary data, and whatever the hell you want.
|
package/README.md
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
<
|
|
2
|
-
<pre>
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
v0.9.5
|
|
1
|
+
<h5 align="center">
|
|
2
|
+
<pre>
|
|
3
|
+
_____ _____ __ _____ _____ _____
|
|
4
|
+
| _ | __|___ __| | __| | | |
|
|
5
|
+
| |__ |___| | |__ | | | | | |
|
|
6
|
+
|__|__|_____| |_____|_____|_____|_|___|
|
|
7
|
+
|
|
8
|
+
v0.9.7
|
|
11
9
|
</pre>
|
|
12
|
-
</
|
|
10
|
+
</h5>
|
|
13
11
|
|
|
14
12
|
## Installation
|
|
15
13
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { JSON } from "..";
|
|
2
2
|
import {
|
|
3
3
|
describe,
|
|
4
|
-
expect
|
|
4
|
+
expect,
|
|
5
|
+
run
|
|
5
6
|
} from "as-test/assembly";
|
|
6
7
|
|
|
7
8
|
@json
|
|
@@ -271,6 +272,8 @@ describe("Should deserialize Objects", () => {
|
|
|
271
272
|
|
|
272
273
|
});
|
|
273
274
|
|
|
275
|
+
run();
|
|
276
|
+
|
|
274
277
|
@json
|
|
275
278
|
class ObjWithString {
|
|
276
279
|
s!: string;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { JSON } from "..";
|
|
2
2
|
import {
|
|
3
3
|
describe,
|
|
4
|
-
expect
|
|
4
|
+
expect,
|
|
5
|
+
run
|
|
5
6
|
} from "as-test/assembly";
|
|
6
7
|
|
|
7
8
|
@json
|
|
@@ -368,6 +369,8 @@ describe("Should serialize @omit'ed objects", () => {
|
|
|
368
369
|
|
|
369
370
|
});
|
|
370
371
|
|
|
372
|
+
run();
|
|
373
|
+
|
|
371
374
|
@json
|
|
372
375
|
class ObjWithString {
|
|
373
376
|
s!: string;
|
package/assembly/test.ts
CHANGED
|
@@ -15,7 +15,8 @@ class Box<T> {
|
|
|
15
15
|
@json
|
|
16
16
|
class Player {
|
|
17
17
|
@alias("first name")
|
|
18
|
-
|
|
18
|
+
@omitnull()
|
|
19
|
+
firstName: string | null;
|
|
19
20
|
lastName!: string;
|
|
20
21
|
lastActive!: i32[];
|
|
21
22
|
// Drop in a code block, function, or expression that evaluates to a boolean
|
|
@@ -29,7 +30,7 @@ class Player {
|
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
const player: Player = {
|
|
32
|
-
firstName:
|
|
33
|
+
firstName: null,
|
|
33
34
|
lastName: "West",
|
|
34
35
|
lastActive: [8, 27, 2022],
|
|
35
36
|
age: 23,
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { Sink } from "../src/sink";
|
|
3
|
+
import { __atoi_fast } from "../src/util";
|
|
4
|
+
import { serializeUnknownArray } from "./array/unknown";
|
|
5
|
+
import { serializeBool } from "./bool";
|
|
6
|
+
import { serializeFloat } from "./float";
|
|
7
|
+
import { serializeInteger } from "./integer";
|
|
8
|
+
import { serializeString } from "./string";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Serializes unknown values into their correct serializer and returns the data.
|
|
12
|
+
*
|
|
13
|
+
* @param data - The JSON.Value to be serialized.
|
|
14
|
+
* @returns The serialized result.
|
|
15
|
+
*/
|
|
16
|
+
export function serializeUnknown(data: JSON.Value, out: Sink | null = null): Sink {
|
|
17
|
+
const type = data.type;
|
|
18
|
+
switch (type) {
|
|
19
|
+
case JSON.Types.String: {
|
|
20
|
+
return serializeString(data.get<string>(), out);
|
|
21
|
+
}
|
|
22
|
+
case JSON.Types.Boolean: {
|
|
23
|
+
return serializeBool(data.get<bool>(), out);
|
|
24
|
+
}
|
|
25
|
+
case JSON.Types.U8: {
|
|
26
|
+
return serializeInteger(data.get<u8>(), out);
|
|
27
|
+
}
|
|
28
|
+
case JSON.Types.U16: {
|
|
29
|
+
return serializeInteger(data.get<u16>(), out);
|
|
30
|
+
}
|
|
31
|
+
case JSON.Types.U32: {
|
|
32
|
+
return serializeInteger(data.get<u32>(), out);
|
|
33
|
+
}
|
|
34
|
+
case JSON.Types.U64: {
|
|
35
|
+
return serializeInteger(data.get<u64>(), out);
|
|
36
|
+
}
|
|
37
|
+
case JSON.Types.F32: {
|
|
38
|
+
return serializeFloat(data.get<f32>(), out);
|
|
39
|
+
}
|
|
40
|
+
case JSON.Types.F64: {
|
|
41
|
+
return serializeFloat(data.get<f64>(), out);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return serializeUnknownArray(data.get<JSON.Value[]>(), out);
|
|
45
|
+
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.7",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -15,7 +15,9 @@
|
|
|
15
15
|
],
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"scripts": {
|
|
18
|
-
"test": "
|
|
18
|
+
"test": "npm run test:serialize && npm run test:deserialize",
|
|
19
|
+
"test:serialize": "wasmtime build/serialize.spec.wasm",
|
|
20
|
+
"test:deserialize": "wasmtime build/deserialize.spec.wasm",
|
|
19
21
|
"tests:build": "asc assembly/__tests__/serialize.spec.ts -o build/serialize.spec.wasm --transform ./transform --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --optimizeLevel 0 --shrinkLevel 0 --noAssert --uncheckedBehavior always & asc assembly/__tests__/deserialize.spec.ts -o build/deserialize.spec.wasm --transform ./transform --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --optimizeLevel 0 --shrinkLevel 0 --noAssert --uncheckedBehavior always",
|
|
20
22
|
"build:test": "JSON_DEBUG=true asc assembly/test.ts -o build/test.wasm --transform ./transform --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --optimizeLevel 0 --shrinkLevel 0 --noAssert --uncheckedBehavior always",
|
|
21
23
|
"build:bench": "asc bench/benchmark.ts -o bench/benchmark.wasm --transform ./transform --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub",
|
|
@@ -31,7 +33,6 @@
|
|
|
31
33
|
"devDependencies": {
|
|
32
34
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
33
35
|
"as-bench": "^0.0.0-alpha",
|
|
34
|
-
"as-test": "JairusSW/as-test",
|
|
35
36
|
"assemblyscript": "^0.27.22",
|
|
36
37
|
"assemblyscript-prettier": "^3.0.1",
|
|
37
38
|
"benchmark": "^2.1.4",
|
|
@@ -44,6 +45,7 @@
|
|
|
44
45
|
"dependencies": {
|
|
45
46
|
"as-container": "^0.8.0",
|
|
46
47
|
"as-string-sink": "^0.5.3",
|
|
48
|
+
"as-test": "^0.0.2",
|
|
47
49
|
"as-variant": "^0.4.1",
|
|
48
50
|
"as-virtual": "^0.2.0"
|
|
49
51
|
},
|
package/transform/lib/index.js
CHANGED
|
@@ -85,59 +85,60 @@ class JSONTransform extends BaseVisitor {
|
|
|
85
85
|
mem.value = value;
|
|
86
86
|
mem.node = member;
|
|
87
87
|
if (member.decorators) {
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
let decorator = null;
|
|
89
|
+
if (decorator = member.decorators.find(v => v.name.text == "alias")) {
|
|
90
90
|
if (decorator.name.text == "alias") {
|
|
91
91
|
if (!decorator.args?.length)
|
|
92
92
|
throw new Error("Expected 1 argument but got zero at @alias in " + node.range.source.normalizedPath);
|
|
93
|
-
mem.
|
|
93
|
+
mem.flags.push(PropertyFlags.Alias);
|
|
94
94
|
mem.alias = decorator.args[0].value;
|
|
95
95
|
}
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
}
|
|
97
|
+
for (let i = 0; i < (member.decorators).length; i++) {
|
|
98
|
+
const decorator = member.decorators[i];
|
|
99
|
+
if (decorator.name.text == "omitnull") {
|
|
100
|
+
mem.flags.push(PropertyFlags.OmitNull);
|
|
98
101
|
}
|
|
99
102
|
else if (decorator.name.text == "omitif") {
|
|
100
103
|
if (!decorator.args?.length)
|
|
101
104
|
throw new Error("Expected 1 argument but got zero at @omitif in " + node.range.source.normalizedPath);
|
|
102
105
|
mem.args?.push(decorator.args[0].value);
|
|
103
|
-
mem.
|
|
106
|
+
mem.flags.push(PropertyFlags.OmitIf);
|
|
104
107
|
}
|
|
105
108
|
else if (decorator.name.text == "flatten") {
|
|
106
109
|
if (!decorator.args?.length)
|
|
107
110
|
throw new Error("Expected 1 argument but got zero at @flatten in " + node.range.source.normalizedPath);
|
|
108
|
-
mem.
|
|
111
|
+
mem.flags.push(PropertyFlags.Flatten);
|
|
109
112
|
mem.args = [decorator.args[0].value];
|
|
110
113
|
}
|
|
111
114
|
}
|
|
112
115
|
}
|
|
113
|
-
if (mem.
|
|
114
|
-
mem.
|
|
115
|
-
|
|
116
|
-
else if (mem.flag === PropertyFlags.None) {
|
|
117
|
-
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
116
|
+
if (!mem.flags.length) {
|
|
117
|
+
mem.flags = [PropertyFlags.None];
|
|
118
|
+
mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
118
119
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
|
|
119
120
|
}
|
|
120
|
-
if (mem.
|
|
121
|
-
mem.serialize = "${changetype<usize>(this." + mem.name + ") == <usize>0" + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
121
|
+
if (mem.flags.includes(PropertyFlags.OmitNull)) {
|
|
122
|
+
mem.serialize = "${changetype<usize>(this." + mem.name + ") == <usize>0" + " ? \"\" : '" + escapeString(JSON.stringify(mem.alias || mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
122
123
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
|
|
123
124
|
}
|
|
124
|
-
else if (mem.
|
|
125
|
-
mem.serialize = "${" + mem.args[0] + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
125
|
+
else if (mem.flags.includes(PropertyFlags.OmitIf)) {
|
|
126
|
+
mem.serialize = "${" + mem.args[0] + " ? \"\" : '" + escapeString(JSON.stringify(mem.alias || mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
126
127
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
|
|
127
128
|
}
|
|
128
|
-
else if (mem.
|
|
129
|
-
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
129
|
+
else if (mem.flags.includes(PropertyFlags.Alias)) {
|
|
130
|
+
mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
130
131
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
|
|
131
132
|
mem.name = name.text;
|
|
132
133
|
}
|
|
133
|
-
else if (mem.
|
|
134
|
+
else if (mem.flags.includes(PropertyFlags.Flatten)) {
|
|
134
135
|
const nullable = mem.node.type.isNullable;
|
|
135
136
|
if (nullable) {
|
|
136
|
-
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}";
|
|
137
|
+
mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}";
|
|
137
138
|
mem.deserialize = "if (value_end - value_start == 4 && load<u64>(changetype<usize>(data) + <usize>(value_start << 1)) == " + charCodeAt64("null", 0) + ") {\n this." + name.text + " = null;\n } else {\n this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args[0] + "\":' + data.substring(value_start, value_end) + \"}\");\n }";
|
|
138
139
|
}
|
|
139
140
|
else {
|
|
140
|
-
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}";
|
|
141
|
+
mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}";
|
|
141
142
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args[0] + "\":' + data.substring(value_start, value_end) + \"}\");";
|
|
142
143
|
}
|
|
143
144
|
mem.name = name.text;
|
|
@@ -159,8 +160,8 @@ class JSONTransform extends BaseVisitor {
|
|
|
159
160
|
if (!schema.members.length)
|
|
160
161
|
return;
|
|
161
162
|
found = false;
|
|
162
|
-
if (schema.members[0]?.
|
|
163
|
-
|| schema.members[0]?.
|
|
163
|
+
if (schema.members[0]?.flags.includes(PropertyFlags.OmitNull)
|
|
164
|
+
|| schema.members[0]?.flags.includes(PropertyFlags.OmitIf)) {
|
|
164
165
|
SERIALIZE_RAW += schema.members[0]?.serialize;
|
|
165
166
|
SERIALIZE_PRETTY += "\\n" + schema.members[0]?.serialize;
|
|
166
167
|
}
|
|
@@ -175,8 +176,8 @@ class JSONTransform extends BaseVisitor {
|
|
|
175
176
|
const member = schema.members[i];
|
|
176
177
|
if (member.initialize)
|
|
177
178
|
INITIALIZE += " " + member.initialize + ";\n";
|
|
178
|
-
if (member.
|
|
179
|
-
|| member.
|
|
179
|
+
if (member.flags.includes(PropertyFlags.OmitNull)
|
|
180
|
+
|| member.flags.includes(PropertyFlags.OmitIf)) {
|
|
180
181
|
SERIALIZE_RAW += member.serialize;
|
|
181
182
|
SERIALIZE_PRETTY += member.serialize;
|
|
182
183
|
}
|
|
@@ -374,7 +375,7 @@ class Property {
|
|
|
374
375
|
this.alias = null;
|
|
375
376
|
this.type = "";
|
|
376
377
|
this.value = null;
|
|
377
|
-
this.
|
|
378
|
+
this.flags = [];
|
|
378
379
|
this.args = [];
|
|
379
380
|
this.serialize = null;
|
|
380
381
|
this.deserialize = null;
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { toString, isStdlib } from "visitor-as/dist/utils.js";
|
|
|
12
12
|
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
|
|
13
13
|
import { Transform } from "assemblyscript/dist/transform.js";
|
|
14
14
|
import { CommonFlags } from "types:assemblyscript/src/common";
|
|
15
|
+
import { DecoratorNode } from "types:assemblyscript/src/ast";
|
|
15
16
|
|
|
16
17
|
class JSONTransform extends BaseVisitor {
|
|
17
18
|
public schemasList: SchemaData[] = [];
|
|
@@ -105,50 +106,54 @@ class JSONTransform extends BaseVisitor {
|
|
|
105
106
|
mem.node = member;
|
|
106
107
|
|
|
107
108
|
if (member.decorators) {
|
|
108
|
-
|
|
109
|
-
|
|
109
|
+
let decorator: DecoratorNode | null = null;
|
|
110
|
+
if (decorator = member.decorators.find(v => (<IdentifierExpression>v.name).text == "alias") as DecoratorNode | null) {
|
|
110
111
|
if ((<IdentifierExpression>decorator.name).text == "alias") {
|
|
111
112
|
if (!decorator.args?.length) throw new Error("Expected 1 argument but got zero at @alias in " + node.range.source.normalizedPath);
|
|
112
|
-
mem.
|
|
113
|
+
mem.flags.push(PropertyFlags.Alias);
|
|
113
114
|
mem.alias = (decorator.args[0] as StringLiteralExpression).value;
|
|
114
|
-
}
|
|
115
|
-
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for (let i = 0; i < (member.decorators).length; i++) {
|
|
119
|
+
const decorator = member.decorators[i]!;
|
|
120
|
+
if ((<IdentifierExpression>decorator.name).text == "omitnull") {
|
|
121
|
+
mem.flags.push(PropertyFlags.OmitNull);
|
|
116
122
|
} else if ((<IdentifierExpression>decorator.name).text == "omitif") {
|
|
117
123
|
if (!decorator.args?.length) throw new Error("Expected 1 argument but got zero at @omitif in " + node.range.source.normalizedPath);
|
|
118
124
|
mem.args?.push((decorator.args[0] as StringLiteralExpression).value);
|
|
119
|
-
mem.
|
|
125
|
+
mem.flags.push(PropertyFlags.OmitIf);
|
|
120
126
|
} else if ((<IdentifierExpression>decorator.name).text == "flatten") {
|
|
121
127
|
if (!decorator.args?.length) throw new Error("Expected 1 argument but got zero at @flatten in " + node.range.source.normalizedPath);
|
|
122
|
-
mem.
|
|
128
|
+
mem.flags.push(PropertyFlags.Flatten);
|
|
123
129
|
mem.args = [(decorator.args[0] as StringLiteralExpression).value];
|
|
124
130
|
}
|
|
125
131
|
}
|
|
126
132
|
}
|
|
127
133
|
|
|
128
|
-
if (mem.
|
|
129
|
-
mem.
|
|
130
|
-
|
|
131
|
-
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
134
|
+
if (!mem.flags.length) {
|
|
135
|
+
mem.flags = [PropertyFlags.None];
|
|
136
|
+
mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
132
137
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
|
|
133
138
|
}
|
|
134
139
|
|
|
135
|
-
if (mem.
|
|
136
|
-
mem.serialize = "${changetype<usize>(this." + mem.name + ") == <usize>0" + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
140
|
+
if (mem.flags.includes(PropertyFlags.OmitNull)) {
|
|
141
|
+
mem.serialize = "${changetype<usize>(this." + mem.name + ") == <usize>0" + " ? \"\" : '" + escapeString(JSON.stringify(mem.alias || mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
137
142
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
|
|
138
|
-
} else if (mem.
|
|
139
|
-
mem.serialize = "${" + mem.args![0]! + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
143
|
+
} else if (mem.flags.includes(PropertyFlags.OmitIf)) {
|
|
144
|
+
mem.serialize = "${" + mem.args![0]! + " ? \"\" : '" + escapeString(JSON.stringify(mem.alias || mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
140
145
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
|
|
141
|
-
} else if (mem.
|
|
142
|
-
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
146
|
+
} else if (mem.flags.includes(PropertyFlags.Alias)) {
|
|
147
|
+
mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
143
148
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
|
|
144
149
|
mem.name = name.text;
|
|
145
|
-
} else if (mem.
|
|
150
|
+
} else if (mem.flags.includes(PropertyFlags.Flatten)) {
|
|
146
151
|
const nullable = (mem.node.type as NamedTypeNode).isNullable;
|
|
147
152
|
if (nullable) {
|
|
148
|
-
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}";
|
|
153
|
+
mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}";
|
|
149
154
|
mem.deserialize = "if (value_end - value_start == 4 && load<u64>(changetype<usize>(data) + <usize>(value_start << 1)) == " + charCodeAt64("null", 0) + ") {\n this." + name.text + " = null;\n } else {\n this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args![0]! + "\":' + data.substring(value_start, value_end) + \"}\");\n }";
|
|
150
155
|
} else {
|
|
151
|
-
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}";
|
|
156
|
+
mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}";
|
|
152
157
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args![0]! + "\":' + data.substring(value_start, value_end) + \"}\");";
|
|
153
158
|
}
|
|
154
159
|
mem.name = name.text;
|
|
@@ -177,8 +182,8 @@ class JSONTransform extends BaseVisitor {
|
|
|
177
182
|
found = false;
|
|
178
183
|
|
|
179
184
|
if (
|
|
180
|
-
schema.members[0]?.
|
|
181
|
-
|| schema.members[0]?.
|
|
185
|
+
schema.members[0]?.flags.includes(PropertyFlags.OmitNull)
|
|
186
|
+
|| schema.members[0]?.flags.includes(PropertyFlags.OmitIf)
|
|
182
187
|
) {
|
|
183
188
|
SERIALIZE_RAW += schema.members[0]?.serialize;
|
|
184
189
|
SERIALIZE_PRETTY += "\\n" + schema.members[0]?.serialize;
|
|
@@ -194,8 +199,8 @@ class JSONTransform extends BaseVisitor {
|
|
|
194
199
|
const member = schema.members[i]!;
|
|
195
200
|
if (member.initialize) INITIALIZE += " " + member.initialize + ";\n";
|
|
196
201
|
if (
|
|
197
|
-
member.
|
|
198
|
-
|| member.
|
|
202
|
+
member.flags.includes(PropertyFlags.OmitNull)
|
|
203
|
+
|| member.flags.includes(PropertyFlags.OmitIf)
|
|
199
204
|
) {
|
|
200
205
|
SERIALIZE_RAW += member.serialize;
|
|
201
206
|
SERIALIZE_PRETTY += member.serialize;
|
|
@@ -385,7 +390,7 @@ class Property {
|
|
|
385
390
|
public alias: string | null = null;
|
|
386
391
|
public type: string = "";
|
|
387
392
|
public value: string | null = null;
|
|
388
|
-
public
|
|
393
|
+
public flags: PropertyFlags[] = [];
|
|
389
394
|
public args: string[] | null = [];
|
|
390
395
|
|
|
391
396
|
public serialize: string | null = null;
|