json-as 1.1.8 → 1.1.10

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.
@@ -1,122 +1,3 @@
1
1
  import { JSON } from "../";
2
2
  import { describe, expect } from "./lib";
3
3
  import { DerivedObject, Null, ObjWithStrangeKey, ObjectWithFloat, OmitIf, Player, Vec3 } from "./types";
4
-
5
- // describe("Should serialize objects", () => {
6
- // expect(
7
- // JSON.stringify<Vec3>({
8
- // x: 3.4,
9
- // y: 1.2,
10
- // z: 8.3,
11
- // }),
12
- // ).toBe('{"x":3.4,"y":1.2,"z":8.3}');
13
-
14
- // expect(
15
- // JSON.stringify<Player>({
16
- // firstName: "Emmet",
17
- // lastName: "West",
18
- // lastActive: [8, 27, 2022],
19
- // age: 23,
20
- // pos: {
21
- // x: 3.4,
22
- // y: 1.2,
23
- // z: 8.3,
24
- // },
25
- // isVerified: true,
26
- // }),
27
- // ).toBe('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}');
28
-
29
- // expect(JSON.stringify<ObjectWithFloat>({ f: 7.23 })).toBe('{"f":7.23}');
30
-
31
- // expect(JSON.stringify<ObjectWithFloat>({ f: 0.000001 })).toBe('{"f":0.000001}');
32
-
33
- // expect(JSON.stringify<ObjectWithFloat>({ f: 1e-7 })).toBe('{"f":1e-7}');
34
-
35
- // expect(JSON.stringify<ObjectWithFloat>({ f: 1e20 })).toBe('{"f":100000000000000000000.0}');
36
-
37
- // expect(JSON.stringify<ObjectWithFloat>({ f: 1e21 })).toBe('{"f":1e+21}');
38
-
39
- // expect(JSON.stringify<ObjWithStrangeKey<string>>({ data: "foo" })).toBe('{"a\\\\\\t\\"\\u0002b`c":"foo"}');
40
- // });
41
-
42
- // describe("Should serialize @omit'ed objects", () => {
43
- // expect(
44
- // JSON.stringify(<OmitIf>{
45
- // y: 1,
46
- // }),
47
- // ).toBe('{"x":1,"y":1,"z":1}');
48
- // });
49
- // describe("Should deserialize class inheritance", () => {
50
- // const jsonStr = '{"a":"1","b":"2"}';
51
- // const obj = JSON.parse<DerivedObject>(jsonStr);
52
-
53
- // expect((obj instanceof DerivedObject).toString()).toBe("true");
54
- // expect(obj.a).toBe("1");
55
- // expect(obj.b).toBe("2");
56
- // });
57
-
58
- // describe("Should deserialize nulls", () => {
59
- // expect(JSON.stringify(JSON.parse<Null>("null"))).toBe("null");
60
- // });
61
-
62
- // describe("Should deserialize integer arrays", () => {
63
- // expect(JSON.stringify(JSON.parse<u32[]>("[0,100,101]"))).toBe(JSON.stringify([0, 100, 101]));
64
-
65
- // expect(JSON.stringify(JSON.parse<i32[]>("[0,100,101,-100,-101]"))).toBe(JSON.stringify([0, 100, 101, -100, -101]));
66
- // });
67
-
68
- // describe("Should deserialize float arrays", () => {
69
- // expect(JSON.stringify(JSON.parse<f64[]>("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]"))).toBe(JSON.stringify([7.23, 1000.0, 1000.0, 1.23456, 1.23456, 0.0, 7.23]));
70
-
71
- // expect(JSON.stringify(JSON.parse<f64[]>("[1e+21,1e+22,1e-7,1e-8,1e-9]"))).toBe(JSON.stringify([1e21, 1e22, 1e-7, 1e-8, 1e-9]));
72
- // });
73
-
74
- // describe("Should deserialize boolean arrays", () => {
75
- // expect(JSON.stringify(JSON.parse<boolean[]>("[true,false]"))).toBe(JSON.stringify([true, false]));
76
- // });
77
-
78
- // describe("Should deserialize string arrays", () => {
79
- // expect(JSON.stringify(JSON.parse<string[]>('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]'))).toBe(JSON.stringify(['string "with random spa\nces and \nnewlines\n\n\n']));
80
- // });
81
-
82
- // describe("Should deserialize nested integer arrays", () => {
83
- // expect(JSON.stringify(JSON.parse<i64[][]>("[[100,101],[-100,-101],[0]]"))).toBe(JSON.stringify([[100, 101], [-100, -101], [0]]));
84
- // });
85
-
86
- // describe("Should deserialize nested float arrays", () => {
87
- // expect(JSON.stringify(JSON.parse<f64[][]>("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]"))).toBe(JSON.stringify([[7.23], [1000.0], [1000.0], [1.23456], [1.23456], [0.0], [7.23]]));
88
- // });
89
-
90
- // describe("Should deserialize nested boolean arrays", () => {
91
- // expect(JSON.stringify(JSON.parse<boolean[][]>("[[true],[false]]"))).toBe(JSON.stringify([[true], [false]]));
92
- // });
93
-
94
- // describe("Should deserialize object arrays", () => {
95
- // expect(JSON.stringify(JSON.parse<Vec3[]>('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]'))).toBe(
96
- // JSON.stringify(<Vec3[]>[
97
- // { x: 3.4, y: 1.2, z: 8.3 },
98
- // { x: 3.4, y: -2.1, z: 9.3 },
99
- // ]),
100
- // );
101
- // });
102
-
103
- // describe("Should deserialize Objects", () => {
104
- // expect(JSON.stringify(JSON.parse<Vec3>('{"x":3.4,"y":1.2,"z":8.3}'))).toBe(JSON.stringify(<Vec3>{ x: 3.4, y: 1.2, z: 8.3 }));
105
-
106
- // expect(JSON.stringify(JSON.parse<Player>('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}'))).toBe(
107
- // JSON.stringify(<Player>{
108
- // firstName: "Emmet",
109
- // lastName: "West",
110
- // lastActive: [8, 27, 2022],
111
- // age: 23,
112
- // pos: { x: 3.4, y: 1.2, z: 8.3 },
113
- // isVerified: true,
114
- // }),
115
- // );
116
-
117
- // expect(JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":7.23}'))).toBe(JSON.stringify(<ObjectWithFloat>{ f: 7.23 }));
118
-
119
- // expect(JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":0.000001}'))).toBe(JSON.stringify(<ObjectWithFloat>{ f: 0.000001 }));
120
-
121
- // expect(JSON.stringify(JSON.parse<ObjWithStrangeKey<string>>('{"a\\\\\\t\\"\\u0002b`c":"foo"}'))).toBe(JSON.stringify(<ObjWithStrangeKey<string>>{ data: "foo" }));
122
- // });
package/assembly/index.ts CHANGED
@@ -424,7 +424,7 @@ export namespace JSON {
424
424
  // @ts-ignore: type
425
425
  private storage: Map<string, JSON.Value> = new Map<string, JSON.Value>();
426
426
 
427
- constructor() { }
427
+ constructor() {}
428
428
 
429
429
  // @ts-ignore: decorator
430
430
  @inline get size(): i32 {
@@ -603,6 +603,8 @@ export namespace JSON {
603
603
  if (isDefined(type.__DESERIALIZE)) {
604
604
  const out = changetype<nonnull<T>>(dst || __new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
605
605
  // @ts-ignore: Defined by transform
606
+ if (isDefined(type.__INITIALIZE)) out.__INITIALIZE();
607
+ // @ts-ignore: Defined by transform
606
608
  return out.__DESERIALIZE(srcStart, srcEnd, out);
607
609
  } else if (type instanceof Map) {
608
610
  // @ts-ignore: type
@@ -626,7 +628,7 @@ export namespace JSON {
626
628
  }
627
629
  throw new Error(`Could not deserialize data '${ptrToStr(srcStart, srcEnd).slice(0, 100)}' to type. Make sure to add the correct decorators to classes.`);
628
630
  }
629
- namespace Util {
631
+ export namespace Util {
630
632
  // @ts-ignore: decorator
631
633
  @inline export function isSpace(code: u16): boolean {
632
634
  return code == 0x20 || code - 9 <= 4;
package/assembly/test.ts CHANGED
@@ -1,292 +1,182 @@
1
1
  import { JSON } from ".";
2
- import { bytes } from "./util";
3
-
4
- // @json
5
- // class Obj {
6
- // public a: string = "hello";
7
- // public b: string = "world";
8
- // public c: string = '"\t\f\u0000\u0001';
9
- // }
10
-
11
- // @json
12
- // class Vec3 {
13
- // x: f32 = 0.0;
14
- // y: f32 = 0.0;
15
- // z: f32 = 0.0;
16
- // }
17
-
18
- // @json
19
- // class Player {
20
- // @alias("first name")
21
- // firstName!: string;
22
- // lastName!: string;
23
- // lastActive!: i32[];
24
- // // Drop in a code block, function, or expression that evaluates to a boolean
25
- // @omitif((self: Player) => self.age < 18)
26
- // age!: i32;
27
-
28
- // @omitnull()
29
- // pos!: Vec3 | null;
30
- // isVerified!: boolean;
31
- // }
32
-
33
- // @json
34
- // class Point { }
2
+ import { expect, it } from "./__tests__/lib";
3
+
4
+ it("should parse", () => {
5
+ const str = `{
6
+ "id": "chatcmpl-BbvlnP0ESWa8OForeEjt7AkoIuh3Q",
7
+ "object": "chat.completion",
8
+ "created": 1748379903,
9
+ "model": "gpt-4o-mini-2024-07-18",
10
+ "choices": [
11
+ {
12
+ "index": 0,
13
+ "message": {
14
+ "role": "assistant",
15
+ "content": "Hello! How can I assist you today?",
16
+ "refusal": null,
17
+ "annotations": []
18
+ },
19
+ "logprobs": null,
20
+ "finish_reason": "stop"
21
+ }
22
+ ],
23
+ "usage": {
24
+ "prompt_tokens": 15,
25
+ "completion_tokens": 9,
26
+ "total_tokens": 24,
27
+ "prompt_tokens_details": {
28
+ "cached_tokens": 0,
29
+ "audio_tokens": 0
30
+ },
31
+ "completion_tokens_details": {
32
+ "reasoning_tokens": 0,
33
+ "audio_tokens": 0,
34
+ "accepted_prediction_tokens": 0,
35
+ "rejected_prediction_tokens": 0
36
+ }
37
+ },
38
+ "service_tier": "default",
39
+ "system_fingerprint": "fp_34a54ae93c"
40
+ }`;
41
+
42
+ const output = JSON.parse<OpenAIChatOutput>(str);
43
+ expect(output.id).toBe("chatcmpl-BbvlnP0ESWa8OForeEjt7AkoIuh3Q");
44
+ expect(output.object).toBe("chat.completion");
45
+ expect(output.created.getTime()).toBe(1748379903000);
46
+ expect(output.model).toBe("gpt-4o-mini-2024-07-18");
47
+ expect(output.choices.length).toBe(1);
48
+
49
+ const choice = output.choices[0];
50
+ expect(choice.index).toBe(0);
51
+ expect(choice.message.content).toBe("Hello! How can I assist you today?");
52
+ // expect(choice.message.refusal).toBe("null");
53
+ // expect(choice.logprobs).toBe("null");
54
+ // expect(choice.finishReason).toBe("stop");
55
+
56
+ // expect(output.usage.promptTokens).toBe(15);
57
+ // expect(output.usage.completionTokens).toBe(9);
58
+ // expect(output.usage.totalTokens).toBe(24);
59
+ // expect(output.serviceTier).toBe("default");
60
+ // expect(output.systemFingerprint).toBe("fp_34a54ae93c");
61
+ });
35
62
 
36
- // @json
37
- // class NewPoint {
38
- // x: f64 = 0.0;
39
- // y: f64 = 0.0;
40
- // constructor(x: f64, y: f64) {
41
- // this.x = x;
42
- // this.y = y;
43
- // }
44
-
45
- // @serializer
46
- // serializer(self: NewPoint): string {
47
- // return `x=${self.x},y=${self.y}`;
48
- // }
49
-
50
- // @deserializer
51
- // deserializer(data: string): NewPoint {
52
- // const dataSize = bytes(data);
53
-
54
- // const c = data.indexOf(",");
55
- // const x = data.slice(2, c);
56
- // const y = data.slice(c + 3);
57
-
58
- // return new NewPoint(f64.parse(x), f64.parse(y));
59
- // }
60
- // }
61
-
62
- // @json
63
- // class InnerObj<T> {
64
- // obj: T = instantiate<T>();
65
- // }
66
-
67
- // @json
68
- // class ObjWithBracketString {
69
- // data: string = "";
70
- // }
71
-
72
- // const player: Player = {
73
- // firstName: "Jairus",
74
- // lastName: "Tanaka",
75
- // lastActive: [2, 7, 2025],
76
- // age: 18,
77
- // pos: {
78
- // x: 3.4,
79
- // y: 1.2,
80
- // z: 8.3,
81
- // },
82
- // isVerified: true,
83
- // };
84
-
85
- // const a1 = JSON.stringify("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000a\u000b\u000c\u000d\u000e\u000f\u000f\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f");
86
- // // console.log("Bytes " + bytes(a1).toString());
87
- // console.log("a1: " + a1);
88
-
89
- // const obj = new Obj();
90
- // const a2 = JSON.stringify(obj);
91
- // // console.log("Bytes " + bytes(a2).toString());
92
- // console.log("a2: " + a2);
93
-
94
- // const a3 = JSON.stringify(player);
95
- // // console.log("Bytes " + bytes(a3).toString());
96
- // console.log("a3: " + a3);
97
-
98
- // const a4 = new JSON.Obj();
99
-
100
- // a4.set("x", 1.5);
101
- // a4.set("y", 5.4);
102
- // a4.set("z", 9.8);
103
- // a4.set("obj", obj);
104
- // a4.set<boolean>("bool", false);
105
-
106
- // console.log("a4: " + JSON.stringify(a4));
107
-
108
- // const a5 = JSON.parse<JSON.Obj>('{"foo":"bar"}');
109
-
110
- // console.log("a5: " + JSON.stringify(a5));
111
-
112
- // const a6 = JSON.parse<JSON.Obj>('{"x":1.5,"y":5.4,"z":9.8,"obj":{"foo":"bar"}}');
113
-
114
- // console.log("a6: " + JSON.stringify(a6));
115
-
116
- // const a7 = JSON.parse<JSON.Value[]>('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0},[1,2,3,true]]');
117
-
118
- // console.log("a7: " + JSON.stringify(a7));
119
-
120
- // const a8 = JSON.stringify(["hello", JSON.stringify("world"), "working?"]);
121
-
122
- // console.log("a8: " + a8);
123
-
124
- // const a9 = JSON.stringify<JSON.Raw>(JSON.Raw.from('"hello world"'));
125
-
126
- // console.log("a9: " + a9);
63
+ @json
64
+ class OpenAIChatOutput {
65
+ id!: string;
127
66
 
128
- // const m10 = new Map<string, JSON.Raw>();
129
- // m10.set("hello", new JSON.Raw('"world"'));
130
- // m10.set("pos", new JSON.Raw('{"x":1.0,"y":2.0,"z":3.0}'));
67
+ object!: string;
131
68
 
132
- // const a10 = JSON.stringify(m10);
69
+ choices: Choice[] = [];
133
70
 
134
- // console.log("a10: " + a10);
71
+ get created(): Date {
72
+ return new Date(this._created * 1000);
73
+ }
135
74
 
136
- // const a11 = JSON.parse<JSON.Obj>(' { "x" : 3.4 , "y" : 1.2 , "z" : 8.3 } ');
75
+ @alias("created")
76
+ private _created!: i64;
137
77
 
138
- // console.log("a11: " + JSON.stringify(a11));
78
+ model!: string;
139
79
 
140
- // const a12 = JSON.parse<InnerObj<ObjWithBracketString>>('{"obj":{"data":"hello} world"}}');
80
+ @alias("service_tier")
81
+ serviceTier: string | null = null;
141
82
 
142
- // console.log("a12: " + JSON.stringify(a12));
83
+ @alias("system_fingerprint")
84
+ systemFingerprint!: string;
143
85
 
144
- // const a13 = JSON.stringify<JSON.Obj>(new JSON.Obj());
86
+ usage!: Usage;
87
+ }
145
88
 
146
- // console.log("a13: " + a13);
89
+ @json
90
+ class ToolCall {
91
+ id!: string;
147
92
 
148
- // const a14 = JSON.stringify(new Point());
149
- // console.log("a14: " + a14);
93
+ type: string = "function";
150
94
 
151
- // const a15 = JSON.parse<Point>(a14);
152
- // console.log("a15: " + JSON.stringify(a15));
95
+ function!: FunctionCall;
96
+ }
153
97
 
154
- // const a16 = JSON.stringify(new NewPoint(1.0, 2.0));
155
- // console.log("a16: " + a16);
98
+ @json
99
+ class FunctionCall {
100
+ name!: string;
156
101
 
157
- // const a17 = JSON.parse<NewPoint>(a16);
158
- // console.log("a17: " + JSON.stringify(a17));
102
+ arguments!: string;
103
+ }
159
104
 
160
- // const a18 = JSON.parse<JSON.Obj[]>('[{"x":1.0,"y":2.0,"z":3.0},{"x":4.0,"y":5.0,"z":6.0},{"x":7.0,"y":8.0,"z":9.0}]');
161
- // console.log("a18: " + JSON.stringify(a18));
105
+ @json
106
+ class Usage {
107
+ @alias("completion_tokens")
108
+ completionTokens!: i32;
162
109
 
163
- // const a19 = JSON.stringify<JSON.Obj[]>(a18);
164
- // console.log("a19: " + a19);
110
+ @alias("prompt_tokens")
111
+ promptTokens!: i32;
165
112
 
166
- // const a20 = JSON.parse<JSON.Box<f64>[]>("[1.3,4.7,9.5]");
167
- // console.log("a20: " + JSON.stringify(a20));
113
+ @alias("total_tokens")
114
+ totalTokens!: i32;
115
+ }
168
116
 
169
- // const a21 = JSON.stringify<JSON.Box<f64>[]>(a20);
170
- // console.log("a21: " + a21);
117
+ @json
118
+ class Choice {
119
+ @alias("finish_reason")
120
+ finishReason!: string;
171
121
 
172
- // const a22 = JSON.parse<Foo>('{"id":"0xb8","firstName":"Jairus","lastName":"Tanaka"}');
173
- // console.log("a22: " + JSON.stringify(a22));
122
+ index!: i32;
174
123
 
124
+ message: CompletionMessage = new CompletionMessage();
175
125
 
176
- // @json
177
- // class Foo {
178
- // id: string = "";
179
- // firstName: string = "";
180
- // lastName: string = "";
181
- // }
126
+ logprobs!: Logprobs | null;
127
+ }
182
128
 
183
- // @json
184
- // class SrvInfo {
185
- // accessUrl: string = "https://example.com";
186
- // cardTypes: i32[] = [1, 2, 3];
187
- // customService: string = "Contact us at support@example.com";
188
- // invoiceApplicationStatus: i32 = 1;
189
- // isCertification: bool = true;
190
- // isOnlineRecharge: bool = false;
191
- // loginTypes: i32[] = [0, 1]; // e.g. 0 = password, 1 = OTP
192
- // record: string = "ICP 12345678";
193
- // regCompanyAudit: i32 = 2;
194
- // regCompanyPipeline: i32[] = [101, 102, 103];
195
- // regPwdLimit: i32 = 8; // min password length
196
- // serverTime: i64 = 1650000000000; // dummy timestamp
197
- // srvDescription: string = "A demo service for handling customer operations.";
198
- // srvHiddenMenu: string[] = ["admin", "beta"];
199
- // srvHost: string = "srv.example.com";
200
- // srvId: i32 = 999;
201
- // srvKeywords: string[] = ["finance", "payments", "online"];
202
- // srvLogoImgUrl: string = "https://example.com/logo.png";
203
- // srvName: string = "ExampleService";
204
- // srvPageId: i32 = 5;
205
- // thirdAuthUrl: string = "https://auth.example.com";
206
- // userCenterStyle: i32 = 1; // e.g. 1 = modern, 0 = legacy
207
- // }
129
+ @json
130
+ class Logprobs {
131
+ content: LogprobsContent[] | null = null;
132
+ }
208
133
 
209
- // const a23 = JSON.stringify(new SrvInfo());
210
- // console.log("a23: " + a23);
134
+ @json
135
+ class LogprobsContent {
136
+ token!: string;
211
137
 
212
- // const a24 = '{"accessUrl":"https://example.com","cardTypes":[1,2,3],"customService":"Contact us at support@example.com","invoiceApplicationStatus":1,"isCertification":true,"isOnlineRecharge":false,"loginTypes":[0,1],"record":"ICP 12345678","regCompanyAudit":2,"regCompanyPipeline":[101,102,103],"regPwdLimit":8,"serverTime":1650000000000,"srvDescription":"A demo service for handling customer operations.","srvHiddenMenu":["admin","beta"],"srvHost":"srv.example.com","srvId":999,"srvKeywords":["finance","payments","online"],"srvLogoImgUrl":"https://example.com/logo.png","srvName":"ExampleService","srvPageId":5,"thirdAuthUrl":"https://auth.example.com","userCenterStyle":1}';
213
- // console.log("a25: " + (a24 == a23).toString());
138
+ logprob!: f64;
214
139
 
215
- // const a26 = JSON.parse<SrvInfo>(a23);
216
- // console.log("a26: " + JSON.stringify(a26));
140
+ bytes!: u8[] | null;
217
141
 
218
- // console.log("a27: " + (JSON.stringify(a26) == a23).toString())
219
- import {
220
- bs
221
- } from "../lib/as-bs"
142
+ @alias("top_logprobs")
143
+ topLogprobs!: TopLogprobsContent[];
144
+ }
222
145
 
223
146
  @json
224
- class GenericEnum<T> {
225
- private tag: string = ""
226
- private value: T | null = null
227
-
228
- constructor() {
229
- this.tag = ""
230
- this.value = null
231
- }
147
+ class TopLogprobsContent {
148
+ token!: string;
232
149
 
233
- static create<T>(tag: string, value: T): GenericEnum<T> {
234
- const item = new GenericEnum<T>()
235
- item.tag = tag
236
- item.value = value
237
- return item
238
- }
150
+ logprob!: f64;
239
151
 
240
- getTag(): string {
241
- return this.tag
242
- }
152
+ bytes!: u8[] | null;
153
+ }
243
154
 
244
- getValue(): T | null {
245
- return this.value
246
- }
247
- @serializer
248
- serialize<T>(self: GenericEnum<T>): string {
249
- const tagJson = JSON.stringify(self.tag);
250
- const valueJson = JSON.stringify(self.value);
251
- return `{${tagJson}:${valueJson}}`
252
- }
253
- @deserializer
254
- deserialize(data: string): GenericEnum<T> {
255
- const parsed = JSON.parse<Map<string, JSON.Raw>>(data);
256
- const result = new GenericEnum<T>();
155
+ @json
156
+ class CompletionMessage {
157
+ content!: string;
257
158
 
258
- const keys = parsed.keys();
259
- const values = parsed.values();
159
+ @omitnull()
160
+ refusal: string | null = null;
260
161
 
261
- result.tag = keys[0];
262
- result.value = JSON.parse<T>(values[0].data);
162
+ @alias("tool_calls")
163
+ @omitif((self: CompletionMessage) => self.toolCalls.length == 0)
164
+ toolCalls: ToolCall[] = [];
263
165
 
264
- return result;
265
- }
166
+ @omitnull()
167
+ audio: AudioOutput | null = null;
266
168
  }
267
169
 
268
170
  @json
269
- class Node<T> {
270
- name: string
271
- id: u32
272
- data: T
273
-
274
- constructor() {
275
- this.name = ""
276
- this.id = 0
277
- this.data = changetype<T>(0);
278
- }
279
- }
280
-
281
- const enumValue = GenericEnum.create<string>("success", "Hello World")
171
+ class AudioOutput {
172
+ id!: string;
282
173
 
283
- const node = new Node<GenericEnum<string>>();
284
- node.name = "test-node";
285
- node.id = 42;
286
- node.data = enumValue;
174
+ get expiresAt(): Date {
175
+ return new Date(this._expiresAt * 1000);
176
+ }
287
177
 
288
- const serialized = JSON.stringify(node);
289
- console.log("Serialized Node: " + serialized);
178
+ @alias("expires_at")
179
+ private _expiresAt!: i64;
290
180
 
291
- const deserialized = JSON.parse<JSON.Obj>(serialized)
292
- console.log("Deserialized Node: " + JSON.stringify(deserialized));
181
+ transcript!: string;
182
+ }
package/assembly/types.ts CHANGED
@@ -0,0 +1,70 @@
1
+ import { JSON } from "."
2
+
3
+ @json
4
+ export class GenericEnum<T> {
5
+ private tag: string = ""
6
+ private value: T | null = null
7
+
8
+ constructor() {
9
+ this.tag = ""
10
+ this.value = null
11
+ }
12
+
13
+ static create<T>(tag: string, value: T): GenericEnum<T> {
14
+ const item = new GenericEnum<T>()
15
+ item.tag = tag
16
+ item.value = value
17
+ return item
18
+ }
19
+
20
+ getTag(): string {
21
+ return this.tag
22
+ }
23
+
24
+ getValue(): T | null {
25
+ return this.value
26
+ }
27
+ @serializer
28
+ serialize<T>(self: GenericEnum<T>): string {
29
+ const tagJson = JSON.stringify(self.tag);
30
+ const valueJson = JSON.stringify(self.value);
31
+ return `{${tagJson}:${valueJson}}`
32
+ }
33
+ @deserializer
34
+ deserialize(data: string): GenericEnum<T> {
35
+ const parsed = JSON.parse<Map<string, JSON.Raw>>(data);
36
+ const result = new GenericEnum<T>();
37
+
38
+ const keys = parsed.keys();
39
+ const values = parsed.values();
40
+
41
+ result.tag = keys[0];
42
+ result.value = JSON.parse<T>(values[0].data);
43
+
44
+ return result;
45
+ }
46
+ }
47
+
48
+ @json
49
+ export class Node<T> {
50
+ name: string
51
+ id: u32
52
+ data: T
53
+
54
+ constructor() {
55
+ this.name = ""
56
+ this.id = 0
57
+ this.data = changetype<T>(0);
58
+ }
59
+ }
60
+
61
+ @json
62
+ export class Vec3 {
63
+ x: f32 = 0.0;
64
+ y: f32 = 0.0;
65
+ z: f32 = 0.0;
66
+ }
67
+
68
+
69
+ @json
70
+ export class Point { }
package/index.ts CHANGED
@@ -1 +1 @@
1
- export { JSON } from "./assembly/index";
1
+ export { JSON } from "./assembly/index";