-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjson.ts
225 lines (211 loc) · 7.9 KB
/
json.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
/**
* Functions for parsing JSONs to specific structures.
* @module
*/
import { Constructor } from "./types.ts";
import { isValid } from "./object.ts";
import { fromObject } from "./error.ts";
import bytes from "./bytes.ts";
const typeRegistry = new Map<Object, { [prop: string | symbol]: Constructor<any>; }>();
/**
* Converts a JSON string into an object of the given type.
*
* The type can be `String`, `Number`, `BigInt`, `Boolean`, `Date`, `Array`, `Object` or any
* class constructor (including Error types). If the class has a static
* `fromJSON(data: any): T` method, it will be invoked to create the instance (unless the
* parsed `data` is `null`, which will be skipped without further processing).
*
* This function generally does not perform loose conversion between types, for example,
* `parseAs('"123"', Number)` will not work, it only reverses to the same type before the
* data are encoded.
*
* However, for compatibility support, there are some exceptions allowed, which are:
*
* - `string` => `Date`
* - `number` or `string` => `bigint`
* - `array` => `Buffer` or `TypedArray` (e.g. `Uint8Array`), when the data only contains
* integers.
* - `object` => `Buffer` or `TypedArray` (e.g. `Uint8Array`), if the data are encoded by
* `JSON.stringify()`.
* - customized in `fromJSON()`
*
* If the data cannot be converted to the given type, this function returns `null`.
*/
export function parseAs(text: string, type: StringConstructor): string | null;
export function parseAs(text: string, type: NumberConstructor): number | null;
export function parseAs(text: string, type: BigIntConstructor): bigint | null;
export function parseAs(text: string, type: BooleanConstructor): boolean | null;
export function parseAs<T>(text: string, type: Constructor<T> & { fromJSON?(data: any): T; }): T | null;
export function parseAs<T>(
text: string,
type: Function & { fromJSON?(data: any): T; }
): T | null {
const data = JSON.parse(text);
return as(data, type as any) as T | null;
}
/**
* Converts the data into the given type.
*
* This function is primarily used in {@link parseAs} and shares the same conversion rules, but it
* can be used in other scenarios too, for example, inside the `fromJSON` function.
*/
export function as(data: unknown, type: StringConstructor): string | null;
export function as(data: unknown, type: NumberConstructor): number | null;
export function as(data: unknown, type: BigIntConstructor): bigint | null;
export function as(data: unknown, type: BooleanConstructor): boolean | null;
export function as<T>(data: unknown, type: Constructor<T> & { fromJSON?(data: any): T; }): T | null;
export function as(data: unknown, type: any): any {
if (data === null || data === undefined) {
return null;
} else if (typeof type.fromJSON === "function") {
return type.fromJSON(data);
} else if (typeof data === "boolean") {
if (type === Boolean) {
return data;
} else {
return null;
}
} else if (typeof data === "number") {
if (type === Number) {
return data;
} else if (type === BigInt) {
try {
return BigInt(data);
} catch {
return null;
}
} else {
return null;
}
} else if (typeof data === "string") {
if (type === String) {
return data;
} else if (type === Date) {
const date = new Date(data);
return isValid(date) ? date : null;
} else if (type === BigInt) {
try {
return BigInt(data);
} catch {
return null;
}
} else {
return null;
}
} else if (Array.isArray(data)) {
if (type === Array) { // Array
return data;
} else if (type.prototype instanceof Array) { // Array-derivative
return (type as ArrayConstructor).from(data);
} else if (typeof (type.prototype as any)[Symbol.iterator] === "function" &&
typeof (type as any)["from"] === "function"
) { // ArrayLike or TypedArray
try {
return (type as ArrayConstructor).from(data);
} catch {
return null;
}
} else {
return null;
}
} else if (!([String, Number, Boolean, Date, Array] as Function[]).includes(type)) {
if ((data as any).type === "Buffer" && Array.isArray((data as any).data)) {
// Node.js Buffer
if (typeof Buffer === "function" && type === Buffer) {
try {
return Buffer.from((data as any).data);
} catch {
return null;
}
} else if (typeof (type.prototype as any)[Symbol.iterator] === "function"
&& typeof (type as any)["from"] === "function"
) {
try {
// Convert Node.js Buffer to TypedArray.
return (type as ArrayConstructor).from((data as any).data);
} catch {
return null;
}
} else {
return null;
}
} else if ((data as any).type === "ByteArray" && Array.isArray((data as any).data)) {
// ByteArray
try {
return bytes((data as any).data);
} catch {
return null;
}
}
const keys = Object.getOwnPropertyNames(data);
const values = Object.values(data);
if (keys.slice(0, 50).map(Number).every(i => !Number.isNaN(i)) &&
values.slice(0, 50).map(Number).every(i => !Number.isNaN(i)) &&
typeof (type.prototype as any)[Symbol.iterator] === "function" &&
typeof (type as any)["from"] === "function"
) {
// Assert the data is a TypedArray.
try {
return (type as ArrayConstructor).from(Object.values(data));
} catch {
return null;
}
} else if (type.prototype instanceof Error) {
const err = fromObject(data);
if (err) {
// Support @JSON.type() decorator in Error constructors.
const typeRecords = typeRegistry.get(type.prototype as Object);
if (typeRecords) {
for (const key of Reflect.ownKeys(data)) {
const ctor = typeRecords[key];
if (ctor) {
(err as any)[key] = as((data as any)[key], ctor);
}
}
}
}
return err;
} else {
const ins = Object.create(type.prototype as Object);
const typeRecords = typeRegistry.get(type.prototype as Object);
if (typeRecords) {
for (const key of Reflect.ownKeys(data)) {
const ctor = typeRecords[key];
ins[key] = ctor ? as((data as any)[key], ctor) : (data as any)[key];
}
} else {
Object.assign(ins, data);
}
return ins;
}
} else {
return null;
}
}
/**
* A decorator to instruct that the target property in the class is of a specific type.
*
* When parsing JSON via {@link parseAs}, this property is guaranteed to be of the given type.
*
* **NOTE:** This decorator only supports TypeScript's `experimentalDecorators`.
*
* @example
* ```ts
* import { type } from "@ayonli/jsext/json";
*
* class Example {
* \@type(Date)
* date: Date;
* }
* ```
*/
export function type(ctor: Constructor<any>): PropertyDecorator {
return (proto, prop) => {
const record = typeRegistry.get(proto);
if (record) {
record[prop] = ctor;
} else {
typeRegistry.set(proto, { [prop]: ctor });
}
};
}