Metadata library that created to resolve your pain with all mappers and type-checking of objects after serialization or deserialization.
Don`t use the spread operator for copying of your models because prototypes chain will be lost after it!
- Add a dependency to your package.json file:
npm install serialize-ts --save
or yarn add serialize-ts
;
- Change a tsconfig.json:
{
...
compilerOptions: {
...
emitDecoratorMetadata: true,
...
},
...
}
Simple model:
class TestModel {
@Field()
@Name("server-id")
id: number;
@Field()
fullName: string;
ignoredField: any;
}
const model = new TestModel();
model.id = 12;
model.fullName = "Default full name";
model.ignoredField = { customName: "test" };
console.log(serialize(model));
/*Output -> {
server-id: 12,
fullName: 'Default full name'
}*/
const obj = {
"server-id": 12,
fullName: "Default full name",
ignoredField: "some ignored value"
};
console.log(deserialize(obj, TestModel));
/*Output -> TestModel {
id: 12,
fullName: 'Default full name'
}*/
Nested models:
@Model()
class NestedModel {
@Field()
firstField: number;
@Field()
secondField: string;
}
class OuterModel {
@Field()
id: number;
@Field()
nestedModel: NestedModel;
}
const obj = {
id: 12,
nestedModel: {
firstField: 24,
secondField: 'Some awesome string!'
}
};
const outerModel = deserialize(obj, OuterModel);
console.log(outerModel);
/*
Output -> OuterModel {
id: 12,
nestedModel: NestedModel {
firstField: 24,
secondField: "Some awesome string!"
}
}
*/
console.log(serialize(outerModel));
/*
Output -> {
id: 12,
nestedModel: {
firstField: 24,
secondField: "Some awesome string!"
}
}
*/
Arrays:
class Test {
@Field()
id: number;
@Field()
@Name('numbers')
@Type(new ArraySerializer(new PrimitiveSerializer()))
arrayOfNumbers: number[];
}
const obj = {
id: 12,
numbers: [12, 24, 36, 48]
};
const deserializedModel = deserialize(obj, Test);
console.log(deserializedModel);
/*
Output -> Test {
id: 12,
arrayOfNumber: [12, 24, 36, 48]
}
*/
Custom serializers:
class CustomSerializer implements Serializer<Object> {
serialize(model: Object) {
// Do some custom logic
}
deserialize(json: Object) {
// Do your custom logic and return deserialized object
}
}
class Model {
@Field()
id: 12;
@Field()
@Type(new CustomSerializer())
customField: any;
}
If you have some troubles with serialization without serializer decorator definition, you are able to define it with @Serializer(YourCustomModel)
or with some default type like Date or String.
I am waiting for the issues of course!