@@ -9,23 +9,56 @@ This a simple package to mapping a json object.
99``` bash
1010npm install --save typescript-json-object-mapper
1111```
12+ ### Import
13+ ```
14+ import { JsonProperty, JsonIgnore, JsonView, JsonObjectMapper } from 'typescript-json-object-mapper';
15+ ```
16+
1217### Create you own Views
13- This an exaple with sub-views and recursive work .
18+ This example tries to show all possible cases in which you might need to use this utility .
1419
1520``` typescript
21+ import { JsonProperty , JsonIgnore , JsonView } from ' typescript-json-object-mapper' ;
22+
23+ class DriverView extends JsonView {
24+ @JsonProperty
25+ public id: number ;
26+ @JsonProperty ({
27+ name: " fullname"
28+ })
29+ public name: number ; // Rename property
30+ @JsonProperty
31+ public email: string ;
32+ @JsonIgnore
33+ public password: string ;
34+ }
1635class WheelsView extends JsonView {
17- @JsonProperty ({name: ' id' ,type: ' string' }) index: number ;
18- @JsonProperty vendor: string ;
19- @JsonProperty size: number ;
20- @JsonProperty timestamp: Date ;
36+ @JsonProperty ({
37+ type: " string"
38+ })
39+ public index: number ;// Convert int to string value
40+ @JsonProperty
41+ public vendor: string ;
42+ @JsonProperty
43+ public size: number ;
44+ @JsonProperty
45+ public timestamp: Date ;
2146}
2247class 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 [];
48+ @JsonProperty
49+ public name: string ;
50+ @JsonProperty
51+ public vendor: string ;
52+ @JsonProperty
53+ public model: string ;
54+ @JsonIgnore
55+ public engine: string ; // Ignore property(don't show)
56+ @JsonProperty
57+ public traction: string ;
58+ @JsonProperty ([WheelsView ])
59+ public wheels: WheelsView [];// Sub-View Array
60+ @JsonProperty (DriverView )
61+ public driver: DriverView ;// Sub-View Object
2962}
3063```
3164
@@ -62,7 +95,13 @@ const json: any = {
6295 size: 10 ,
6396 timestamp: " Tue, 28 Aug 2018 17:03:56 GMT"
6497 }
65- ]
98+ ],
99+ driver: {
100+ id: 1 ,
101+ name: " John Smith" ,
102+ email: " john.smith@example.com" ,
103+ password: " 12345678"
104+ }
66105};
67106```
68107### Serilize
@@ -73,46 +112,109 @@ const serialized = JsonObjectMapper.serialize(json, CarView).toString();
73112### Result
74113``` json
75114{
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- },
115+ "name" : " cautito" ,
116+ "vendor" : " citroen" ,
117+ "model" : " lira" ,
118+ "engine" : " v8" ,
119+ "traction" : " 4x4" ,
120+ "wheels" : [
121+ {
122+ "vendor" : " pirelli" ,
123+ "size" : 26 ,
124+ "timestamp" : " 2018-08-28T18:11:44.204Z" ,
125+ "index" : " 0"
126+ },
127+ {
128+ "vendor" : " firestone" ,
129+ "size" : 26 ,
130+ "timestamp" : " 1970-01-18T18:31:05.061Z" ,
131+ "index" : " 1"
132+ },
133+ {
134+ "vendor" : " pirelli" ,
135+ "size" : 26 ,
136+ "timestamp" : " 2018-08-28T17:03:56.000Z" ,
137+ "index" : " 2"
138+ },
139+ {
140+ "vendor" : " pirelli" ,
141+ "size" : 10 ,
142+ "timestamp" : " 2018-08-28T17:03:56.000Z" ,
143+ "index" : " 3"
144+ }
145+ ],
146+ "driver" : {
147+ "id" : " 1" ,
148+ "fullname" : " John Smith" ,
149+ "email" : " john.smith@example.com"
150+ }
151+ }
152+ ```
153+
154+ ## Features
155+ * [x] No-Initiation(Using only reference to class)
156+ * [x] Renaming properties
157+ * [x] Convert data types
158+ * [x] to Date
159+ * [ x] from String using ` Date.parse `
160+ * [ x] from Integer using ` Date `
161+ * [x] to Integer
162+ * [x] from String using ` Number `
163+ * [x] to Float
164+ * [x] from String using ` Number `
165+ * [x] to Boolean
166+ * [x] Sub-Views(Recursivity)
167+ * [x] Array sub-views
168+ * [x] Single sub-view
169+ * [x] Date values
170+ * [x] Serialize from ` Object Array `
171+ * [x] Serialize from ` Object `
172+
173+ ## ToDo
174+ * [ ] Deserialize
175+ * [ ] Custom Regex validator
176+ * [ ] Custom format value
177+ * [ ] Default values
178+ * [ ] Enum values
179+ * [ ] String
180+ * [ ] Number
181+ * [ ] Boolean
182+
183+
184+ ## API:
185+
186+ ### JsonObjectMapper.serialize
187+ This function always return ` Serialization ` object.
188+ And can using it with data as ` Array ` or ` Object ` .
189+
190+ ##### Example
191+ ``` typescript
192+ // from Array
193+ JsonObjectMapper .serialize ([
88194 {
89- "vendor" : " firestone" ,
90- "size" : 26 ,
91- "timestamp" : " 1970-01-18T18:31:05.061Z" ,
92- "id" : " 1"
195+ email: " john.smith@example.com" ,
196+ password: " 123456"
93197 },
94198 {
95- "vendor" : " pirelli" ,
96- "size" : 26 ,
97- "timestamp" : " 2018-08-28T17:03:56.000Z" ,
98- "id" : " 2"
199+ email: " john.smith@example.com" ,
200+ password: " 123456"
99201 },
100202 {
101- "vendor" : " pirelli" ,
102- "size" : 10 ,
103- "timestamp" : " 2018-08-28T17:03:56.000Z" ,
104- "id" : " 3"
203+ email: " john.smith@example.com" ,
204+ password: " 123456"
105205 }
106- ]
107- }
206+ ], UserView );
207+
208+ // from Object
209+ JsonObjectMapper .serialize ({
210+ email: " john.smith@example.com" ,
211+ password: " 123456"
212+ }, UserView );
108213```
109214
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
215+ ### Serialization
216+ #### Serialization.toString(` spaces:number = 4 ` ): ` string `
217+ This method return a string representation of object.
218+
219+ #### Serialization.toJson()
220+ This method return a json object representation.
0 commit comments