-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjson.test.ts
226 lines (179 loc) · 7.84 KB
/
json.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import "./augment.ts";
import { deepStrictEqual, strictEqual } from "node:assert";
import bytes, { ByteArray } from "./bytes.ts";
import { isDeno } from "./env.ts";
describe("JSON", () => {
describe("JSON.parseAs", () => {
it("null", () => {
strictEqual(JSON.parseAs("null", Object), null);
strictEqual(JSON.parseAs("null", Array), null);
});
it("boolean", () => {
strictEqual(JSON.parseAs("true", Boolean), true);
strictEqual(JSON.parseAs("false", Boolean), false);
strictEqual(JSON.parseAs("true", Number), null);
});
it("number", () => {
strictEqual(JSON.parseAs("123", Number), 123);
strictEqual(JSON.parseAs("123", BigInt), BigInt(123));
strictEqual(JSON.parseAs("123", String), null);
});
it("string", () => {
strictEqual(JSON.parseAs(`"Hello, World!"`, String), "Hello, World!");
const date = new Date();
deepStrictEqual(JSON.parseAs(`"${date.toISOString()}"`, Date), date);
strictEqual(JSON.parseAs(`"123"`, Number), null);
});
it("array", () => {
class MyArray extends Array { }
deepStrictEqual(JSON.parseAs(`[1,2,3]`, Array), [1, 2, 3]);
// @ts-ignore
deepStrictEqual(JSON.parseAs(`[1,2,3]`, MyArray), new MyArray<number>(1, 2, 3));
if (typeof Buffer === "function") {
deepStrictEqual(JSON.parseAs(`[1,2,3]`, Buffer), Buffer.from([1, 2, 3]));
}
strictEqual(JSON.parseAs(`[1,2,3]`, Object), null);
});
it("object", () => {
class Example {
constructor(public foo: string, public bar: Date) { }
static fromJSON(data: any) {
if (typeof data === "object" && !Array.isArray(data)) {
return new this(data.foo, new Date(data.bar));
} else {
return null;
}
}
}
class Example2 {
constructor(public foo: string) { }
}
deepStrictEqual(JSON.parseAs(`{"foo":"Hello","bar":"World"}`, Object), {
foo: "Hello",
bar: "World",
});
const date = new Date();
deepStrictEqual(
JSON.parseAs(`{"foo":"Hello","bar":"${date.toISOString()}"}`, Example),
new Example("Hello", date)
);
deepStrictEqual(JSON.parseAs(`{"foo":"Hello"}`, Example2), new Example2("Hello"));
deepStrictEqual(JSON.parseAs(`{"foo":"Hello"}`, String), null);
const tArr = Uint8Array.from([1, 2, 3]);
deepStrictEqual(JSON.parseAs(JSON.stringify(tArr), Uint8Array), tArr);
const bArr = bytes("Hello, World!");
deepStrictEqual(JSON.parseAs(JSON.stringify(bArr), ByteArray), bArr);
if (typeof Buffer === "function") {
const buf = Buffer.from([1, 2, 3]);
deepStrictEqual(JSON.parseAs(JSON.stringify(buf), Buffer), buf);
deepStrictEqual(JSON.parseAs(JSON.stringify(buf), Uint8Array), tArr);
}
const err = new Error("something went wrong");
const err2 = JSON.parseAs(JSON.stringify(err), Error);
strictEqual(err2?.name, err.name);
strictEqual(err2?.message, err.message);
strictEqual(err2?.stack, err.stack);
const err3 = new Exception("something is not right");
const err4 = JSON.parseAs(JSON.stringify(err3), Exception);
strictEqual(err4?.name, err3.name);
strictEqual(err4?.message, err3.message);
strictEqual(err4?.stack, err3.stack);
strictEqual(err4?.cause, err3.cause);
strictEqual(err4.code, err3.code);
});
});
describe("JSON.as", () => {
it("null", () => {
strictEqual(JSON.as(null, Object), null);
strictEqual(JSON.as(null, Array), null);
});
it("boolean", () => {
strictEqual(JSON.as(true, Boolean), true);
strictEqual(JSON.as(false, Boolean), false);
strictEqual(JSON.as(true, Number), null);
});
it("number", () => {
strictEqual(JSON.as(123, Number), 123);
strictEqual(JSON.as(123, BigInt), BigInt(123));
strictEqual(JSON.as(123, String), null);
});
it("string", () => {
strictEqual(JSON.as("Hello, World!", String), "Hello, World!");
const date = new Date();
deepStrictEqual(JSON.as(date.toISOString(), Date), date);
strictEqual(JSON.as("123", Number), null);
});
it("array", () => {
class MyArray extends Array { }
deepStrictEqual(JSON.as([1, 2, 3], Array), [1, 2, 3]);
// @ts-ignore
deepStrictEqual(JSON.as([1, 2, 3], MyArray), new MyArray<number>(1, 2, 3));
if (typeof Buffer === "function") {
deepStrictEqual(JSON.as([1, 2, 3], Buffer), Buffer.from([1, 2, 3]));
}
strictEqual(JSON.as([1, 2, 3], Object), null);
});
it("object", () => {
class Example {
constructor(public foo: string, public bar: Date) { }
static fromJSON(data: any) {
if (typeof data === "object" && !Array.isArray(data)) {
return new this(data.foo, new Date(data.bar));
} else {
return null;
}
}
}
class Example2 {
constructor(public foo: string) { }
}
deepStrictEqual(JSON.as({ "foo": "Hello", "bar": "World" }, Object), {
foo: "Hello",
bar: "World",
});
const date = new Date();
deepStrictEqual(
JSON.as({ "foo": "Hello", "bar": date.toISOString() }, Example),
new Example("Hello", date)
);
deepStrictEqual(JSON.as({ "foo": "Hello" }, Example2), new Example2("Hello"));
deepStrictEqual(JSON.as({ "foo": "Hello" }, String), null);
const tArr = Uint8Array.from([1, 2, 3]);
deepStrictEqual(JSON.as(JSON.parse(JSON.stringify(tArr)), Uint8Array), tArr);
if (typeof Buffer === "function") {
const buf = Buffer.from([1, 2, 3]);
deepStrictEqual(JSON.as(JSON.parse(JSON.stringify(buf)), Buffer), buf);
deepStrictEqual(JSON.as(JSON.parse(JSON.stringify(buf)), Uint8Array), tArr);
}
const err = new Error("something went wrong");
const err2 = JSON.as(JSON.parse(JSON.stringify(err)), Error);
strictEqual(err2?.name, err.name);
strictEqual(err2?.message, err.message);
strictEqual(err2?.stack, err.stack);
const err3 = new Exception("something is not right");
const err4 = JSON.as(JSON.parse(JSON.stringify(err3)), Exception);
strictEqual(err4?.name, err3.name);
strictEqual(err4?.message, err3.message);
strictEqual(err4?.stack, err3.stack);
strictEqual(err4?.cause, err3.cause);
strictEqual(err4.code, err3.code);
});
});
it("@JSON.type", function () {
if (isDeno) {
this.skip();
}
class Example {
@JSON.type(Date)
bar: Date | undefined;
constructor(public foo: string, bar: Date) {
this.bar = bar;
}
}
const date = new Date();
deepStrictEqual(
JSON.parseAs(`{"foo":"Hello","bar":"${date.toISOString()}"}`, Example),
new Example("Hello", date)
);
});
});