-
-
Notifications
You must be signed in to change notification settings - Fork 587
/
Copy pathannotations.rst
974 lines (751 loc) · 40.4 KB
/
annotations.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
Attributes and annotations
==========================
PHP 8 support
~~~~~~~~~~~~~~~
JMS serializer now supports PHP 8 attributes, with a few caveats:
- Due to the missing support for nested attributes, the syntax for a few attributes has changed
(see the ``VirtualProperty`` ``options`` syntax here below)
- There is an edge case when setting this exact serialization group ``#[Groups(['value' => 'any value here'])]``.
(when there is only one item in th serialization groups array and has as key ``value`` the attribute will not work as expected,
please use the alternative syntax ``#[Groups(groups: ['value' => 'any value here'])]`` that works with no issues),
- Some support for unions exists. For unions of primitive types, the system will try to resolve them automatically. For
classes that contain union attributes, the ``#[UnionDiscriminator]`` attribute must be used to specify the type of the union.
Converting your annotations to attributes
-----------------------------------------
Example:
.. code-block :: php
/**
* @VirtualProperty(
* "classlow",
* exp="object.getVirtualValue(1)",
* options={@Until("8")}
* )
* @VirtualProperty(
* "classhigh",
* exp="object.getVirtualValue(8)",
* options={@Since("6")}
* )
*/
#[VirtualProperty('classlow', exp: 'object.getVirtualValue(1)', options: [[Until::class, ['8']]])]
#[VirtualProperty('classhigh', exp: 'object.getVirtualValue(8)', options: [[Since::class, ['6']]])]
class ObjectWithVersionedVirtualProperties
{
/**
* @Groups({"versions"})
* @VirtualProperty
* @SerializedName("low")
* @Until("8")
*/
#[Groups(['versions'])]
#[VirtualProperty]
#[SerializedName('low')]
#[Until('8')]
public function getVirtualLowValue()
{
return 1;
}
...
To automate migration of Annotations to Attributes you can use `rector/rector` with following config:
.. code-block :: php
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Symfony\Set\JMSSetList;
return static function (RectorConfig $rectorConfig) {
$rectorConfig->paths([
__DIR__ . '/src',
]);
$rectorConfig->sets([
JMSSetList::ANNOTATIONS_TO_ATTRIBUTES,
LevelSetList::UP_TO_PHP_80,
]);
};
#[ExclusionPolicy]
~~~~~~~~~~~~~~~~~~
This attribute can be defined on a class to indicate the exclusion strategy
that should be used for the class.
+----------+----------------------------------------------------------------+
| Policy | Description |
+==========+================================================================+
| all | all properties are excluded by default; only properties marked |
| | with #Expose will be serialized/unserialized |
+----------+----------------------------------------------------------------+
| none | no properties are excluded by default; all properties except |
| | those marked with #Exclude will be serialized/unserialized |
+----------+----------------------------------------------------------------+
#[Exclude]
~~~~~~~~~~
This attribute can be defined on a property or a class to indicate that the property or class
should not be serialized/unserialized. Works only in combination with NoneExclusionPolicy.
If the ``ExpressionLanguageExclusionStrategy`` exclusion strategy is enabled, it will
be possible to use ``#[Exclude(if:"expression")]`` to exclude dynamically a property
or an object if used on class level.
#[Expose]
~~~~~~~~~
This attribute can be defined on a property to indicate that the property should
be serialized/unserialized. Works only in combination with AllExclusionPolicy.
If the ``ExpressionLanguageExclusionStrategy`` exclusion strategy is enabled, will
be possible to use ``#Expose[if:"expression"]`` to expose dynamically a property.
#[SkipWhenEmpty]
~~~~~~~~~~~~~~~~
This attribute can be defined on a property to indicate that the property should
not be serialized if the result will be "empty".
Works option works only when serializing.
#[SerializedName]
~~~~~~~~~~~~~~~~~
This attribute can be defined on a property to define the serialized name for a
property. If this is not defined, the property will be translated from camel-case
to a lower-cased underscored name, e.g. camelCase -> camel_case.
Note that this attribute is not used when you're using any other naming
strategy than the default configuration (which includes the
``SerializedNameattributeStrategy``). In order to re-enable the attribute, you
will need to wrap your custom strategy with the ``SerializedNameattributeStrategy``.
.. code-block :: php
<?php
$serializer = \JMS\Serializer\SerializerBuilder::create()
->setPropertyNamingStrategy(
new \JMS\Serializer\Naming\SerializedNameattributeStrategy(
new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy()
)
)
->build();
#[Since]
~~~~~~~~
This attribute can be defined on a property to specify starting from which
version this property is available. If an earlier version is serialized, then
this property is excluded automatically. The version must be in a format that is
understood by PHP's ``version_compare`` function.
#[Until]
~~~~~~~~
This attribute can be defined on a property to specify until which version this
property was available. If a later version is serialized, then this property is
excluded automatically. The version must be in a format that is understood by
PHP's ``version_compare`` function.
#[Groups]
~~~~~~~~~
This attribute can be defined on a property to specify if the property
should be serialized when only serializing specific groups (see
:doc:`../cookbook/exclusion_strategies`).
#[MaxDepth]
~~~~~~~~~~~
This attribute can be defined on a property to limit the depth to which the
content will be serialized. It is very useful when a property will contain a
large object graph.
#[AccessType]
~~~~~~~~~~~~~
This attribute can be defined on a property, or a class to specify in which way
the properties should be accessed. By default, the serializer will retrieve, or
set the value via reflection, but you may change this to use a public method instead:
.. code-block :: php
<?php
use JMS\Serializer\Annotation\AccessType;
#[AccessType(type: 'public_method')]
class User
{
private $name;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = trim($name);
}
}
#[Accessor]
~~~~~~~~~~~
This attribute can be defined on a property to specify which public method should
be called to retrieve, or set the value of the given property:
.. code-block :: php
<?php
use JMS\Serializer\Annotation\Accessor;
class User
{
private $id;
#[Accessor(getter: 'getTrimmedName', setter: 'setName')]
private $name;
// ...
public function getTrimmedName()
{
return trim($this->name);
}
public function setName($name)
{
$this->name = $name;
}
}
.. note ::
If you need only to serialize your data, you can avoid providing a setter by
setting the property as read-only using the ``#[ReadOnlyProperty]`` attribute.
#[AccessorOrder]
~~~~~~~~~~~~~~~~
This attribute can be defined on a class to control the order of properties. By
default the order is undefined, but you may change it to either "alphabetical", or
"custom".
.. code-block :: php
<?php
use JMS\Serializer\Annotation\AccessorOrder;
use JMS\Serializer\Annotation\VirtualProperty;
use JMS\Serializer\Annotation\SerializedName;
#[AccessorOrder('alphabetical')]
class User
{
private $id;
private $name;
}
/**
* Resulting Property Order: name, id
*/
#[AccessorOrder(order: 'custom', custom: ['name', 'id'])]
class User
{
private $id;
private $name;
}
/**
* Resulting Property Order: name, mood, id
*/
#[AccessorOrder(order: 'custom', custom: ['name', 'someMethod', 'id'])]
class User
{
private $id;
private $name;
#[VirtualProperty]
#[SerializedName(name: 'mood')]
public function getSomeMethod(): string
{
return 'happy';
}
}
#[VirtualProperty]
~~~~~~~~~~~~~~~~~~
This attribute can be defined on a method to indicate that the data returned by
the method should appear like a property of the object.
A virtual property can be defined for a method of an object to serialize and can be
also defined at class level exposing data using the Symfony Expression Language.
.. code-block :: php
#[Serializer\VirtualProperty(name: 'firstName', exp: 'object.getFirstName()', options: [[Serializer\SerializedName::class, ['my_first_name']]])]
class Author
{
#[Serializer\Expose]
private $id;
#[Serializer\Exclude]
private $firstName;
#[Serializer\Exclude]
private $lastName;
#[Serializer\VirtualProperty]
public function getLastName(): string
{
return $this->lastName;
}
public function getFirstName(): string
{
return $this->firstName;
}
}
In this example:
- ``id`` is exposed using the object reflection.
- ``lastName`` is exposed using the ``getLastName`` getter method.
- ``firstName`` is exposed using the ``object.getFirstName()`` expression (``exp`` can contain any valid symfony expression).
``#[VirtualProperty]`` can also have an optional property ``name``, used to define the internal property name
(for sorting proposes as example). When not specified, it defaults to the method name with the "get" prefix removed.
.. note ::
This only works for serialization and is completely ignored during deserialization.
In PHP 8, due to the missing support for nested attributes, in the options array you need to pass an array with the class name and an array with the arguments for its constructor.
.. code-block :: php
/**
* @Serializer\VirtualProperty(
* "firstName",
* exp="object.getFirstName()",
* options={@Serializer\SerializedName("my_first_name")}
* )
*/
#[Serializer\VirtualProperty(name: "firstName", exp: "object.getFirstName()", options: [[Serializer\SerializedName::class, ["my_first_name"]]])]
class Author
{
...
#[Inline]
~~~~~~~~~
This attribute can be defined on a property to indicate that the data of the property
should be inlined.
**Note**: AccessorOrder will be using the name of the property to determine the order.
#[ReadOnlyProperty]
~~~~~~~~~~~~~~~~~~~
This attribute can be defined on a property to indicate that the data of the property
is read only and cannot be set during deserialization.
A property can be marked as non read only with ``#[ReadOnlyProperty(readOnly: false)]`` attribute
(useful when a class is marked as read only).
#[PreSerialize]
~~~~~~~~~~~~~~~
This attribute can be defined on a method which is supposed to be called before
the serialization of the object starts.
#[PostSerialize]
~~~~~~~~~~~~~~~~
This attribute can be defined on a method which is then called directly after the
object has been serialized.
#[PostDeserialize]
~~~~~~~~~~~~~~~~~~
This attribute can be defined on a method which is supposed to be called after
the object has been deserialized.
#[Discriminator]
~~~~~~~~~~~~~~~~
This attribute allows serialization/deserialization of relations which are polymorphic, but
where a common base class exists. The ``#[Discriminator]`` attribute has to be applied
to the least super type:
.. code-block :: php
#[Serializer\Discriminator(field: 'type', disabled: false, map: ['car' => 'Car', 'moped' => 'Moped'], groups=["foo", "bar"])]
abstract class Vehicle { }
class Car extends Vehicle { }
class Moped extends Vehicle { }
...
.. note ::
`groups` is optional and is used as exclusion policy.
#[UnionDiscriminator]
~~~~~~~~~~~~~~~~~~~~~
This attribute allows deserialization of unions. The ``#[UnionDiscriminator]`` attribute has to be applied
to an attribute that can be one of many types.
.. code-block :: php
class Vehicle {
#[UnionDiscriminator(field: 'typeField', map: ['manual' => 'FullyQualified/Path/Manual', 'automatic' => 'FullyQualified/Path/Automatic'])]
private Manual|Automatic $transmission;
}
In the case of this example, both Manual and Automatic should contain a string attribute named `typeField`. The value of that field will be passed
to the `map` option to determine which class to instantiate.
#[Type]
~~~~~~~
This attribute can be defined on a property to specify the type of that property.
For deserialization, this attribute must be defined.
The ``#[Type]`` attribute can have parameters and parameters can be used by serialization/deserialization
handlers to enhance the serialization or deserialization result; for example, you may want to
force a certain format to be used for serializing DateTime types and specifying at the same time a different format
used when deserializing them.
Available Types:
+------------------------------------------------------------+--------------------------------------------------+
| Type | Description |
+============================================================+==================================================+
| boolean or bool | Primitive boolean |
+------------------------------------------------------------+--------------------------------------------------+
| integer or int | Primitive integer |
+------------------------------------------------------------+--------------------------------------------------+
| double or float | Primitive double |
+------------------------------------------------------------+--------------------------------------------------+
| double<2> or float<2> | Primitive double with precision |
+------------------------------------------------------------+--------------------------------------------------+
| double<2, 'HALF_DOWN'> or float<2, 'HALF_DOWN'> | Primitive double with precision and |
| | Rounding Mode. |
| | (HALF_UP, HALF_DOWN, HALF_EVEN HALF_ODD) |
+------------------------------------------------------------+--------------------------------------------------+
| double<2, 'HALF_DOWN', 2> or float<2, 'HALF_DOWN', 2> | Primitive double with precision, |
| double<2, 'HALF_DOWN', 3> or float<2, 'HALF_DOWN', 3> | Rounding Mode and decimals padding up to |
| | N digits. As example, the float ``1.23456`` when |
| | specified as ``double<2, 'HALF_DOWN', 5>`` will |
| | be serialized as ``1.23000``. |
| | NOTE: this is available only for the XML |
| | serializer. |
+------------------------------------------------------------+--------------------------------------------------+
| string | Primitive string |
+------------------------------------------------------------+--------------------------------------------------+
| array | An array with arbitrary keys, and values. |
+------------------------------------------------------------+--------------------------------------------------+
| list | A list with arbitrary values. |
+------------------------------------------------------------+--------------------------------------------------+
| array<T> | An array of type T (T can be any available type).|
| | Examples: |
| | array<string>, array<MyNamespace\MyObject>, etc. |
+------------------------------------------------------------+--------------------------------------------------+
| list<T> | A list of type T (T can be any available type). |
| | Examples: |
| | list<string>, list<MyNamespace\MyObject>, etc. |
+------------------------------------------------------------+--------------------------------------------------+
| array<K, V> | A map of keys of type K to values of type V. |
| | Examples: array<string, string>, |
| | array<string, MyNamespace\MyObject>, etc. |
+------------------------------------------------------------+--------------------------------------------------+
| enum<'Color'> | Enum of type Color, use its case values |
| | for serialization and deserialization |
| | if the enum is a backed enum, |
| | use its case names if it is not a backed enum. |
+------------------------------------------------------------+--------------------------------------------------+
| enum<'Color', 'name'> | Enum of type Color, use its case names |
| | (as string) for serialization |
| | and deserialization. |
+------------------------------------------------------------+--------------------------------------------------+
| enum<'Color', 'value'> | Backed Enum of type Color, use its case value |
| | for serialization and deserialization. |
+------------------------------------------------------------+--------------------------------------------------+
| enum<'Color', 'value', 'integer'> | Backed Enum of type Color, use its case value |
| | (forced as integer) for serialization |
| | and deserialization. |
+------------------------------------------------------------+--------------------------------------------------+
| DateTime | PHP's DateTime object (default format*/timezone) |
+------------------------------------------------------------+--------------------------------------------------+
| DateTime<'format'> | PHP's DateTime object (custom format/default |
| | timezone). |
+------------------------------------------------------------+--------------------------------------------------+
| DateTime<'format', 'zone'> | PHP's DateTime object (custom format/timezone) |
+------------------------------------------------------------+--------------------------------------------------+
| DateTime<'format', 'zone', 'deserializeFormats'> | PHP's DateTime object (custom format/timezone, |
| | deserialize format). If you do not want to |
| | specify a specific timezone, use an empty |
| | string (''). DeserializeFormats can either be a |
| | string or an array of string. |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeImmutable | PHP's DateTimeImmutable object (default format*/ |
| | timezone). |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeImmutable<'format'> | PHP's DateTimeImmutable object (custom format/ |
| | default timezone) |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeImmutable<'format', 'zone'> | PHP's DateTimeImmutable object (custom format/ |
| | timezone) |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeImmutable<'format', 'zone', 'deserializeFormats'> | PHP's DateTimeImmutable object (custom format/ |
| | timezone/deserialize format). If you do not want |
| | to specify a specific timezone, use an empty |
| | string (''). DeserializeFormats can either be a |
| | string or an array of string. |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeInterface | PHP's DateTimeInterface interface (default |
| | format*/timezone). |
| | Data will be always deserialised into |
| | `\DateTime` object |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeInterface<'format'> | PHP's DateTimeInterface interface (custom |
| | format/default timezone) |
| | Data will be deserialised into |
| | `\\DateTime` object |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeInterface<'format', 'zone'> | PHP's DateTimeInterface interface (custom |
| | format/timezone) |
| | Data will be deserialised into |
| | `\\DateTime` object |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeInterface<'format', 'zone', 'deserializeFormats'> | PHP's DateTimeInterface interface (custom |
| | format/timezone/deserialize format). If you do |
| | not want to specify a specific timezone, use an |
| | empty string (''). DeserializeFormats can either |
| | be a string or an array of string. |
| | Data will be deserialised into |
| | `\\DateTime` object |
+------------------------------------------------------------+--------------------------------------------------+
| DateInterval | PHP's DateInterval object using ISO 8601 format |
+------------------------------------------------------------+--------------------------------------------------+
| T | Where T is a fully qualified class name. |
+------------------------------------------------------------+--------------------------------------------------+
| iterable | Similar to array. Will always be deserialized |
| | into array as implementation info is lost during |
| | serialization. |
+------------------------------------------------------------+--------------------------------------------------+
| iterable<T> | Similar to array<T>. Will always be deserialized |
| | into array as implementation info is lost during |
| | serialization. |
+------------------------------------------------------------+--------------------------------------------------+
| iterable<K, V> | Similar to array<K, V>. Will always be |
| | deserialized into array as implementation info |
| | is lost during serialization. |
+------------------------------------------------------------+--------------------------------------------------+
| ArrayCollection<T> | Similar to array<T>, but will be deserialized |
| | into Doctrine's ArrayCollection class. |
+------------------------------------------------------------+--------------------------------------------------+
| ArrayCollection<K, V> | Similar to array<K, V>, but will be deserialized |
| | into Doctrine's ArrayCollection class. |
+------------------------------------------------------------+--------------------------------------------------+
| Generator | Similar to array, but will be deserialized |
| | into Generator class. |
+------------------------------------------------------------+--------------------------------------------------+
| Generator<T> | Similar to array<T>, but will be deserialized |
| | into Generator class. |
+------------------------------------------------------------+--------------------------------------------------+
| Generator<K, V> | Similar to array<K, V>, but will be deserialized |
| | into Generator class. |
+------------------------------------------------------------+--------------------------------------------------+
| ArrayIterator | Similar to array, but will be deserialized |
| | into ArrayIterator class. |
+------------------------------------------------------------+--------------------------------------------------+
| ArrayIterator<T> | Similar to array<T>, but will be deserialized |
| | into ArrayIterator class. |
+------------------------------------------------------------+--------------------------------------------------+
| ArrayIterator<K, V> | Similar to array<K, V>, but will be deserialized |
| | into ArrayIterator class. |
+------------------------------------------------------------+--------------------------------------------------+
| Iterator | Similar to array, but will be deserialized |
| | into ArrayIterator class. |
+------------------------------------------------------------+--------------------------------------------------+
| Iterator<T> | Similar to array<T>, but will be deserialized |
| | into ArrayIterator class. |
+------------------------------------------------------------+--------------------------------------------------+
| Iterator<K, V> | Similar to array<K, V>, but will be deserialized |
| | into ArrayIterator class. |
+------------------------------------------------------------+--------------------------------------------------+
(*) If the standalone jms/serializer is used then default format is `\DateTime::ISO8601` (which is not compatible with ISO-8601 despite the name). For jms/serializer-bundle the default format is `\DateTime::ATOM` (the real ISO-8601 format) but it can be changed in `configuration`_.
(**) The key type K for array-linke formats as ``array``. ``ArrayCollection``, ``iterable``, etc., is only used for deserialization,
for serializaiton is treated as ``string``.
Examples:
.. code-block :: php
<?php
namespace MyNamespace;
use JMS\Serializer\Annotation\Type;
class BlogPost
{
#[Type(name: "ArrayCollection<MyNamespace\Comment>")]
private $comments;
#[Type(name: "string")]
private $title;
#[Type(name: Author:class)]
private $author;
#[Type(name: DateTime:class)]
private $startAt;
#[Type(name: 'DateTime<'Y-m-d'>')]
private $endAt;
#[Type(name: 'DateTime<'Y-m-d'>')]
#[Type(name:"DateTime<'Y-m-d', '', ['Y-m-d', 'Y/m/d']>")]
private $publishedAt;
#[Type(name:'DateTimeImmutable')]
private $createdAt;
#[Type(name:"DateTimeImmutable<'Y-m-d'>")]
private $updatedAt;
#[Type(name:"DateTimeImmutable<'Y-m-d', '', ['Y-m-d', 'Y/m/d']>")]
private $deletedAt;
#[Type(name:'boolean')]
private $published;
#[Type(name:'array<string, string>')]
private $keyValueStore;
}
.. note ::
if you are using ``PHP attributes`` with PHP 8.1 you can pass an object which implements ``__toString()`` method as a value for ``#[Type]`` attribute.
.. code-block :: php
<?php
namespace MyNamespace;
use JMS\Serializer\Annotation\Type;
class BlogPost
{
#[Type(new ArrayOf(Comment::class))]
private $comments;
}
class ArrayOf implements \Stringable
{
public function __construct(private string $className) {}
public function __toString(): string
{
return "array<$className>";
}
}
.. _configuration: https://jmsyst.com/bundles/JMSSerializerBundle/master/configuration#configuration-block-2-0
#[XmlRoot]
~~~~~~~~~~
This allows you to specify the name of the top-level element.
.. code-block :: php
<?php
use JMS\Serializer\Annotation\XmlRoot;
#[XmlRoot('user')]
class User
{
private $name = 'Johannes';
}
Resulting XML:
.. code-block :: xml
<user>
<name><![CDATA[Johannes]]></name>
</user>
.. note ::
#[XmlRoot] only applies to the root element, but is for example not taken into
account for collections. You can define the entry name for collections using
#[XmlList], or #[XmlMap].
#[XmlAttribute]
~~~~~~~~~~~~~~~
This allows you to mark properties which should be set as attributes,
and not as child elements.
.. code-block :: php
<?php
use JMS\Serializer\Annotation\XmlAttribute;
class User
{
#[XmlAttribute]
private $id = 1;
private $name = 'Johannes';
}
Resulting XML:
.. code-block :: xml
<result id="1">
<name><![CDATA[Johannes]]></name>
</result>
#[XmlDiscriminator]
~~~~~~~~~~~~~~~~~~~
This attribute allows to modify the behaviour of ``#[Discriminator]`` regarding handling of XML.
Available Options:
+-------------------------------------+--------------------------------------------------+
| Type | Description |
+=====================================+==================================================+
| attribute | use an attribute instead of a child node |
+-------------------------------------+--------------------------------------------------+
| cdata | render child node content with or without cdata |
+-------------------------------------+--------------------------------------------------+
| namespace | render child node using the specified namespace |
+-------------------------------------+--------------------------------------------------+
Example for "attribute":
.. code-block :: php
<?php
use JMS\Serializer\Annotation\Discriminator;
use JMS\Serializer\Annotation\XmlDiscriminator;
#[Discriminator(field: 'type', map: ['car' => 'Car', 'moped' => 'Moped'], groups: ['foo', 'bar'])]
#[XmlDiscriminator(attribute: true)]
abstract class Vehicle { }
class Car extends Vehicle { }
Resulting XML:
.. code-block :: xml
<vehicle type="car" />
Example for "cdata":
.. code-block :: php
<?php
use JMS\Serializer\Annotation\Discriminator;
use JMS\Serializer\Annotation\XmlDiscriminator;
#[Discriminator(field: 'type', map: ['car' => 'Car', 'moped' => 'Moped'], groups: ['foo', 'bar'])]
#[XmlDiscriminator]
abstract class Vehicle { }
class Car extends Vehicle { }
Resulting XML:
.. code-block :: xml
<vehicle><type>car</type></vehicle>
#[XmlValue]
~~~~~~~~~~~
This allows you to mark properties which should be set as the value of the
current element. Note that this has the limitation that any additional
properties of that object must have the #[XmlAttribute] attribute.
XMlValue also has property cdata. Which has the same meaning as the one in
XMLElement.
.. code-block :: php
<?php
use JMS\Serializer\Annotation\XmlAttribute;
use JMS\Serializer\Annotation\XmlValue;
use JMS\Serializer\Annotation\XmlRoot;
#[XmlRoot('price')]
class Price
{
#[XmlAttribute]
private $currency = 'EUR';
#[XmlValue]
private $amount = 1.23;
}
Resulting XML:
.. code-block :: xml
<price currency="EUR">1.23</price>
#[XmlList]
~~~~~~~~~~
This allows you to define several properties of how arrays should be
serialized. This is very similar to #[XmlMap], and should be used if the
keys of the array are not important.
.. code-block :: php
<?php
use JMS\Serializer\Annotation\XmlList;
use JMS\Serializer\Annotation\XmlRoot;
#[XmlRoot('post')]
class Post
{
public function __construct(
#[XmlList(inline: true, entry: 'comment')]
private array $comments
)
{
}
}
class Comment
{
public function __construct(private string $text)
{
}
}
Resulting XML:
.. code-block :: xml
<post>
<comment>
<text><![CDATA[Foo]]></text>
</comment>
<comment>
<text><![CDATA[Bar]]></text>
</comment>
</post>
You can also specify the entry tag namespace using the ``namespace`` attribute (``#[XmlList(inline: true, entry: 'comment', namespace: 'http://www.example.com/ns')]``).
#[XmlMap]
~~~~~~~~~
Similar to #[XmlList], but the keys of the array are meaningful.
#[XmlKeyValuePairs]
~~~~~~~~~~~~~~~~~~~
This allows you to use the keys of an array as xml tags.
.. note ::
When a key is an invalid xml tag name (e.g. 1_foo) the tag name *entry* will be used instead of the key.
#[XmlAttributeMap]
~~~~~~~~~~~~~~~~~~
This is similar to the #[XmlKeyValuePairs], but instead of creating child elements, it creates attributes.
.. code-block :: php
<?php
use JMS\Serializer\Annotation\XmlAttributeMap;
class Input
{
#[XmlAttributeMap]
private $id = ['name' => 'firstname', 'value' => 'Adrien'];
}
Resulting XML:
.. code-block :: xml
<result name="firstname" value="Adrien"/>
#[XmlElement]
~~~~~~~~~~~~~
This attribute can be defined on a property to add additional xml serialization/deserialization properties.
.. code-block :: php
<?php
use JMS\Serializer\Annotation\XmlElement;
use JMS\Serializer\Annotation\XmlNamespace;
#[XmlNamespace(uri: 'http://www.w3.org/2005/Atom', prefix: 'atom')]
class User
{
#[XmlElement(cdata: false, namespace: 'http://www.w3.org/2005/Atom')]
private $id = 'my_id';
}
Resulting XML:
.. code-block :: xml
<atom:id>my_id</atom:id>
#[XmlNamespace]
~~~~~~~~~~~~~~~
This attribute allows you to specify Xml namespace/s and prefix used.
.. code-block :: php
<?php
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlElement;
use JMS\Serializer\Annotation\XmlNamespace;
#[XmlNamespace(uri: 'http://example.com/namespace')]
#[XmlNamespace(uri: 'http://www.w3.org/2005/Atom', prefix: 'atom')]
class BlogPost
{
#[Type(\JMS\Serializer\Tests\Fixtures\Author::class)]
#[Groups(['post'])]
#[XmlElement(namespace: 'http://www.w3.org/2005/Atom')]
private $author;
}
class Author
{
#[Type('string')]
#[SerializedName('full_name')]
private $name;
}
Resulting XML:
.. code-block :: xml
<?xml version="1.0" encoding="UTF-8"?>
<blog-post xmlns="http://example.com/namespace" xmlns:atom="http://www.w3.org/2005/Atom">
<atom:author>
<full_name><![CDATA[Foo Bar]]></full_name>
</atom:author>
</blog>
Enum support
~~~~~~~~~~~~~~
Enum support is disabled by default, to enable it run:
.. code-block :: php
$builder = SerializerBuilder::create();
$builder->enableEnumSupport();
$serializer = $builder->build();
With the enum support enabled, enums are automatically detected using typed properties typehints.
When typed properties are no available (virtual properties as example), it is necessary to explicitly typehint
the underlying type using the ``#[Type]`` attribute.
- If the enum is a ``BackedEnum``, the case value will be used for serialization and deserialization by default;
- If the enum is not a ``BackedEnum``, the case name will be used for serialization and deserialization by default;