Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/olaferlandsen/typescript-json-object-mapper

Json Object mapper writing in TypeScript
https://github.com/olaferlandsen/typescript-json-object-mapper

annotations javascript javascript-object jom js json json-objects mapper object-mapper ts typescript

Last synced: about 1 month ago
JSON representation

Json Object mapper writing in TypeScript

Awesome Lists containing this project

README

        

# (TypeScript) Json-Object-Mapper
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](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

## 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: "[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.