argos/dmp-frontend/src/app/utilities/JsonSerializer.ts

23 lines
734 B
TypeScript
Raw Normal View History

import { Serializable } from '../models/interfaces/Serializable';
2018-03-21 14:15:06 +01:00
export class JsonSerializer {
2018-10-05 17:00:54 +02:00
public static fromJSONArray<T extends Serializable<T>>(items: any[], type: { new(): T; }): T[] {
if (!items) { return new Array<T>(); }
const objectList: T[] = new Array<T>();
for (let i = 0; i < items.length; i++) {
objectList.push(new type().fromJSONObject(items[i]));
}
return objectList;
}
2018-10-05 17:00:54 +02:00
public static fromJSONObject<T extends Serializable<T>>(item: any, type: { new(): T; }): T {
if (!item) { return null; }
return new type().fromJSONObject(item);
}
2018-09-06 14:50:38 +02:00
2018-10-05 17:00:54 +02:00
public static copy<T extends Serializable<T>>(item: any, itemToCopy: any): T {
if (!item) { return null; }
return item.fromJSONObject(itemToCopy);
}
2018-09-06 14:50:38 +02:00
}