json-as 1.1.4 → 1.1.6

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,149 +0,0 @@
1
- // misc.spec.ts
2
-
3
- import { JSON } from "..";
4
- import { describe, expect } from "./lib";
5
-
6
- @json
7
- class Obj {
8
- a: string = "hello";
9
- b: string = "world";
10
- c: string = '"\t\f\u0000\u0001';
11
- }
12
-
13
- @json
14
- class Vec3 {
15
- x: f32 = 0.0;
16
- y: f32 = 0.0;
17
- z: f32 = 0.0;
18
- }
19
-
20
- @json
21
- class Player {
22
- @alias("first name")
23
- firstName!: string;
24
- lastName!: string;
25
- lastActive!: i32[];
26
- @omitif((self: Player) => self.age < 18)
27
- age!: i32;
28
- @omitnull()
29
- pos!: Vec3 | null;
30
- isVerified!: boolean;
31
- }
32
-
33
- @json class Point {}
34
-
35
- @json
36
- class NewPoint {
37
- x: f64 = 0.0;
38
- y: f64 = 0.0;
39
-
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 c = data.indexOf(",");
53
- const x = data.slice(2, c);
54
- const y = data.slice(c + 3);
55
- return new NewPoint(f64.parse(x), f64.parse(y));
56
- }
57
- }
58
-
59
- @json class InnerObj<T> { obj: T = instantiate<T>(); }
60
- @json class ObjWithBracketString { data: string = ""; }
61
-
62
- @json
63
- class Foo {
64
- id: string = "";
65
- firstName: string = "";
66
- lastName: string = "";
67
- }
68
-
69
- @json
70
- class SrvInfo {
71
- accessUrl: string = "https://example.com";
72
- cardTypes: i32[] = [1, 2, 3];
73
- customService: string = "Contact us at support@example.com";
74
- invoiceApplicationStatus: i32 = 1;
75
- isCertification: bool = true;
76
- isOnlineRecharge: bool = false;
77
- loginTypes: i32[] = [0, 1];
78
- record: string = "ICP 12345678";
79
- regCompanyAudit: i32 = 2;
80
- regCompanyPipeline: i32[] = [101, 102, 103];
81
- regPwdLimit: i32 = 8;
82
- serverTime: i64 = 1650000000000;
83
- srvDescription: string = "A demo service for handling customer operations.";
84
- srvHiddenMenu: string[] = ["admin", "beta"];
85
- srvHost: string = "srv.example.com";
86
- srvId: i32 = 999;
87
- srvKeywords: string[] = ["finance", "payments", "online"];
88
- srvLogoImgUrl: string = "https://example.com/logo.png";
89
- srvName: string = "ExampleService";
90
- srvPageId: i32 = 5;
91
- thirdAuthUrl: string = "https://auth.example.com";
92
- userCenterStyle: i32 = 1;
93
- }
94
-
95
- describe("Basic JSON.stringify behavior", () => {
96
- expect(JSON.stringify(new Obj())).toBe('{"a":"hello","b":"world","c":"\\"\\t\\f\\u0000\\u0001"}');
97
-
98
- const player: Player = {
99
- firstName: "Jairus",
100
- lastName: "Tanaka",
101
- lastActive: [2, 7, 2025],
102
- age: 18,
103
- pos: { x: 3.4, y: 1.2, z: 8.3 },
104
- isVerified: true,
105
- };
106
-
107
- expect(JSON.stringify(player)).toBe(
108
- '{"age":18,"pos":{"x":3.4,"y":1.2,"z":8.3},"first name":"Jairus","lastName":"Tanaka","lastActive":[2,7,2025],"isVerified":true}'
109
- );
110
-
111
- const raw = JSON.Raw.from('"hello world"');
112
- expect(JSON.stringify<JSON.Raw>(raw)).toBe('"hello world"');
113
-
114
- const emptyObj = new JSON.Obj();
115
- expect(JSON.stringify(emptyObj)).toBe("{}");
116
-
117
- emptyObj.set("x", 1.5);
118
- emptyObj.set("y", 5.4);
119
- emptyObj.set("z", 9.8);
120
- expect(JSON.stringify(emptyObj)).toBe('{"x":1.5,"y":5.4,"z":9.8}');
121
- });
122
-
123
- describe("Parse and stringify combinations", () => {
124
- const input = '{"x":3.4,"y":1.2,"z":8.3}';
125
- const parsed = JSON.parse<JSON.Obj>(input);
126
- expect(JSON.stringify(parsed)).toBe(input);
127
-
128
- const strange = JSON.parse<InnerObj<ObjWithBracketString>>('{"obj":{"data":"hello} world"}}');
129
- expect(JSON.stringify(strange)).toBe('{"obj":{"data":"hello} world"}}');
130
-
131
- const point = new Point();
132
- expect(JSON.stringify(point)).toBe('{}');
133
- expect(JSON.stringify(JSON.parse<Point>(JSON.stringify(point)))).toBe('{}');
134
-
135
- const np = new NewPoint(1.0, 2.0);
136
- const ser = JSON.stringify(np);
137
- expect(ser).toBe('x=1.0,y=2.0');
138
- expect(JSON.stringify(JSON.parse<NewPoint>(ser))).toBe('x=1.0,y=2.0');
139
- });
140
-
141
- describe("SrvInfo consistency", () => {
142
- const srv = new SrvInfo();
143
- const serialized = JSON.stringify(srv);
144
- const expected = '{"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}';
145
-
146
- expect(serialized).toBe(expected);
147
- const parsed = JSON.parse<SrvInfo>(serialized);
148
- expect(JSON.stringify(parsed)).toBe(expected);
149
- });