json-as 0.9.2 → 0.9.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/CHANGELOG +2 -0
- package/README.md +1 -1
- package/assembly/__tests__/deserialize.spec.ts +1 -1
- package/assembly/__tests__/serialize.spec.ts +21 -1
- package/assembly/serialize/unknown.ts +45 -0
- package/assembly/test.ts +2 -1
- package/package.json +2 -2
- package/transform/lib/index.js +23 -20
- package/transform/package.json +1 -1
- package/transform/src/index.ts +20 -16
package/CHANGELOG
CHANGED
|
@@ -7,5 +7,7 @@ v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have
|
|
|
7
7
|
v0.9.0 - BREAKING CHANGE - API changed from JSON.parse(data, defaultValues) to JSON.parse(data). Default Values defaults to true. Large update. Refactor all the code, nullable primitives, rewrite the transform, allow extensibility with @omit keywords, and fix a plethora of bugs
|
|
8
8
|
v0.9.1 - Fix #71
|
|
9
9
|
v0.9.2 - Fix #75 + Build sizes significantly reduced
|
|
10
|
+
v0.9.3 - Fix #76
|
|
11
|
+
v0.9.4 - Fix #77
|
|
10
12
|
|
|
11
13
|
[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
|
@@ -48,6 +48,16 @@ class Player {
|
|
|
48
48
|
isVerified: boolean;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
@json
|
|
52
|
+
class OmitIf {
|
|
53
|
+
x: i32 = 1;
|
|
54
|
+
@omitif("this.y == -1")
|
|
55
|
+
y: i32 = -1;
|
|
56
|
+
z: i32 = 1;
|
|
57
|
+
@omitnull()
|
|
58
|
+
foo: string | null = null
|
|
59
|
+
}
|
|
60
|
+
|
|
51
61
|
class Nullable { }
|
|
52
62
|
type Null = Nullable | null;
|
|
53
63
|
|
|
@@ -297,7 +307,7 @@ describe("Should serialize object arrays", () => {
|
|
|
297
307
|
|
|
298
308
|
});
|
|
299
309
|
|
|
300
|
-
describe("
|
|
310
|
+
describe("Should serialize objects", () => {
|
|
301
311
|
|
|
302
312
|
expect(
|
|
303
313
|
JSON.stringify<Vec3>({
|
|
@@ -348,6 +358,16 @@ describe("Ser/de Objects", () => {
|
|
|
348
358
|
|
|
349
359
|
});
|
|
350
360
|
|
|
361
|
+
describe("Should serialize @omit'ed objects", () => {
|
|
362
|
+
|
|
363
|
+
expect(
|
|
364
|
+
JSON.stringify(<OmitIf>{
|
|
365
|
+
y: 1
|
|
366
|
+
})
|
|
367
|
+
).toBe('{"x":1,"y":1,"z":1}');
|
|
368
|
+
|
|
369
|
+
});
|
|
370
|
+
|
|
351
371
|
@json
|
|
352
372
|
class ObjWithString {
|
|
353
373
|
s!: string;
|
|
@@ -0,0 +1,45 @@
|
|
|
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): string {
|
|
17
|
+
const type = data.type;
|
|
18
|
+
switch (type) {
|
|
19
|
+
case JSON.Types.String: {
|
|
20
|
+
return serializeString(data.get<string>());
|
|
21
|
+
}
|
|
22
|
+
case JSON.Types.Bool: {
|
|
23
|
+
return serializeBool(data.get<bool>());
|
|
24
|
+
}
|
|
25
|
+
case JSON.Types.U8: {
|
|
26
|
+
return serializeInteger(data.get<u8>());
|
|
27
|
+
}
|
|
28
|
+
case JSON.Types.U16: {
|
|
29
|
+
return serializeInteger(data.get<u16>());
|
|
30
|
+
}
|
|
31
|
+
case JSON.Types.U32: {
|
|
32
|
+
return serializeInteger(data.get<u32>());
|
|
33
|
+
}
|
|
34
|
+
case JSON.Types.U64: {
|
|
35
|
+
return serializeInteger(data.get<u64>());
|
|
36
|
+
}
|
|
37
|
+
case JSON.Types.F32: {
|
|
38
|
+
return serializeFloat(data.get<f32>());
|
|
39
|
+
}
|
|
40
|
+
case JSON.Types.F64: {
|
|
41
|
+
return serializeFloat(data.get<f64>());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return serializeUnknownArray(data.get<JSON.Value[]>());
|
|
45
|
+
}
|
package/assembly/test.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.4",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"scripts": {
|
|
18
18
|
"test": "wasmtime build/serialize.spec.wasm && wasmtime build/deserialize.spec.wasm",
|
|
19
19
|
"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
|
-
"build:test": "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",
|
|
20
|
+
"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
21
|
"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",
|
|
22
22
|
"bench:wasmtime": "wasmtime ./bench/benchmark.wasm",
|
|
23
23
|
"bench:wasmer": "wasmer --llvm ./bench/benchmark.wasm",
|
package/transform/lib/index.js
CHANGED
|
@@ -33,8 +33,9 @@ class JSONTransform extends BaseVisitor {
|
|
|
33
33
|
if (schema.parent?.members) {
|
|
34
34
|
for (let i = 0; i < schema.parent.members.length; i++) {
|
|
35
35
|
const replace = schema.members.find((v) => v.name == schema.parent?.members[i]?.name);
|
|
36
|
-
if (!replace)
|
|
36
|
+
if (!replace) {
|
|
37
37
|
schema.members.unshift(schema.parent.members[i]);
|
|
38
|
+
}
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
}
|
|
@@ -76,29 +77,31 @@ class JSONTransform extends BaseVisitor {
|
|
|
76
77
|
continue;
|
|
77
78
|
if (member.flags === 1024 /* CommonFlags.Protected */)
|
|
78
79
|
continue;
|
|
80
|
+
if (member.decorators && member.decorators.find((v) => v.name.text == "omit"))
|
|
81
|
+
continue;
|
|
79
82
|
const mem = new Property();
|
|
80
83
|
mem.name = name.text;
|
|
81
84
|
mem.type = type;
|
|
82
85
|
mem.value = value;
|
|
83
86
|
mem.node = member;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
87
|
+
if (member.decorators) {
|
|
88
|
+
for (let i = 0; i < (member.decorators).length; i++) {
|
|
89
|
+
const decorator = member.decorators[i];
|
|
90
|
+
if (decorator.name.text == "alias") {
|
|
91
|
+
if (!decorator.args?.length)
|
|
92
|
+
throw new Error("Expected 1 argument but got zero at @alias in " + node.range.source.normalizedPath);
|
|
93
|
+
mem.flag = PropertyFlags.Alias;
|
|
94
|
+
mem.alias = decorator.args[0].value;
|
|
95
|
+
}
|
|
96
|
+
else if (decorator.name.text == "omitnull") {
|
|
97
|
+
mem.flag = PropertyFlags.OmitNull;
|
|
98
|
+
}
|
|
99
|
+
else if (decorator.name.text == "omitif") {
|
|
100
|
+
if (!decorator.args?.length)
|
|
101
|
+
throw new Error("Expected 1 argument but got zero at @omitif in " + node.range.source.normalizedPath);
|
|
102
|
+
mem.args?.push(decorator.args[0].value);
|
|
103
|
+
mem.flag = PropertyFlags.OmitIf;
|
|
104
|
+
}
|
|
102
105
|
}
|
|
103
106
|
}
|
|
104
107
|
if (mem.flag === PropertyFlags.Alias) {
|
|
@@ -113,7 +116,7 @@ class JSONTransform extends BaseVisitor {
|
|
|
113
116
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
|
|
114
117
|
}
|
|
115
118
|
else if (mem.flag == PropertyFlags.OmitIf) {
|
|
116
|
-
mem.serialize = "${" + mem.args[0] + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
119
|
+
mem.serialize = "${" + mem.args[0] + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
117
120
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
|
|
118
121
|
}
|
|
119
122
|
else if (mem.flag == PropertyFlags.Alias) {
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -49,8 +49,10 @@ class JSONTransform extends BaseVisitor {
|
|
|
49
49
|
for (let i = 0; i < schema.parent.members.length; i++) {
|
|
50
50
|
const replace = schema.members.find(
|
|
51
51
|
(v) => v.name == schema.parent?.members[i]?.name
|
|
52
|
-
)
|
|
53
|
-
if (!replace)
|
|
52
|
+
);
|
|
53
|
+
if (!replace) {
|
|
54
|
+
schema.members.unshift(schema.parent.members[i]!);
|
|
55
|
+
}
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
}
|
|
@@ -94,6 +96,7 @@ class JSONTransform extends BaseVisitor {
|
|
|
94
96
|
if (member.flags == CommonFlags.Static) continue;
|
|
95
97
|
if (member.flags === CommonFlags.Private) continue;
|
|
96
98
|
if (member.flags === CommonFlags.Protected) continue;
|
|
99
|
+
if (member.decorators && member.decorators.find((v) => (<IdentifierExpression>v.name).text == "omit")) continue;
|
|
97
100
|
|
|
98
101
|
const mem = new Property();
|
|
99
102
|
mem.name = name.text;
|
|
@@ -101,19 +104,20 @@ class JSONTransform extends BaseVisitor {
|
|
|
101
104
|
mem.value = value;
|
|
102
105
|
mem.node = member;
|
|
103
106
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
107
|
+
if (member.decorators) {
|
|
108
|
+
for (let i = 0; i < (member.decorators).length; i++) {
|
|
109
|
+
const decorator = member.decorators[i]!;
|
|
110
|
+
if ((<IdentifierExpression>decorator.name).text == "alias") {
|
|
111
|
+
if (!decorator.args?.length) throw new Error("Expected 1 argument but got zero at @alias in " + node.range.source.normalizedPath);
|
|
112
|
+
mem.flag = PropertyFlags.Alias;
|
|
113
|
+
mem.alias = (decorator.args[0] as StringLiteralExpression).value;
|
|
114
|
+
} else if ((<IdentifierExpression>decorator.name).text == "omitnull") {
|
|
115
|
+
mem.flag = PropertyFlags.OmitNull;
|
|
116
|
+
} else if ((<IdentifierExpression>decorator.name).text == "omitif") {
|
|
117
|
+
if (!decorator.args?.length) throw new Error("Expected 1 argument but got zero at @omitif in " + node.range.source.normalizedPath);
|
|
118
|
+
mem.args?.push((decorator.args[0] as StringLiteralExpression).value);
|
|
119
|
+
mem.flag = PropertyFlags.OmitIf;
|
|
120
|
+
}
|
|
117
121
|
}
|
|
118
122
|
}
|
|
119
123
|
|
|
@@ -128,7 +132,7 @@ class JSONTransform extends BaseVisitor {
|
|
|
128
132
|
mem.serialize = "${changetype<usize>(this." + mem.name + ") == <usize>0" + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
129
133
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
|
|
130
134
|
} else if (mem.flag == PropertyFlags.OmitIf) {
|
|
131
|
-
mem.serialize = "${" + mem.args![0]! + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ")}";
|
|
135
|
+
mem.serialize = "${" + mem.args![0]! + " ? \"\" : '" + escapeString(JSON.stringify(mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
|
|
132
136
|
mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
|
|
133
137
|
} else if (mem.flag == PropertyFlags.Alias) {
|
|
134
138
|
mem.serialize = escapeString(JSON.stringify(mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
|