54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
export namespace main {
|
|
|
|
export class Address {
|
|
street: string;
|
|
postcode: string;
|
|
|
|
static createFrom(source: any = {}) {
|
|
return new Address(source);
|
|
}
|
|
|
|
constructor(source: any = {}) {
|
|
if ('string' === typeof source) source = JSON.parse(source);
|
|
this.street = source["street"];
|
|
this.postcode = source["postcode"];
|
|
}
|
|
}
|
|
export class Person {
|
|
name: string;
|
|
age: number;
|
|
address?: Address;
|
|
|
|
static createFrom(source: any = {}) {
|
|
return new Person(source);
|
|
}
|
|
|
|
constructor(source: any = {}) {
|
|
if ('string' === typeof source) source = JSON.parse(source);
|
|
this.name = source["name"];
|
|
this.age = source["age"];
|
|
this.address = this.convertValues(source["address"], Address);
|
|
}
|
|
|
|
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
|
if (!a) {
|
|
return a;
|
|
}
|
|
if (a.slice && a.map) {
|
|
return (a as any[]).map(elem => this.convertValues(elem, classs));
|
|
} else if ("object" === typeof a) {
|
|
if (asMap) {
|
|
for (const key of Object.keys(a)) {
|
|
a[key] = new classs(a[key]);
|
|
}
|
|
return a;
|
|
}
|
|
return new classs(a);
|
|
}
|
|
return a;
|
|
}
|
|
}
|
|
|
|
}
|
|
|