|
1 | | -# Json-Object-Mapper |
| 1 | +# (TypeScript) Json-Object-Mapper |
| 2 | +[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HEF7696BDQTDG) |
| 3 | + |
| 4 | +This a simple package to mapping a json object. |
| 5 | + |
| 6 | + |
| 7 | +## Getting Started |
| 8 | +### Install |
| 9 | +```bash |
| 10 | +npm install --save typescript-json-object-mapper |
| 11 | +``` |
| 12 | +### Create you own Views |
| 13 | +This an exaple with sub-views and recursive work. |
| 14 | + |
| 15 | +```typescript |
| 16 | +class WheelsView extends JsonView { |
| 17 | + @JsonProperty({name: 'id',type: 'string'}) index: number; |
| 18 | + @JsonProperty vendor: string; |
| 19 | + @JsonProperty size: number; |
| 20 | + @JsonProperty timestamp: Date; |
| 21 | +} |
| 22 | +class CarView extends JsonView { |
| 23 | + @JsonProperty name: string; |
| 24 | + @JsonProperty vendor: string; |
| 25 | + @JsonProperty model: string; |
| 26 | + @JsonProperty engine: string; |
| 27 | + @JsonProperty traction: string; |
| 28 | + @JsonProperty({view: [WheelsView]}) wheels: WheelsView[]; |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +### Define you data object |
| 33 | +```typescript |
| 34 | +const json: any = { |
| 35 | + name: "cautito", |
| 36 | + vendor: "citroen", |
| 37 | + model: "lira", |
| 38 | + engine: "v8", |
| 39 | + traction: "4x4", |
| 40 | + wheels: [ // Array of object's |
| 41 | + { |
| 42 | + index: 0, |
| 43 | + vendor: "pirelli", |
| 44 | + size: 26, |
| 45 | + timestamp: new Date() // date as object |
| 46 | + }, |
| 47 | + { |
| 48 | + index: 1, |
| 49 | + vendor: "firestone", |
| 50 | + size: 26, |
| 51 | + timestamp: 1535465061 // date as integer |
| 52 | + }, |
| 53 | + { |
| 54 | + index: 2, |
| 55 | + vendor: "pirelli", |
| 56 | + size: 26, |
| 57 | + timestamp: "Tue, 28 Aug 2018 17:03:56 GMT" // date as string |
| 58 | + }, |
| 59 | + { |
| 60 | + index: 3, |
| 61 | + vendor: "pirelli", |
| 62 | + size: 10, |
| 63 | + timestamp: "Tue, 28 Aug 2018 17:03:56 GMT" |
| 64 | + } |
| 65 | + ] |
| 66 | +}; |
| 67 | +``` |
| 68 | +### Serilize |
| 69 | +```typescript |
| 70 | +const serialized = JsonObjectMapper.serialize(json, CarView).toString(); |
| 71 | +``` |
| 72 | + |
| 73 | +### Result |
| 74 | +```json |
| 75 | +{ |
| 76 | + "name": "cautito", |
| 77 | + "vendor": "citroen", |
| 78 | + "model": "lira", |
| 79 | + "engine": "v8", |
| 80 | + "traction": "4x4", |
| 81 | + "wheels": [ |
| 82 | + { |
| 83 | + "vendor": "pirelli", |
| 84 | + "size": 26, |
| 85 | + "timestamp": "2018-08-28T18:11:44.204Z", |
| 86 | + "id": "0" |
| 87 | + }, |
| 88 | + { |
| 89 | + "vendor": "firestone", |
| 90 | + "size": 26, |
| 91 | + "timestamp": "1970-01-18T18:31:05.061Z", |
| 92 | + "id": "1" |
| 93 | + }, |
| 94 | + { |
| 95 | + "vendor": "pirelli", |
| 96 | + "size": 26, |
| 97 | + "timestamp": "2018-08-28T17:03:56.000Z", |
| 98 | + "id": "2" |
| 99 | + }, |
| 100 | + { |
| 101 | + "vendor": "pirelli", |
| 102 | + "size": 10, |
| 103 | + "timestamp": "2018-08-28T17:03:56.000Z", |
| 104 | + "id": "3" |
| 105 | + } |
| 106 | + ] |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +## To Do |
| 111 | +* [x] Recursive support |
| 112 | +* [x] No-Initiation support |
| 113 | +* [x] Renaming support |
| 114 | +* [x] Convert types support |
| 115 | +* [x] Annotation support |
| 116 | +* [ ] Format support |
| 117 | +* [ ] Enum support |
| 118 | +* [ ] Validator support |
0 commit comments