@@ -19,9 +19,9 @@ 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 )
22
+ - [ plainToInstance ] ( #plaintoinstance )
23
23
- [ plainToClassFromExist] ( #plaintoclassfromexist )
24
- - [ classToPlain ] ( #classtoplain )
24
+ - [ instanceToPlain ] ( #instancetoplain )
25
25
- [ instanceToInstance] ( #instanceToInstance )
26
26
- [ serialize] ( #serialize )
27
27
- [ deserialize and deserializeArray] ( #deserialize-and-deserializearray )
@@ -140,7 +140,7 @@ Here is an example how it will look like:
140
140
141
141
``` typescript
142
142
fetch (' users.json' ).then ((users : Object []) => {
143
- const realUsers = plainToClass (User , users );
143
+ const realUsers = plainToInstance (User , users );
144
144
// now each user in realUsers is an instance of User class
145
145
});
146
146
```
@@ -214,14 +214,14 @@ Now you can use `users[0].getName()` and `users[0].isAdult()` methods.
214
214
215
215
## Methods[ ⬆] ( #table-of-contents )
216
216
217
- ### plainToClass [ ⬆] ( #table-of-contents )
217
+ ### plainToInstance [ ⬆] ( #table-of-contents )
218
218
219
219
This method transforms a plain javascript object to instance of specific class.
220
220
221
221
``` typescript
222
- import { plainToClass } from ' class-transformer' ;
222
+ import { plainToInstance } from ' class-transformer' ;
223
223
224
- let users = plainToClass (User , userJson ); // to convert user plain object a single user. also supports arrays
224
+ let users = plainToInstance (User , userJson ); // to convert user plain object a single user. also supports arrays
225
225
```
226
226
227
227
### plainToClassFromExist[ ⬆] ( #table-of-contents )
@@ -235,13 +235,13 @@ defaultUser.role = 'user';
235
235
let mixedUser = plainToClassFromExist (defaultUser , user ); // mixed user should have the value role = user when no value is set otherwise.
236
236
```
237
237
238
- ### classToPlain [ ⬆] ( #table-of-contents )
238
+ ### instanceToPlain [ ⬆] ( #table-of-contents )
239
239
240
240
This method transforms your class object back to plain javascript object, that can be ` JSON.stringify ` later.
241
241
242
242
``` typescript
243
- import { classToPlain } from ' class-transformer' ;
244
- let photo = classToPlain (photo );
243
+ import { instanceToPlain } from ' class-transformer' ;
244
+ let photo = instanceToPlain (photo );
245
245
```
246
246
247
247
### instanceToInstance[ ⬆] ( #table-of-contents )
@@ -285,11 +285,11 @@ let photos = deserializeArray(Photo, photos);
285
285
286
286
## Enforcing type-safe instance[ ⬆] ( #table-of-contents )
287
287
288
- The default behaviour of the ` plainToClass ` method is to set _ all_ properties from the plain object,
288
+ The default behaviour of the ` plainToInstance ` method is to set _ all_ properties from the plain object,
289
289
even those which are not specified in the class.
290
290
291
291
``` typescript
292
- import { plainToClass } from ' class-transformer' ;
292
+ import { plainToInstance } from ' class-transformer' ;
293
293
294
294
class User {
295
295
id: number ;
@@ -303,7 +303,7 @@ const fromPlainUser = {
303
303
lastName: ' Khudoiberdiev' ,
304
304
};
305
305
306
- console .log (plainToClass (User , fromPlainUser ));
306
+ console .log (plainToInstance (User , fromPlainUser ));
307
307
308
308
// User {
309
309
// unkownProp: 'hello there',
@@ -313,10 +313,10 @@ console.log(plainToClass(User, fromPlainUser));
313
313
```
314
314
315
315
If this behaviour does not suit your needs, you can use the ` excludeExtraneousValues ` option
316
- in the ` plainToClass ` method while _ exposing all your class properties_ as a requirement.
316
+ in the ` plainToInstance ` method while _ exposing all your class properties_ as a requirement.
317
317
318
318
``` typescript
319
- import { Expose , plainToClass } from ' class-transformer' ;
319
+ import { Expose , plainToInstance } from ' class-transformer' ;
320
320
321
321
class User {
322
322
@Expose () id: number ;
@@ -330,7 +330,7 @@ const fromPlainUser = {
330
330
lastName: ' Khudoiberdiev' ,
331
331
};
332
332
333
- console .log (plainToClass (User , fromPlainUser , { excludeExtraneousValues: true }));
333
+ console .log (plainToInstance (User , fromPlainUser , { excludeExtraneousValues: true }));
334
334
335
335
// User {
336
336
// id: undefined,
@@ -351,7 +351,7 @@ Lets say we have an album with photos.
351
351
And we are trying to convert album plain object to class object:
352
352
353
353
``` typescript
354
- import { Type , plainToClass } from ' class-transformer' ;
354
+ import { Type , plainToInstance } from ' class-transformer' ;
355
355
356
356
export class Album {
357
357
id: number ;
@@ -367,7 +367,7 @@ export class Photo {
367
367
filename: string ;
368
368
}
369
369
370
- let album = plainToClass (Album , albumJson );
370
+ let album = plainToInstance (Album , albumJson );
371
371
// now album is Album object with Photo objects inside
372
372
```
373
373
@@ -399,7 +399,7 @@ the additional property `__type`. This property is removed during transformation
399
399
```
400
400
401
401
``` typescript
402
- import { Type , plainToClass } from ' class-transformer' ;
402
+ import { Type , plainToInstance } from ' class-transformer' ;
403
403
404
404
export abstract class Photo {
405
405
id: number ;
@@ -435,7 +435,7 @@ export class Album {
435
435
topPhoto: Landscape | Portrait | UnderWater ;
436
436
}
437
437
438
- let album = plainToClass (Album , albumJson );
438
+ let album = plainToInstance (Album , albumJson );
439
439
// now album is Album object with a UnderWater object without `__type` property.
440
440
```
441
441
@@ -530,7 +530,7 @@ export class User {
530
530
}
531
531
```
532
532
533
- Now ` password ` property will be excluded only during ` classToPlain ` operation. Vice versa, use the ` toClassOnly ` option.
533
+ Now ` password ` property will be excluded only during ` instanceToPlain ` operation. Vice versa, use the ` toClassOnly ` option.
534
534
535
535
## Skipping all properties of the class[ ⬆] ( #table-of-contents )
536
536
@@ -555,8 +555,8 @@ Now `id` and `email` will be exposed, and password will be excluded during trans
555
555
Alternatively, you can set exclusion strategy during transformation:
556
556
557
557
``` typescript
558
- import { classToPlain } from ' class-transformer' ;
559
- let photo = classToPlain (photo , { strategy: ' excludeAll' });
558
+ import { instanceToPlain } from ' class-transformer' ;
559
+ let photo = instanceToPlain (photo , { strategy: ' excludeAll' });
560
560
```
561
561
562
562
In this case you don't need to ` @Exclude() ` a whole class.
@@ -567,16 +567,16 @@ If you name your private properties with a prefix, lets say with `_`,
567
567
then you can exclude such properties from transformation too:
568
568
569
569
``` typescript
570
- import { classToPlain } from ' class-transformer' ;
571
- let photo = classToPlain (photo , { excludePrefixes: [' _' ] });
570
+ import { instanceToPlain } from ' class-transformer' ;
571
+ let photo = instanceToPlain (photo , { excludePrefixes: [' _' ] });
572
572
```
573
573
574
574
This will skip all properties that start with ` _ ` prefix.
575
575
You can pass any number of prefixes and all properties that begin with these prefixes will be ignored.
576
576
For example:
577
577
578
578
``` typescript
579
- import { Expose , classToPlain } from ' class-transformer' ;
579
+ import { Expose , instanceToPlain } from ' class-transformer' ;
580
580
581
581
export class User {
582
582
id: number ;
@@ -600,7 +600,7 @@ user.id = 1;
600
600
user .setName (' Johny' , ' Cage' );
601
601
user ._password = ' 123' ;
602
602
603
- const plainUser = classToPlain (user , { excludePrefixes: [' _' ] });
603
+ const plainUser = instanceToPlain (user , { excludePrefixes: [' _' ] });
604
604
// here plainUser will be equal to
605
605
// { id: 1, name: "Johny Cage" }
606
606
```
@@ -610,7 +610,7 @@ const plainUser = classToPlain(user, { excludePrefixes: ['_'] });
610
610
You can use groups to control what data will be exposed and what will not be:
611
611
612
612
``` typescript
613
- import { Exclude , Expose , classToPlain } from ' class-transformer' ;
613
+ import { Exclude , Expose , instanceToPlain } from ' class-transformer' ;
614
614
615
615
export class User {
616
616
id: number ;
@@ -624,8 +624,8 @@ export class User {
624
624
password: string ;
625
625
}
626
626
627
- let user1 = classToPlain (user , { groups: [' user' ] }); // will contain id, name, email and password
628
- let user2 = classToPlain (user , { groups: [' admin' ] }); // will contain id, name and email
627
+ let user1 = instanceToPlain (user , { groups: [' user' ] }); // will contain id, name, email and password
628
+ let user2 = instanceToPlain (user , { groups: [' admin' ] }); // will contain id, name and email
629
629
```
630
630
631
631
## Using versioning to control exposed and excluded properties[ ⬆] ( #table-of-contents )
@@ -634,7 +634,7 @@ If you are building an API that has different versions, class-transformer has ex
634
634
You can control which properties of your model should be exposed or excluded in what version. Example:
635
635
636
636
``` typescript
637
- import { Exclude , Expose , classToPlain } from ' class-transformer' ;
637
+ import { Exclude , Expose , instanceToPlain } from ' class-transformer' ;
638
638
639
639
export class User {
640
640
id: number ;
@@ -648,11 +648,11 @@ export class User {
648
648
password: string ;
649
649
}
650
650
651
- let user1 = classToPlain (user , { version: 0.5 }); // will contain id and name
652
- let user2 = classToPlain (user , { version: 0.7 }); // will contain id, name and email
653
- let user3 = classToPlain (user , { version: 1 }); // will contain id and name
654
- let user4 = classToPlain (user , { version: 2 }); // will contain id and name
655
- let user5 = classToPlain (user , { version: 2.1 }); // will contain id, name and password
651
+ let user1 = instanceToPlain (user , { version: 0.5 }); // will contain id and name
652
+ let user2 = instanceToPlain (user , { version: 0.7 }); // will contain id, name and email
653
+ let user3 = instanceToPlain (user , { version: 1 }); // will contain id and name
654
+ let user4 = instanceToPlain (user , { version: 2 }); // will contain id and name
655
+ let user5 = instanceToPlain (user , { version: 2.1 }); // will contain id, name and password
656
656
```
657
657
658
658
## Сonverting date strings into Date objects[ ⬆] ( #table-of-contents )
@@ -763,7 +763,7 @@ export class Photo {
763
763
}
764
764
```
765
765
766
- Now when you call ` plainToClass ` and send a plain representation of the Photo object,
766
+ Now when you call ` plainToInstance ` and send a plain representation of the Photo object,
767
767
it will convert a date value in your photo object to moment date.
768
768
` @Transform ` decorator also supports groups and versioning.
769
769
@@ -787,9 +787,9 @@ The `@Transform` decorator is given more arguments to let you configure how you
787
787
788
788
| Signature | Example | Description |
789
789
| ------------------------ | ---------------------------------------------------- | ------------------------------------------------------------------------------------- |
790
- | ` @TransformClassToPlain ` | ` @TransformClassToPlain({ groups: ["user"] }) ` | Transform the method return with classToPlain and expose the properties on the class. |
791
- | ` @TransformClassToClass ` | ` @TransformClassToClass({ groups: ["user"] }) ` | Transform the method return with classToClass and expose the properties on the class. |
792
- | ` @TransformPlainToClass ` | ` @TransformPlainToClass(User, { groups: ["user"] }) ` | Transform the method return with plainToClass and expose the properties on the class. |
790
+ | ` @TransformClassToPlain ` | ` @TransformClassToPlain({ groups: ["user"] }) ` | Transform the method return with instanceToPlain and expose the properties on the class. |
791
+ | ` @TransformClassToClass ` | ` @TransformClassToClass({ groups: ["user"] }) ` | Transform the method return with instanceToInstance and expose the properties on the class. |
792
+ | ` @TransformPlainToClass ` | ` @TransformPlainToClass(User, { groups: ["user"] }) ` | Transform the method return with plainToInstance and expose the properties on the class. |
793
793
794
794
The above decorators accept one optional argument:
795
795
ClassTransformOptions - The transform options like groups, version, name
@@ -853,8 +853,8 @@ class MyPayload {
853
853
prop: string ;
854
854
}
855
855
856
- const result1 = plainToClass (MyPayload , { prop: 1234 }, { enableImplicitConversion: true });
857
- const result2 = plainToClass (MyPayload , { prop: 1234 }, { enableImplicitConversion: false });
856
+ const result1 = plainToInstance (MyPayload , { prop: 1234 }, { enableImplicitConversion: true });
857
+ const result2 = plainToInstance (MyPayload , { prop: 1234 }, { enableImplicitConversion: false });
858
858
859
859
/**
860
860
* result1 will be `{ prop: "1234" }` - notice how the prop value has been converted to string.
@@ -867,19 +867,19 @@ const result2 = plainToClass(MyPayload, { prop: 1234 }, { enableImplicitConversi
867
867
Circular references are ignored.
868
868
For example, if you are transforming class ` User ` that contains property ` photos ` with type of ` Photo ` ,
869
869
and ` Photo ` contains link ` user ` to its parent ` User ` , then ` user ` will be ignored during transformation.
870
- Circular references are not ignored only during ` classToClass ` operation.
870
+ Circular references are not ignored only during ` instanceToInstance ` operation.
871
871
872
872
## Example with Angular2[ ⬆] ( #table-of-contents )
873
873
874
874
Lets say you want to download users and want them automatically to be mapped to the instances of ` User ` class.
875
875
876
876
``` typescript
877
- import { plainToClass } from ' class-transformer' ;
877
+ import { plainToInstance } from ' class-transformer' ;
878
878
879
879
this .http
880
880
.get (' users.json' )
881
881
.map (res => res .json ())
882
- .map (res => plainToClass (User , res as Object []))
882
+ .map (res => plainToInstance (User , res as Object []))
883
883
.subscribe (users => {
884
884
// now "users" is type of User[] and each user has getName() and isAdult() methods available
885
885
console .log (users );
0 commit comments