forked from brian9206/json-typescript-mapper
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
317 lines (271 loc) · 8.49 KB
/
index.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import 'reflect-metadata';
import {isTargetType, isPrimitiveOrPrimitiveClass, isArrayOrArrayClass} from './libs/utils';
/**
* provide interface to indicate the object is allowed to be traversed
*
* @interface
*/
export interface IGenericObject {
[key: string]: any;
}
/**
* Decorator variable name
*
* @const
*/
const JSON_META_DATA_KEY = 'JsonProperty';
/**
* When custom mapping of a property is required.
*
* @interface
*/
export interface ICustomConverter {
fromJson(data: any): any;
toJson(data: any): any;
}
/**
* IDecoratorMetaData<T>
* DecoratorConstraint
*
* @interface
* @property {ICustomConverter} customConverter, will be used for mapping the property, if specified
* @property {boolean} excludeToJson, will exclude the property for serialization, if true
*/
export interface IDecoratorMetaData<T> {
name?: string,
clazz?: {new(): T},
customConverter?: ICustomConverter,
excludeToJson?: boolean
}
/**
* DecoratorMetaData
* Model used for decoration parameters
*
* @class
* @property {string} name, indicate which json property needed to map
* @property {string} clazz, if the target is not primitive type, map it to corresponding class
*/
class DecoratorMetaData<T> {
constructor(public name?: string, public clazz?: {new(): T}) {
}
}
/**
* JsonProperty
*
* @function
* @property {IDecoratorMetaData<T>|string} metadata, encapsulate it to DecoratorMetaData for standard use
* @return {(target:Object, targetKey:string | symbol)=> void} decorator function
*/
export function JsonProperty<T>(metadata?: IDecoratorMetaData<T>|string): (target: Object, targetKey: string | symbol)=> void {
let decoratorMetaData: IDecoratorMetaData<T>;
if (isTargetType(metadata, 'string')) {
decoratorMetaData = new DecoratorMetaData<T>(metadata as string);
}
else if (isTargetType(metadata, 'object')) {
decoratorMetaData = metadata as IDecoratorMetaData<T>;
}
else {
throw new Error('index.ts: meta data in Json property is undefined. meta data: ' + metadata)
}
return Reflect.metadata(JSON_META_DATA_KEY, decoratorMetaData);
}
/**
* getClazz
*
* @function
* @property {any} target object
* @property {string} propertyKey, used as target property
* @return {Function} Function/Class indicate the target property type
* @description Used for type checking, if it is not primitive type, loop inside recursively
*/
function getClazz<T>(target: T, propertyKey: string): {new(): T} {
return Reflect.getMetadata('design:type', target, propertyKey)
}
/**
* getJsonProperty
*
* @function
* @property {any} target object
* @property {string} propertyKey, used as target property
* @return {IDecoratorMetaData<T>} Obtain target property decorator meta data
*/
function getJsonProperty<T>(target: any, propertyKey: string): IDecoratorMetaData<T> {
return Reflect.getMetadata(JSON_META_DATA_KEY, target, propertyKey);
}
/**
* hasAnyNullOrUndefined
*
* @function
* @property {...args:any[]} any arguments
* @return {IDecoratorMetaData<T>} check if any arguments is null or undefined
*/
function hasAnyNullOrUndefined(...args: any[]) {
return args.some((arg: any) => arg === null || arg === undefined);
}
function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T, json: IGenericObject, key: any): any {
/**
* if decorator name is not found, use target property key as decorator name. It means mapping it directly
*/
let decoratorName = decoratorMetadata.name || key;
let innerJson: any = json ? json[decoratorName] : undefined;
let clazz = getClazz(instance, key);
if (isArrayOrArrayClass(clazz)) {
let metadata = getJsonProperty(instance, key);
if (metadata && metadata.clazz || isPrimitiveOrPrimitiveClass(clazz)) {
if (innerJson && isArrayOrArrayClass(innerJson)) {
return innerJson.map(
(item: any) => deserialize(metadata.clazz, item)
);
}
return;
} else {
return innerJson;
}
}
if (!isPrimitiveOrPrimitiveClass(clazz)) {
return deserialize(clazz, innerJson);
}
// handle boolean
if (clazz as any === Boolean as any) {
if (typeof json[decoratorName] === 'undefined') {
return undefined;
}
let val = json[decoratorName];
// allow string/number
return val >= 1 || val === true;
}
return typeof json[decoratorName] === 'undefined' ? undefined : json[decoratorName];
}
/**
* deserialize
*
* @function
* @param {{new():T}} clazz, class type which is going to initialize and hold a mapping json
* @param {Object} json, input json object which to be mapped
*
* @return {T} return mapped object
*/
export function deserialize<T extends IGenericObject>(Clazz: {new(): T}, json: IGenericObject): T {
if (json === null) {
return null;
}
/**
* As it is a recursive function, ignore any arguments that are unset
*/
if (hasAnyNullOrUndefined(Clazz, json)) {
return void 0;
}
/**
* Prevent non-json continue
*/
if (!isTargetType(json, 'object')) {
// convert date
let date = new Date(json as any);
if (isNaN(date.getTime())) {
return void 0;
}
else {
return date as any;
}
}
/**
* init root class to contain json
*/
let instance = new Clazz();
Object.keys(instance).forEach((key: string) => {
/**
* get decoratorMetaData, structure: { name?:string, clazz?:{ new():T } }
*/
let decoratorMetaData = getJsonProperty(instance, key);
/**
* pass value to instance
*/
if (decoratorMetaData && decoratorMetaData.customConverter) {
instance[key] = decoratorMetaData.customConverter.fromJson(json[decoratorMetaData.name || key]);
} else {
instance[key] = decoratorMetaData ? mapFromJson(decoratorMetaData, instance, json, key) : json[key];
}
});
return instance;
}
/**
* deserialize array
*
* @function
* @param {{new():T}} clazz, class type which is going to initialize and hold a mapping json
* @param {Object[]} json, input json array which to be mapped
*
* @return {T[]} return array of mapped object
*/
export function deserializeArray<T extends IGenericObject>(Clazz: {new(): T}, json: IGenericObject[]): T[] {
let results: T[] = [];
for (let o of json) {
results.push(deserialize(Clazz, o));
}
return results;
}
/**
* Serialize: Creates a ready-for-json-serialization object from the provided model instance.
* Only @JsonProperty decorated properties in the model instance are processed.
*
* @param instance an instance of a model class
* @returns {any} an object ready to be serialized to JSON
*/
export function serialize(instance: any): any {
if (!isTargetType(instance, 'object') || isArrayOrArrayClass(instance)) {
return instance;
}
const obj: any = {};
Object.keys(instance).forEach(key => {
const metadata = getJsonProperty(instance, key);
obj[metadata && metadata.name ? metadata.name : key] = serializeProperty(metadata, instance[key]);
});
return obj;
}
/**
* Serialize array: Creates an array of ready-for-json-serialization object from the provided model instances.
* Only @JsonProperty decorated properties in the model instance are processed.
*
* @param instances an array of instance of a model classes
* @returns {any[]} an array of object ready to be serialized to JSON
*/
export function serializeArray(instances: any[]): any[] {
let results: any[] = [];
for (let instance of instances) {
results.push(serialize(instance));
}
return results;
}
/**
* Prepare a single property to be serialized to JSON.
*
* @param metadata
* @param prop
* @returns {any}
*/
function serializeProperty(metadata: IDecoratorMetaData<any>, prop: any): any {
if (typeof prop === 'undefined') {
return undefined;
}
if (prop === null) {
return null;
}
if (!metadata || metadata.excludeToJson === true) {
return;
}
if (metadata.customConverter) {
return metadata.customConverter.toJson(prop);
}
if (!metadata.clazz) {
if (prop instanceof Date) {
let tzoffset = prop.getTimezoneOffset() * 60000; //offset in milliseconds
let localISOTime = (new Date(prop.getTime() - tzoffset)).toISOString().slice(0,-1);
prop = localISOTime;
}
return prop;
}
if (isArrayOrArrayClass(prop)) {
return prop.map((propItem: any) => serialize(propItem));
}
return serialize(prop);
}