json-as 1.0.6 → 1.0.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/CHANGELOG.md +9 -0
- package/README.md +3 -23
- package/assembly/__tests__/custom.spec.ts +12 -0
- package/assembly/index.ts +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 2025-05-14 - 1.0.7
|
|
4
|
+
|
|
5
|
+
- merge: pull request [#128](https://github.com/JairusSW/json-as/pull/128) from [loredanacirstea/nested-custom-serializer-fix](https://github.com/loredanacirstea/nested-custom-serializer-fix)
|
|
6
|
+
|
|
7
|
+
## 2025-05-12 - 1.0.6
|
|
8
|
+
|
|
9
|
+
- fix: support zero-param serialization and make sure types are consistent
|
|
10
|
+
- fix: [#124](https://github.com/JairusSW/json-as/issues/124)
|
|
11
|
+
|
|
3
12
|
## 2025-05-11 - 1.0.5
|
|
4
13
|
|
|
5
14
|
- feat: add sanity checks for badly formatted strings
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
7
7
|
█████ ███████ ██████ ██ ████ ██ ██ ███████
|
|
8
8
|
</span>
|
|
9
|
-
AssemblyScript - v1.0.
|
|
9
|
+
AssemblyScript - v1.0.7
|
|
10
10
|
</pre>
|
|
11
11
|
</h6>
|
|
12
12
|
|
|
@@ -57,7 +57,6 @@ If you'd like to see the code that the transform generates, run the build step w
|
|
|
57
57
|
```typescript
|
|
58
58
|
import { JSON } from "json-as";
|
|
59
59
|
|
|
60
|
-
|
|
61
60
|
@json
|
|
62
61
|
class Vec3 {
|
|
63
62
|
x: f32 = 0.0;
|
|
@@ -65,10 +64,8 @@ class Vec3 {
|
|
|
65
64
|
z: f32 = 0.0;
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
|
|
69
67
|
@json
|
|
70
68
|
class Player {
|
|
71
|
-
|
|
72
69
|
@alias("first name")
|
|
73
70
|
firstName!: string;
|
|
74
71
|
lastName!: string;
|
|
@@ -76,8 +73,6 @@ class Player {
|
|
|
76
73
|
// Drop in a code block, function, or expression that evaluates to a boolean
|
|
77
74
|
@omitif((self: Player) => self.age < 18)
|
|
78
75
|
age!: i32;
|
|
79
|
-
|
|
80
|
-
|
|
81
76
|
@omitnull()
|
|
82
77
|
pos!: Vec3 | null;
|
|
83
78
|
isVerified!: boolean;
|
|
@@ -114,12 +109,9 @@ This library allows selective omission of fields during serialization using the
|
|
|
114
109
|
This decorator excludes a field from serialization entirely.
|
|
115
110
|
|
|
116
111
|
```typescript
|
|
117
|
-
|
|
118
112
|
@json
|
|
119
113
|
class Example {
|
|
120
114
|
name!: string;
|
|
121
|
-
|
|
122
|
-
|
|
123
115
|
@omit
|
|
124
116
|
SSN!: string;
|
|
125
117
|
}
|
|
@@ -136,12 +128,9 @@ console.log(JSON.stringify(obj)); // { "name": "Jairus" }
|
|
|
136
128
|
This decorator omits a field only if its value is null.
|
|
137
129
|
|
|
138
130
|
```typescript
|
|
139
|
-
|
|
140
131
|
@json
|
|
141
132
|
class Example {
|
|
142
133
|
name!: string;
|
|
143
|
-
|
|
144
|
-
|
|
145
134
|
@omitnull()
|
|
146
135
|
optionalField!: string | null;
|
|
147
136
|
}
|
|
@@ -158,12 +147,9 @@ console.log(JSON.stringify(obj)); // { "name": "Jairus" }
|
|
|
158
147
|
This decorator omits a field based on a custom predicate function.
|
|
159
148
|
|
|
160
149
|
```typescript
|
|
161
|
-
|
|
162
150
|
@json
|
|
163
151
|
class Example {
|
|
164
152
|
name!: string;
|
|
165
|
-
|
|
166
|
-
|
|
167
153
|
@omitif((self: Example) => self.age <= 18)
|
|
168
154
|
age!: number;
|
|
169
155
|
}
|
|
@@ -188,7 +174,6 @@ AssemblyScript doesn't support using nullable primitive types, so instead, json-
|
|
|
188
174
|
For example, this schema won't compile in AssemblyScript:
|
|
189
175
|
|
|
190
176
|
```typescript
|
|
191
|
-
|
|
192
177
|
@json
|
|
193
178
|
class Person {
|
|
194
179
|
name!: string;
|
|
@@ -199,7 +184,6 @@ class Person {
|
|
|
199
184
|
Instead, use `JSON.Box` to allow nullable primitives:
|
|
200
185
|
|
|
201
186
|
```typescript
|
|
202
|
-
|
|
203
187
|
@json
|
|
204
188
|
class Person {
|
|
205
189
|
name: string;
|
|
@@ -262,7 +246,6 @@ More often, objects will be completely statically typed except for one or two va
|
|
|
262
246
|
In such cases, `JSON.Value` can be used to handle fields that may hold different types at runtime.
|
|
263
247
|
|
|
264
248
|
```typescript
|
|
265
|
-
|
|
266
249
|
@json
|
|
267
250
|
class DynamicObj {
|
|
268
251
|
id: i32 = 0;
|
|
@@ -319,7 +302,6 @@ Here's an example of creating a custom data type called `Point` which serializes
|
|
|
319
302
|
```typescript
|
|
320
303
|
import { bytes } from "json-as/assembly/util";
|
|
321
304
|
|
|
322
|
-
|
|
323
305
|
@json
|
|
324
306
|
class Point {
|
|
325
307
|
x: f64 = 0.0;
|
|
@@ -329,13 +311,11 @@ class Point {
|
|
|
329
311
|
this.y = y;
|
|
330
312
|
}
|
|
331
313
|
|
|
332
|
-
|
|
333
314
|
@serializer
|
|
334
315
|
serializer(self: Point): string {
|
|
335
316
|
return `(${self.x},${self.y})`;
|
|
336
317
|
}
|
|
337
318
|
|
|
338
|
-
|
|
339
319
|
@deserializer
|
|
340
320
|
deserializer(data: string): Point {
|
|
341
321
|
const dataSize = bytes(data);
|
|
@@ -365,8 +345,8 @@ The deserializer function parses the string `(x,y)` back into a `Point` instance
|
|
|
365
345
|
These functions are then wrapped before being consumed by the json-as library:
|
|
366
346
|
|
|
367
347
|
```typescript
|
|
368
|
-
@inline __SERIALIZE_CUSTOM(
|
|
369
|
-
const data = this.serializer(
|
|
348
|
+
@inline __SERIALIZE_CUSTOM(): void {
|
|
349
|
+
const data = this.serializer(this);
|
|
370
350
|
const dataSize = data.length << 1;
|
|
371
351
|
memory.copy(bs.offset, changetype<usize>(data), dataSize);
|
|
372
352
|
bs.offset += dataSize;
|
|
@@ -32,6 +32,14 @@ class Point {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
@json
|
|
36
|
+
export class ObjectWithCustom {
|
|
37
|
+
value: Point = new Point(0, 0)
|
|
38
|
+
constructor(value: Point) {
|
|
39
|
+
this.value = value
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
35
43
|
describe("Should serialize using custom serializers", () => {
|
|
36
44
|
expect(JSON.stringify<Point>(new Point(1, 2))).toBe("(1.0,2.0)");
|
|
37
45
|
});
|
|
@@ -41,3 +49,7 @@ describe("Should deserialize using custom deserializers", () => {
|
|
|
41
49
|
expect(p1.x.toString()).toBe("1.0");
|
|
42
50
|
expect(p1.y.toString()).toBe("2.0");
|
|
43
51
|
});
|
|
52
|
+
|
|
53
|
+
describe("Should serialize and deserialize using nested custom serializers", () => {
|
|
54
|
+
expect(JSON.stringify<ObjectWithCustom>(new ObjectWithCustom(new Point(1, 2)))).toBe(`{"value":(1.0,2.0)}`);
|
|
55
|
+
});
|
package/assembly/index.ts
CHANGED
|
@@ -552,7 +552,7 @@ export namespace JSON {
|
|
|
552
552
|
// @ts-ignore: Supplied by transform
|
|
553
553
|
} else if (isDefined(src.__SERIALIZE_CUSTOM)) {
|
|
554
554
|
// @ts-ignore
|
|
555
|
-
return src.__SERIALIZE_CUSTOM(
|
|
555
|
+
return src.__SERIALIZE_CUSTOM();
|
|
556
556
|
// @ts-ignore: Supplied by transform
|
|
557
557
|
} else if (isDefined(src.__SERIALIZE)) {
|
|
558
558
|
// @ts-ignore
|