@@ -19,12 +19,12 @@ Source code is available [here](https://github.com/pleerock/class-transformer-de
19
19
- [ Node.js] ( #nodejs )
20
20
- [ Browser] ( #browser )
21
21
- [ Methods] ( #methods )
22
- - [ plainToClass] ( #plaintoclass )
23
- - [ plainToClassFromExist] ( #plaintoclassfromexist )
24
- - [ classToPlain] ( #classtoplain )
25
- - [ classToClass] ( #classtoclass )
26
- - [ serialize] ( #serialize )
27
- - [ deserialize and deserializeArray] ( #deserialize-and-deserializearray )
22
+ - [ plainToClass] ( #plaintoclass )
23
+ - [ plainToClassFromExist] ( #plaintoclassfromexist )
24
+ - [ classToPlain] ( #classtoplain )
25
+ - [ classToClass] ( #classtoclass )
26
+ - [ serialize] ( #serialize )
27
+ - [ deserialize and deserializeArray] ( #deserialize-and-deserializearray )
28
28
- [ Enforcing type-safe instance] ( #enforcing-type-safe-instance )
29
29
- [ Working with nested objects] ( #working-with-nested-objects )
30
30
- [ Providing more than one type option] ( #providing-more-than-one-type-option )
@@ -36,7 +36,7 @@ Source code is available [here](https://github.com/pleerock/class-transformer-de
36
36
- [ Skipping private properties, or some prefixed properties] ( #skipping-private-properties-or-some-prefixed-properties )
37
37
- [ Using groups to control excluded properties] ( #using-groups-to-control-excluded-properties )
38
38
- [ Using versioning to control exposed and excluded properties] ( #using-versioning-to-control-exposed-and-excluded-properties )
39
- - [ Сonverting date strings into Date objects] ( #%d0%a1onverting -date-strings-into-date-objects )
39
+ - [ Сonverting date strings into Date objects] ( #сonverting -date-strings-into-date-objects )
40
40
- [ Working with arrays] ( #working-with-arrays )
41
41
- [ Additional data transformation] ( #additional-data-transformation )
42
42
- [ Basic usage] ( #basic-usage )
@@ -211,7 +211,7 @@ Now you can use `users[0].getName()` and `users[0].isAdult()` methods.
211
211
212
212
## Methods
213
213
214
- #### plainToClass
214
+ ### plainToClass
215
215
216
216
This method transforms a plain javascript object to instance of specific class .
217
217
@@ -221,7 +221,7 @@ import {plainToClass} from "class-transformer";
221
221
let users = plainToClass(User, userJson); // to convert user plain object a single user. also supports arrays
222
222
` ` `
223
223
224
- #### plainToClassFromExist
224
+ ### plainToClassFromExist
225
225
226
226
This method transforms a plain object into a instance using a already filled Object which is a instance from the target class .
227
227
@@ -232,7 +232,7 @@ defaultUser.role = 'user';
232
232
let mixedUser = plainToClassFromExist(defaultUser, user); // mixed user should have the value role = user when no value is set otherwise.
233
233
` ` `
234
234
235
- #### classToPlain
235
+ ### classToPlain
236
236
237
237
This method transforms your class object back to plain javascript object , that can be `JSON .stringify ` later .
238
238
@@ -241,7 +241,7 @@ import {classToPlain} from "class-transformer";
241
241
let photo = classToPlain (photo );
242
242
```
243
243
244
- #### classToClass
244
+ ### classToClass
245
245
246
246
This method transforms your class object into new instance of the class object.
247
247
This maybe treated as deep clone of your objects.
@@ -253,7 +253,7 @@ let photo = classToClass(photo);
253
253
254
254
You can also use a ` ignoreDecorators ` option in transformation options to ignore all decorators you classes is using.
255
255
256
- #### serialize
256
+ ### serialize
257
257
258
258
You can serialize your model right to the json using ` serialize ` method:
259
259
@@ -264,7 +264,7 @@ let photo = serialize(photo);
264
264
265
265
` serialize ` works with both arrays and non-arrays.
266
266
267
- #### deserialize and deserializeArray
267
+ ### deserialize and deserializeArray
268
268
269
269
You can deserialize your model to from a json using ` deserialize ` method:
270
270
@@ -580,7 +580,7 @@ You can pass any number of prefixes and all properties that begin with these pre
580
580
For example:
581
581
582
582
``` typescript
583
- import {Expose } from " class-transformer" ;
583
+ import {Expose , classToPlain } from " class-transformer" ;
584
584
585
585
export class User {
586
586
@@ -596,15 +596,15 @@ export class User {
596
596
597
597
@Expose ()
598
598
get name() {
599
- return this .firstName + " " + this .lastName ;
599
+ return this ._firstName + " " + this ._lastName ;
600
600
}
601
601
602
602
}
603
603
604
604
const user = new User ();
605
605
user .id = 1 ;
606
606
user .setName (" Johny" , " Cage" );
607
- user ._password = 123 ;
607
+ user ._password = " 123" ;
608
608
609
609
const plainUser = classToPlain (user , { excludePrefixes: [" _" ] });
610
610
// here plainUser will be equal to
@@ -616,9 +616,8 @@ const plainUser = classToPlain(user, { excludePrefixes: ["_"] });
616
616
You can use groups to control what data will be exposed and what will not be:
617
617
618
618
``` typescript
619
- import {Exclude , Expose } from " class-transformer" ;
619
+ import {Exclude , Expose , classToPlain } from " class-transformer" ;
620
620
621
- @Exclude ()
622
621
export class User {
623
622
624
623
id: number ;
@@ -631,10 +630,7 @@ export class User {
631
630
@Expose ({ groups: [" user" ] }) // this means that this data will be exposed only to users
632
631
password: string ;
633
632
}
634
- ```
635
633
636
- ``` typescript
637
- import {classToPlain } from " class-transformer" ;
638
634
let user1 = classToPlain (user , { groups: [" user" ] }); // will contain id, name, email and password
639
635
let user2 = classToPlain (user , { groups: [" admin" ] }); // will contain id, name and email
640
636
```
@@ -645,9 +641,8 @@ If you are building an API that has different versions, class-transformer has ex
645
641
You can control which properties of your model should be exposed or excluded in what version. Example:
646
642
647
643
``` typescript
648
- import {Exclude , Expose } from " class-transformer" ;
644
+ import {Exclude , Expose , classToPlain } from " class-transformer" ;
649
645
650
- @Exclude ()
651
646
export class User {
652
647
653
648
id: number ;
@@ -660,15 +655,12 @@ export class User {
660
655
@Expose ({ since: 2.1 }) // this means that this property will be exposed for version starting from 2.1
661
656
password: string ;
662
657
}
663
- ```
664
658
665
- ``` typescript
666
- import {classToPlain } from " class-transformer" ;
667
659
let user1 = classToPlain (user , { version: 0.5 }); // will contain id and name
668
660
let user2 = classToPlain (user , { version: 0.7 }); // will contain id, name and email
669
661
let user3 = classToPlain (user , { version: 1 }); // will contain id and name
670
662
let user4 = classToPlain (user , { version: 2 }); // will contain id and name
671
- let user5 = classToPlain (user , { version: 2.1 }); // will contain id, name nad password
663
+ let user5 = classToPlain (user , { version: 2.1 }); // will contain id, name and password
672
664
```
673
665
674
666
## Сonverting date strings into Date objects
@@ -921,4 +913,4 @@ usages.
921
913
922
914
## Release notes
923
915
924
- See information about breaking changes and release notes [ here] ( https://github.com/pleerock /class-transformer/tree /master/doc/release-notes .md ) .
916
+ See information about breaking changes and release notes [ here] ( https://github.com/typestack /class-transformer/blob /master/CHANGELOG .md ) .
0 commit comments