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

17 lines
594 B
TypeScript

import { Serializable } from '../models/interfaces/Serializable';
export class JsonSerializer<T extends Serializable<T>>{
public fromJSONArray(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;
}
public fromJSONObject(item: any, type: { new(): T; }): T {
if(!item)return null;
return new type().fromJSONObject(item);
}
}