@@ -19,9 +19,9 @@ Source code is available [here](https://github.com/pleerock/class-transformer-de
1919 - [ Node.js] ( #nodejs )
2020 - [ Browser] ( #browser )
2121- [ Methods] ( #methods )
22- - [ plainToClass ] ( #plaintoclass )
22+ - [ plainToInstance ] ( #plaintoinstance )
2323 - [ plainToClassFromExist] ( #plaintoclassfromexist )
24- - [ classToPlain ] ( #classtoplain )
24+ - [ instanceToPlain ] ( #instancetoplain )
2525 - [ instanceToInstance] ( #instanceToInstance )
2626 - [ serialize] ( #serialize )
2727 - [ deserialize and deserializeArray] ( #deserialize-and-deserializearray )
@@ -140,7 +140,7 @@ Here is an example how it will look like:
140140
141141``` typescript
142142fetch (' users.json' ).then ((users : Object []) => {
143- const realUsers = plainToClass (User , users );
143+ const realUsers = plainToInstance (User , users );
144144 // now each user in realUsers is an instance of User class
145145});
146146```
@@ -214,14 +214,14 @@ Now you can use `users[0].getName()` and `users[0].isAdult()` methods.
214214
215215## Methods[ ⬆] ( #table-of-contents )
216216
217- ### plainToClass [ ⬆] ( #table-of-contents )
217+ ### plainToInstance [ ⬆] ( #table-of-contents )
218218
219219This method transforms a plain javascript object to instance of specific class.
220220
221221``` typescript
222- import { plainToClass } from ' class-transformer' ;
222+ import { plainToInstance } from ' class-transformer' ;
223223
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
225225```
226226
227227### plainToClassFromExist[ ⬆] ( #table-of-contents )
@@ -235,13 +235,13 @@ defaultUser.role = 'user';
235235let mixedUser = plainToClassFromExist (defaultUser , user ); // mixed user should have the value role = user when no value is set otherwise.
236236```
237237
238- ### classToPlain [ ⬆] ( #table-of-contents )
238+ ### instanceToPlain [ ⬆] ( #table-of-contents )
239239
240240This method transforms your class object back to plain javascript object, that can be ` JSON.stringify ` later.
241241
242242``` typescript
243- import { classToPlain } from ' class-transformer' ;
244- let photo = classToPlain (photo );
243+ import { instanceToPlain } from ' class-transformer' ;
244+ let photo = instanceToPlain (photo );
245245```
246246
247247### instanceToInstance[ ⬆] ( #table-of-contents )
@@ -285,11 +285,11 @@ let photos = deserializeArray(Photo, photos);
285285
286286## Enforcing type-safe instance[ ⬆] ( #table-of-contents )
287287
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,
289289even those which are not specified in the class.
290290
291291``` typescript
292- import { plainToClass } from ' class-transformer' ;
292+ import { plainToInstance } from ' class-transformer' ;
293293
294294class User {
295295 id: number ;
@@ -303,7 +303,7 @@ const fromPlainUser = {
303303 lastName: ' Khudoiberdiev' ,
304304};
305305
306- console .log (plainToClass (User , fromPlainUser ));
306+ console .log (plainToInstance (User , fromPlainUser ));
307307
308308// User {
309309// unkownProp: 'hello there',
@@ -313,10 +313,10 @@ console.log(plainToClass(User, fromPlainUser));
313313```
314314
315315If 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.
317317
318318``` typescript
319- import { Expose , plainToClass } from ' class-transformer' ;
319+ import { Expose , plainToInstance } from ' class-transformer' ;
320320
321321class User {
322322 @Expose () id: number ;
@@ -330,7 +330,7 @@ const fromPlainUser = {
330330 lastName: ' Khudoiberdiev' ,
331331};
332332
333- console .log (plainToClass (User , fromPlainUser , { excludeExtraneousValues: true }));
333+ console .log (plainToInstance (User , fromPlainUser , { excludeExtraneousValues: true }));
334334
335335// User {
336336// id: undefined,
@@ -351,7 +351,7 @@ Lets say we have an album with photos.
351351And we are trying to convert album plain object to class object:
352352
353353``` typescript
354- import { Type , plainToClass } from ' class-transformer' ;
354+ import { Type , plainToInstance } from ' class-transformer' ;
355355
356356export class Album {
357357 id: number ;
@@ -367,7 +367,7 @@ export class Photo {
367367 filename: string ;
368368}
369369
370- let album = plainToClass (Album , albumJson );
370+ let album = plainToInstance (Album , albumJson );
371371// now album is Album object with Photo objects inside
372372```
373373
@@ -399,7 +399,7 @@ the additional property `__type`. This property is removed during transformation
399399```
400400
401401``` typescript
402- import { Type , plainToClass } from ' class-transformer' ;
402+ import { Type , plainToInstance } from ' class-transformer' ;
403403
404404export abstract class Photo {
405405 id: number ;
@@ -435,7 +435,7 @@ export class Album {
435435 topPhoto: Landscape | Portrait | UnderWater ;
436436}
437437
438- let album = plainToClass (Album , albumJson );
438+ let album = plainToInstance (Album , albumJson );
439439// now album is Album object with a UnderWater object without `__type` property.
440440```
441441
@@ -530,7 +530,7 @@ export class User {
530530}
531531```
532532
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.
534534
535535## Skipping all properties of the class[ ⬆] ( #table-of-contents )
536536
@@ -555,8 +555,8 @@ Now `id` and `email` will be exposed, and password will be excluded during trans
555555Alternatively, you can set exclusion strategy during transformation:
556556
557557``` 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' });
560560```
561561
562562In 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 `_`,
567567then you can exclude such properties from transformation too:
568568
569569``` 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: [' _' ] });
572572```
573573
574574This will skip all properties that start with ` _ ` prefix.
575575You can pass any number of prefixes and all properties that begin with these prefixes will be ignored.
576576For example:
577577
578578``` typescript
579- import { Expose , classToPlain } from ' class-transformer' ;
579+ import { Expose , instanceToPlain } from ' class-transformer' ;
580580
581581export class User {
582582 id: number ;
@@ -600,7 +600,7 @@ user.id = 1;
600600user .setName (' Johny' , ' Cage' );
601601user ._password = ' 123' ;
602602
603- const plainUser = classToPlain (user , { excludePrefixes: [' _' ] });
603+ const plainUser = instanceToPlain (user , { excludePrefixes: [' _' ] });
604604// here plainUser will be equal to
605605// { id: 1, name: "Johny Cage" }
606606```
@@ -610,7 +610,7 @@ const plainUser = classToPlain(user, { excludePrefixes: ['_'] });
610610You can use groups to control what data will be exposed and what will not be:
611611
612612``` typescript
613- import { Exclude , Expose , classToPlain } from ' class-transformer' ;
613+ import { Exclude , Expose , instanceToPlain } from ' class-transformer' ;
614614
615615export class User {
616616 id: number ;
@@ -624,8 +624,8 @@ export class User {
624624 password: string ;
625625}
626626
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
629629```
630630
631631## 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
634634You can control which properties of your model should be exposed or excluded in what version. Example:
635635
636636``` typescript
637- import { Exclude , Expose , classToPlain } from ' class-transformer' ;
637+ import { Exclude , Expose , instanceToPlain } from ' class-transformer' ;
638638
639639export class User {
640640 id: number ;
@@ -648,11 +648,11 @@ export class User {
648648 password: string ;
649649}
650650
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
656656```
657657
658658## Сonverting date strings into Date objects[ ⬆] ( #table-of-contents )
@@ -763,7 +763,7 @@ export class Photo {
763763}
764764```
765765
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,
767767it will convert a date value in your photo object to moment date.
768768` @Transform ` decorator also supports groups and versioning.
769769
@@ -787,9 +787,9 @@ The `@Transform` decorator is given more arguments to let you configure how you
787787
788788| Signature | Example | Description |
789789| ------------------------ | ---------------------------------------------------- | ------------------------------------------------------------------------------------- |
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. |
793793
794794The above decorators accept one optional argument:
795795ClassTransformOptions - The transform options like groups, version, name
@@ -853,8 +853,8 @@ class MyPayload {
853853 prop: string ;
854854}
855855
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 });
858858
859859/**
860860 * 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
867867Circular references are ignored.
868868For example, if you are transforming class ` User ` that contains property ` photos ` with type of ` Photo ` ,
869869and ` 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.
871871
872872## Example with Angular2[ ⬆] ( #table-of-contents )
873873
874874Lets say you want to download users and want them automatically to be mapped to the instances of ` User ` class.
875875
876876``` typescript
877- import { plainToClass } from ' class-transformer' ;
877+ import { plainToInstance } from ' class-transformer' ;
878878
879879this .http
880880 .get (' users.json' )
881881 .map (res => res .json ())
882- .map (res => plainToClass (User , res as Object []))
882+ .map (res => plainToInstance (User , res as Object []))
883883 .subscribe (users => {
884884 // now "users" is type of User[] and each user has getName() and isAdult() methods available
885885 console .log (users );
0 commit comments