17 lines
584 B
TypeScript
17 lines
584 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 null;
|
||
|
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);
|
||
|
}
|
||
|
}
|