2017-11-23 11:40:02 +01:00
|
|
|
import { Serializable } from '../models/interfaces/Serializable';
|
2018-03-21 14:15:06 +01:00
|
|
|
export class JsonSerializer {
|
2017-11-23 11:40:02 +01:00
|
|
|
|
2018-01-03 17:36:31 +01:00
|
|
|
public static fromJSONArray<T extends Serializable<T>>(items: any[], type: { new(): T; }): T[] {
|
2018-03-21 14:15:06 +01:00
|
|
|
if (!items) return new Array<T>();
|
2017-11-23 11:40:02 +01:00
|
|
|
const objectList: T[] = new Array<T>();
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
objectList.push(new type().fromJSONObject(items[i]))
|
|
|
|
}
|
|
|
|
return objectList;
|
|
|
|
}
|
|
|
|
|
2018-01-03 17:36:31 +01:00
|
|
|
public static fromJSONObject<T extends Serializable<T>>(item: any, type: { new(): T; }): T {
|
2018-03-21 14:15:06 +01:00
|
|
|
if (!item) return null;
|
2017-11-23 11:40:02 +01:00
|
|
|
return new type().fromJSONObject(item);
|
|
|
|
}
|
|
|
|
}
|