@@ -12,6 +12,8 @@ This tool is super useful on both frontend and backend.
12
12
Example how to use with angular 2 in [ plunker] ( http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R ) .
13
13
Source code is available [ here] ( https://github.com/pleerock/class-transformer-demo ) .
14
14
15
+ ## Table of contents
16
+
15
17
- [ What is class-transformer] ( #what-is-class-transformer )
16
18
- [ Installation] ( #installation )
17
19
- [ Node.js] ( #nodejs )
@@ -47,7 +49,7 @@ Source code is available [here](https://github.com/pleerock/class-transformer-de
47
49
- [ Samples] ( #samples )
48
50
- [ Release notes] ( #release-notes )
49
51
50
- ## What is class-transformer
52
+ ## What is class-transformer[ ⬆ ] ( #table-of-contents )
51
53
52
54
In JavaScript there are two types of objects:
53
55
@@ -145,9 +147,9 @@ fetch('users.json').then((users: Object[]) => {
145
147
146
148
Now you can use ` users[0].getName() ` and ` users[0].isAdult() ` methods.
147
149
148
- ## Installation
150
+ ## Installation[ ⬆ ] ( #table-of-contents )
149
151
150
- ### Node.js
152
+ ### Node.js[ ⬆ ] ( #table-of-contents )
151
153
152
154
1 . Install module:
153
155
@@ -173,7 +175,7 @@ Now you can use `users[0].getName()` and `users[0].isAdult()` methods.
173
175
import ' es6-shim' ;
174
176
```
175
177
176
- ### Browser
178
+ ### Browser[ ⬆ ] ( #table-of-contents )
177
179
178
180
1 . Install module:
179
181
@@ -210,9 +212,9 @@ Now you can use `users[0].getName()` and `users[0].isAdult()` methods.
210
212
}
211
213
```
212
214
213
- ## Methods
215
+ ## Methods[ ⬆ ] ( #table-of-contents )
214
216
215
- ### plainToClass
217
+ ### plainToClass[ ⬆ ] ( #table-of-contents )
216
218
217
219
This method transforms a plain javascript object to instance of specific class.
218
220
@@ -222,7 +224,7 @@ import { plainToClass } from 'class-transformer';
222
224
let users = plainToClass (User , userJson ); // to convert user plain object a single user. also supports arrays
223
225
```
224
226
225
- ### plainToClassFromExist
227
+ ### plainToClassFromExist[ ⬆ ] ( #table-of-contents )
226
228
227
229
This method transforms a plain object into a instance using a already filled Object which is a instance from the target class.
228
230
@@ -233,7 +235,7 @@ defaultUser.role = 'user';
233
235
let mixedUser = plainToClassFromExist (defaultUser , user ); // mixed user should have the value role = user when no value is set otherwise.
234
236
```
235
237
236
- ### classToPlain
238
+ ### classToPlain[ ⬆ ] ( #table-of-contents )
237
239
238
240
This method transforms your class object back to plain javascript object, that can be ` JSON.stringify ` later.
239
241
@@ -242,7 +244,7 @@ import { classToPlain } from 'class-transformer';
242
244
let photo = classToPlain (photo );
243
245
```
244
246
245
- ### classToClass
247
+ ### classToClass[ ⬆ ] ( #table-of-contents )
246
248
247
249
This method transforms your class object into new instance of the class object.
248
250
This maybe treated as deep clone of your objects.
@@ -254,7 +256,7 @@ let photo = classToClass(photo);
254
256
255
257
You can also use a ` ignoreDecorators ` option in transformation options to ignore all decorators you classes is using.
256
258
257
- ### serialize
259
+ ### serialize[ ⬆ ] ( #table-of-contents )
258
260
259
261
You can serialize your model right to the json using ` serialize ` method:
260
262
@@ -265,7 +267,7 @@ let photo = serialize(photo);
265
267
266
268
` serialize ` works with both arrays and non-arrays.
267
269
268
- ### deserialize and deserializeArray
270
+ ### deserialize and deserializeArray[ ⬆ ] ( #table-of-contents )
269
271
270
272
You can deserialize your model to from a json using ` deserialize ` method:
271
273
@@ -281,7 +283,7 @@ import { deserializeArray } from 'class-transformer';
281
283
let photos = deserializeArray (Photo , photos );
282
284
```
283
285
284
- ## Enforcing type-safe instance
286
+ ## Enforcing type-safe instance[ ⬆ ] ( #table-of-contents )
285
287
286
288
The default behaviour of the ` plainToClass ` method is to set _ all_ properties from the plain object,
287
289
even those which are not specified in the class.
@@ -337,7 +339,7 @@ console.log(plainToClass(User, fromPlainUser, { excludeExtraneousValues: true })
337
339
// }
338
340
```
339
341
340
- ## Working with nested objects
342
+ ## Working with nested objects[ ⬆ ] ( #table-of-contents )
341
343
342
344
When you are trying to transform objects that have nested objects,
343
345
its required to known what type of object you are trying to transform.
@@ -369,7 +371,7 @@ let album = plainToClass(Album, albumJson);
369
371
// now album is Album object with Photo objects inside
370
372
```
371
373
372
- ### Providing more than one type option
374
+ ### Providing more than one type option[ ⬆ ] ( #table-of-contents )
373
375
374
376
In case the nested object can be of different types, you can provide an additional options object,
375
377
that specifies a discriminator. The discriminator option must define a ` property ` that holds the sub
@@ -440,7 +442,7 @@ let album = plainToClass(Album, albumJson);
440
442
Hint: The same applies for arrays with different sub types. Moreover you can specify ` keepDiscriminatorProperty: true `
441
443
in the options to keep the discriminator property also inside your resulting class.
442
444
443
- ## Exposing getters and method return values
445
+ ## Exposing getters and method return values[ ⬆ ] ( #table-of-contents )
444
446
445
447
You can expose what your getter or method return by setting a ` @Expose() ` decorator to those getters or methods:
446
448
@@ -465,7 +467,7 @@ export class User {
465
467
}
466
468
```
467
469
468
- ## Exposing properties with different names
470
+ ## Exposing properties with different names[ ⬆ ] ( #table-of-contents )
469
471
470
472
If you want to expose some of properties with a different name,
471
473
you can do it by specifying a ` name ` option to ` @Expose ` decorator:
@@ -491,7 +493,7 @@ export class User {
491
493
}
492
494
```
493
495
494
- ## Skipping specific properties
496
+ ## Skipping specific properties[ ⬆ ] ( #table-of-contents )
495
497
496
498
Sometimes you want to skip some properties during transformation.
497
499
This can be done using ` @Exclude ` decorator:
@@ -511,7 +513,7 @@ export class User {
511
513
512
514
Now when you transform a User, ` password ` property will be skipped and not be included in the transformed result.
513
515
514
- ## Skipping depend of operation
516
+ ## Skipping depend of operation[ ⬆ ] ( #table-of-contents )
515
517
516
518
You can control on what operation you will exclude a property. Use ` toClassOnly ` or ` toPlainOnly ` options:
517
519
@@ -530,7 +532,7 @@ export class User {
530
532
531
533
Now ` password ` property will be excluded only during ` classToPlain ` operation. Oppositely, use ` toClassOnly ` option.
532
534
533
- ## Skipping all properties of the class
535
+ ## Skipping all properties of the class[ ⬆ ] ( #table-of-contents )
534
536
535
537
You can skip all properties of the class, and expose only those are needed explicitly:
536
538
@@ -559,7 +561,7 @@ let photo = classToPlain(photo, { strategy: 'excludeAll' });
559
561
560
562
In this case you don't need to ` @Exclude() ` a whole class.
561
563
562
- ## Skipping private properties, or some prefixed properties
564
+ ## Skipping private properties, or some prefixed properties[ ⬆ ] ( #table-of-contents )
563
565
564
566
If you name your private properties with a prefix, lets say with ` _ ` ,
565
567
then you can exclude such properties from transformation too:
@@ -603,7 +605,7 @@ const plainUser = classToPlain(user, { excludePrefixes: ['_'] });
603
605
// { id: 1, name: "Johny Cage" }
604
606
```
605
607
606
- ## Using groups to control excluded properties
608
+ ## Using groups to control excluded properties[ ⬆ ] ( #table-of-contents )
607
609
608
610
You can use groups to control what data will be exposed and what will not be:
609
611
@@ -626,7 +628,7 @@ let user1 = classToPlain(user, { groups: ['user'] }); // will contain id, name,
626
628
let user2 = classToPlain (user , { groups: [' admin' ] }); // will contain id, name and email
627
629
```
628
630
629
- ## Using versioning to control exposed and excluded properties
631
+ ## Using versioning to control exposed and excluded properties[ ⬆ ] ( #table-of-contents )
630
632
631
633
If you are building an API that has different versions, class-transformer has extremely useful tools for that.
632
634
You can control which properties of your model should be exposed or excluded in what version. Example:
@@ -653,7 +655,7 @@ let user4 = classToPlain(user, { version: 2 }); // will contain id and name
653
655
let user5 = classToPlain (user , { version: 2.1 }); // will contain id, name and password
654
656
```
655
657
656
- ## Сonverting date strings into Date objects
658
+ ## Сonverting date strings into Date objects[ ⬆ ] ( #table-of-contents )
657
659
658
660
Sometimes you have a Date in your plain javascript object received in a string format.
659
661
And you want to create a real javascript Date object from it.
@@ -679,7 +681,7 @@ Note, that dates will be converted to strings when you'll try to convert class o
679
681
Same technique can be used with ` Number ` , ` String ` , ` Boolean `
680
682
primitive types when you want to convert your values into these types.
681
683
682
- ## Working with arrays
684
+ ## Working with arrays[ ⬆ ] ( #table-of-contents )
683
685
684
686
When you are using arrays you must provide a type of the object that array contains.
685
687
This type, you specify in a ` @Type() ` decorator:
@@ -741,9 +743,9 @@ export class Player {
741
743
}
742
744
```
743
745
744
- ## Additional data transformation
746
+ ## Additional data transformation[ ⬆ ] ( #table-of-contents )
745
747
746
- ### Basic usage
748
+ ### Basic usage[ ⬆ ] ( #table-of-contents )
747
749
748
750
You can perform additional data transformation using ` @Transform ` decorator.
749
751
For example, you want to make your ` Date ` object to be a ` moment ` object when you are
@@ -767,7 +769,7 @@ Now when you call `plainToClass` and send a plain representation of the Photo ob
767
769
it will convert a date value in your photo object to moment date.
768
770
` @Transform ` decorator also supports groups and versioning.
769
771
770
- ### Advanced usage
772
+ ### Advanced usage[ ⬆ ] ( #table-of-contents )
771
773
772
774
The ` @Transform ` decorator is given more arguments to let you configure how you want the transformation to be done.
773
775
@@ -781,7 +783,7 @@ The `@Transform` decorator is given more arguments to let you configure how you
781
783
| ` obj ` | The transformation source object. |
782
784
| ` type ` | The transformation type. |
783
785
784
- ## Other decorators
786
+ ## Other decorators[ ⬆ ] ( #table-of-contents )
785
787
786
788
| Signature | Example | Description |
787
789
| ------------------------ | ---------------------------------------------------- | ------------------------------------------------------------------------------------- |
@@ -830,14 +832,14 @@ const user = controller.getUser();
830
832
the ` user ` variable will contain only firstName,lastName, email properties becuase they are
831
833
the exposed variables. email property is also exposed becuase we metioned the group "user.email".
832
834
833
- ## Working with generics
835
+ ## Working with generics[ ⬆ ] ( #table-of-contents )
834
836
835
837
Generics are not supported because TypeScript does not have good reflection abilities yet.
836
838
Once TypeScript team provide us better runtime type reflection tools, generics will be implemented.
837
839
There are some tweaks however you can use, that maybe can solve your problem.
838
840
[ Checkout this example.] ( https://github.com/pleerock/class-transformer/tree/master/sample/sample4-generics )
839
841
840
- ## Implicit type conversion
842
+ ## Implicit type conversion[ ⬆ ] ( #table-of-contents )
841
843
842
844
> ** NOTE** If you use class-validator together with class-transformer you propably DON'T want to enable this function.
843
845
@@ -860,14 +862,14 @@ const result2 = plainToClass(MyPayload, { prop: 1234 }, { enableImplicitConversi
860
862
*/
861
863
```
862
864
863
- ## How does it handle circular references?
865
+ ## How does it handle circular references?[ ⬆ ] ( #table-of-contents )
864
866
865
867
Circular references are ignored.
866
868
For example, if you are transforming class ` User ` that contains property ` photos ` with type of ` Photo ` ,
867
869
and ` Photo ` contains link ` user ` to its parent ` User ` , then ` user ` will be ignored during transformation.
868
870
Circular references are not ignored only during ` classToClass ` operation.
869
871
870
- ## Example with Angular2
872
+ ## Example with Angular2[ ⬆ ] ( #table-of-contents )
871
873
872
874
Lets say you want to download users and want them automatically to be mapped to the instances of ` User ` class.
873
875
@@ -889,11 +891,11 @@ You can also inject a class `ClassTransformer` as a service in `providers`, and
889
891
Example how to use with angular 2 in [ plunker] ( http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R ) .
890
892
Source code is [ here] ( https://github.com/pleerock/class-transformer-demo ) .
891
893
892
- ## Samples
894
+ ## Samples[ ⬆ ] ( #table-of-contents )
893
895
894
896
Take a look on samples in [ ./sample] ( https://github.com/pleerock/class-transformer/tree/master/sample ) for more examples of
895
897
usages.
896
898
897
- ## Release notes
899
+ ## Release notes[ ⬆ ] ( #table-of-contents )
898
900
899
901
See information about breaking changes and release notes [ here] ( https://github.com/typestack/class-transformer/blob/master/CHANGELOG.md ) .
0 commit comments