Skip to content

Commit 95084af

Browse files
committed
update to 1.1.5 👍
1 parent 53f6c09 commit 95084af

22 files changed

+182
-273
lines changed

README.md

Lines changed: 149 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,56 @@ This a simple package to mapping a json object.
99
```bash
1010
npm 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+
}
1635
class 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
}
2247
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[];
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+
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": "[email protected]"
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+
196+
password: "123456"
93197
},
94198
{
95-
"vendor": "pirelli",
96-
"size": 26,
97-
"timestamp": "2018-08-28T17:03:56.000Z",
98-
"id": "2"
199+
200+
password: "123456"
99201
},
100202
{
101-
"vendor": "pirelli",
102-
"size": 10,
103-
"timestamp": "2018-08-28T17:03:56.000Z",
104-
"id": "3"
203+
204+
password: "123456"
105205
}
106-
]
107-
}
206+
], UserView);
207+
208+
// from Object
209+
JsonObjectMapper.serialize({
210+
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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-json-object-mapper",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "Json Object mapper writing in TypeScript",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/core/JsonObjectMapper.d.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/core/JsonObjectMapper.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/core/JsonObjectMapper.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/core/JsonObjectMapper.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { JsonView } from './JsonView';
2-
3-
class Serialization {
4-
public constructor (private serialized: any) {}
5-
public toString (space = 4): string {
6-
return JSON.stringify(this.serialized, null, space);
7-
}
8-
public toJson (): object {
9-
return this.serialized;
10-
}
11-
}
2+
import { Serialization } from './Serialization';
123

134
export class JsonObjectMapper {
14-
public static serialize(data: any, view: typeof JsonView): Serialization;
5+
public static serialize (data: object[], view: typeof JsonView): Serialization;
6+
public static serialize (data: object, view: typeof JsonView): Serialization;
157
public static serialize (...args: any[]): Serialization {
168
const filter: Function = (target: typeof JsonView, input: {[key: string]: any}): any => {
179
const jsonProperties: { [key: string]: any} = Reflect.getMetadata("JSON:PROPERTY", target.prototype) || {};
@@ -20,7 +12,6 @@ export class JsonObjectMapper {
2012
delete input[prop];
2113
continue;
2214
}
23-
console.log("property", prop, jsonProperties[prop].type, input[prop]);
2415
switch (jsonProperties[prop].type.toLowerCase()) {
2516
case 'date':
2617
if (typeof input[prop] === 'number') {
@@ -79,9 +70,11 @@ export class JsonObjectMapper {
7970
}
8071
return input;
8172
};
82-
return new Serialization(filter(args[1], args[0]));
83-
}
84-
public static serializeArray(dataArray: any[], view: typeof JsonView): Serialization {
85-
return new Serialization(dataArray.map(data => JsonObjectMapper.serialize(data, view).toJson()));
73+
if (Array.isArray(args[0])) {
74+
const elements: object[] = (args[0] as object[])
75+
.map(data => JsonObjectMapper.serialize(data, args[1]).toJson());
76+
return new Serialization(elements);
77+
}
78+
else return new Serialization(filter(args[1], args[0]));
8679
}
8780
}

src/core/JsonView.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/core/JsonView.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/core/JsonView.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)