typescript-json-object-mapper
Version:
Json Object mapper writing in TypeScript
159 lines (143 loc) • 3.63 kB
Markdown
# (TypeScript) Json-Object-Mapper
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HEF7696BDQTDG)
This a simple package to mapping a json object.
## Getting Started
### Install
```bash
npm install typescript-json-object-mapper
```
```bash
yarn add typescript-json-object-mapper
```
### Configure
To work with decorators, you need first enable `emitDecoratorMetadata` y `experimentalDecorators` on you `tsconfig.json`.
Example:
```json
{
"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.
```typescript
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
```typescript
const json = {
username: "annon",
password: "12345678",
birthday: "1992-03-20",
phone: "+0123456789"
};
```
### Serilize(without topic's)
```typescript
const serialized = JsonObjectMapper.serialize(json, UserView).toString();
```
results:
```json
{
username: "annon",
birthday: "1992-03-20",
phone: "+0123456789"
}
```
### Serilize(with topic)
```typescript
const serialized = JsonObjectMapper.serialize(json, UserView, ['custom']).toString();
```
results:
```json
{
username: "annon",
birthday: "1992-03-20"
}
```
### Serilize(with topic)
```typescript
const serialized = JsonObjectMapper.serialize(json, UserView, ['custom', 'custom2']).toString();
```
results:
```json
{
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] to Integer
* [x] from String using `Number`
* [x] to Float
* [x] from String using `Number`
* [x] to Boolean
* [x] to String
* [x] to Object
* [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` to `String`
* [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.
## API:
### JsonObjectMapper.serialize
This function always return `Serialization` object.
And can using it with data as `Array` or `Object`.
##### Example
```typescript
// from Array
JsonObjectMapper.serialize([
{
email: "john.smith@example.com",
password: "123456"
},
{
email: "john.smith@example.com",
password: "123456"
},
{
email: "john.smith@example.com",
password: "123456"
}
], UserView);
// from Object
JsonObjectMapper.serialize({
email: "john.smith@example.com",
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.