ライブラリを導入する

オープンソースのライブラリhttps://github.com/typestack/class-transformerを使用してオブジェクトとクラスの相互変換を簡単に行う。

Node.jsで使用する場合はnpm経由でインストールする。

npm install class-transformer --save

プレインオブジェクトをクラスに変換する

import { plainToInstance } from 'class-transformer';
class Customer {
    constructor(private name: string, private age: number) {
        this.name = name;
        this.age = age;
    }
}
const customer = plainToInstance(Customer, {
    name: 'alice',
    age: 13,
});
console.log(customer); // Customer {name: "alice", age: 13}
console.log(customer instanceof Customer); // true

配列を渡すと変換されたインスタンスの配列が返される。

クラスをプレインオブジェクトに変換する

import { instanceToPlain } from 'class-transformer';
class Customer {
    constructor(private name: string, private age: number) {
        this.name = name;
        this.age = age;
    }
}
const customer = instanceToPlain(new Customer('alice', 13));
console.log(customer); // {name: "alice", age: 13}
console.log(customer instanceof Customer); // false

ネストされたオブジェクトも変換できる

クラス定義に@Type属性を付けることでネストされたオブジェクトも変換できる。

import { Type, plainToInstance } from 'class-transformer';
class Album {
    id: number;
    name: string;
    @Type(() => Photo)
    photos: Photo[];
}
class Photo {
    id: number;
    filename: string;
}
const album = plainToInstance(Album, {
    id: 1,
    name: 'Photograph',
    photos: [{id: 1, filename: 'IMG_0001.PNG'}, {id: 2, filename: 'IMG_0002.PNG'}]
});