typescript-json-object-mapper
v1.4.5
Published
Json Object mapper writing in TypeScript
Downloads
393
Maintainers
Readme
(TypeScript) Json-Object-Mapper
This a simple package to mapping a json object.
Getting Started
Install
npm install typescript-json-object-mapper
yarn add typescript-json-object-mapper
Configure
To work with decorators, you need first enable emitDecoratorMetadata
y experimentalDecorators
on you tsconfig.json
.
Example:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
Create you own Views
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;
}
Define you data object
const json = {
username: "annon",
password: "12345678",
birthday: "1992-03-20",
phone: "+0123456789"
};
Serilize(without topic's)
const serialized = JsonObjectMapper.serialize(json, UserView).toString();
results:
{
username: "annon",
birthday: "1992-03-20",
phone: "+0123456789"
}
Serilize(with topic)
const serialized = JsonObjectMapper.serialize(json, UserView, ['custom']).toString();
results:
{
username: "annon",
birthday: "1992-03-20"
}
Serilize(with topic)
const serialized = JsonObjectMapper.serialize(json, UserView, ['custom', 'custom2']).toString();
results:
{
username: "annon",
birthday: "1992-03-20",
phone: "+0123456789"
}
Features
- [x] No-Initiation(Using only reference to class)
- [x] Renaming properties
- [x] Change data types
- [x] to Date
- [x] from String using
Date.parse
- [x] from Integer using
Date
- [x] from String using
- [x] to Integer
- [x] from String using
Number
- [x] from String using
- [x] to Float
- [x] from String using
Number
- [x] from String using
- [x] to Boolean
- [x] to String
- [x] to Object
- [x] to Date
- [x] Sub-Views(Recursivity)
- [x] Array sub-views
- [x] Single sub-view
- [x] Date values(String, Number, Date)
- [x] Serialize from
Object Array
- [x] Serialize from
Object
- [x] Serialize from
String
- [x] Property Topic's
- [x] Smart
Object
,Constructor
,Function
,Date
,ObjectId
toString
- [x] Using
toString
method when it exists- [x] If
toString
return[object object]
, JsonObjectMapper will try iterate object to create a plain object with defualts values.
- [x] If
- [x] Using
API:
JsonObjectMapper.serialize
This function always return Serialization
object.
And can using it with data as Array
or Object
.
Example
// 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);
Serialization
Serialization.toString(spaces:number = 4
): string
This method return a string representation of object.
Serialization.toJson()
This method return a json object representation.