Json Object mapper writing in TypeScript
MIT License
This a simple package to mapping a json object.
npm install typescript-json-object-mapper
yarn add typescript-json-object-mapper
To work with decorators, you need first enable emitDecoratorMetadata
y experimentalDecorators
on you tsconfig.json
.
Example:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
This example tries to show all possible cases in which you might need to use this utility.
class UserView extends JsonView {
@JsonProperty
username: string;
@JsonProperty({
ignore: true
})
password: string;
@JsonProperty({
topic: 'custom'
})
birthday: string;
@JsonProperty({
topic: 'custom2'
})
phone: string;
}
const json = {
username: "annon",
password: "12345678",
birthday: "1992-03-20",
phone: "+0123456789"
};
const serialized = JsonObjectMapper.serialize(json, UserView).toString();
results:
{
username: "annon",
birthday: "1992-03-20",
phone: "+0123456789"
}
const serialized = JsonObjectMapper.serialize(json, UserView, ['custom']).toString();
results:
{
username: "annon",
birthday: "1992-03-20"
}
const serialized = JsonObjectMapper.serialize(json, UserView, ['custom', 'custom2']).toString();
results:
{
username: "annon",
birthday: "1992-03-20",
phone: "+0123456789"
}
Date.parse
Date
Number
Number
Object Array
Object
String
This function always return Serialization
object.
And can using it with data as Array
or Object
.
// from Array
JsonObjectMapper.serialize([
{
email: "[email protected]",
password: "123456"
},
{
email: "[email protected]",
password: "123456"
},
{
email: "[email protected]",
password: "123456"
}
], UserView);
// from Object
JsonObjectMapper.serialize({
email: "[email protected]",
password: "123456"
}, UserView);
spaces:number = 4
): string
This method return a string representation of object.
This method return a json object representation.