Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- deserialize.ts
- --------------------
- import 'reflect-metadata';
- export const Metadata: any = {};
- export function Expose(target: any, key: string) {
- const type = Reflect.getMetadata('design:type', target, key);
- if (!Metadata[target.constructor.name]) {
- Metadata[target.constructor.name] = {};
- }
- Metadata[target.constructor.name][key] = type;
- }
- export class Deserializable {
- deserialize(input: object): this {
- Object.keys(Metadata[this.constructor.name]).forEach(key => {
- this[key] = input[key] || Metadata[this.constructor.name][key]();
- });
- return this;
- }
- }
- Page.model.ts
- --------------------
- import { Expose, Deserializable } from './deserialize';
- export class Page extends Deserializable {
- @Expose content: object;
- @Expose date: string;
- @Expose id: number;
- @Expose slug: string;
- @Expose title: object;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement