Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Last active October 10, 2019 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/e1ea8cf4fa8052ebd4f95508f39f4800 to your computer and use it in GitHub Desktop.
Save antsmartian/e1ea8cf4fa8052ebd4f95508f39f4800 to your computer and use it in GitHub Desktop.
// import {plainToClass} from "class-transformer";
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-proposal-decorators"]
});
/*
{
"id": 1,
"firstName": "Johny",
"lastName": "Cage",
"age": 27,
"nested" : {
"key" : "value"
}
}
*/
const plainToClass = require("class-transformer").plainToClass;
const Type = require("class-transformer").Type
const userJson = require("./test.json");
var reflectMetadata = require('reflect-metadata');
class Nested {
constructor(data) {
this.key = null;
}
}
class User {
constructor() {
this.id = null;
this.firstName = null;
this.lastName = null;
// decoratorsBeforeExport: true
@Type(() => Nested)
this.nested = null
}
getId() {
return this.id
}
}
let users = plainToClass(User, userJson);
// console.log("users are ", users.getNested())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment