@@ -424,7 +424,7 @@ Converting Property Names when Serializing and Deserializing
424
424
Sometimes serialized attributes must be named differently than properties
425
425
or getter/setter methods of PHP classes.
426
426
427
- The Serializer Component provides a handy way to translate or map PHP field
427
+ The Serializer component provides a handy way to translate or map PHP field
428
428
names to serialized names: The Name Converter System.
429
429
430
430
Given you have the following object::
@@ -582,7 +582,7 @@ There are several types of normalizers available:
582
582
``firstName ``).
583
583
584
584
The ``ObjectNormalizer `` is the most powerful normalizer. It is configured by
585
- default when using the Symfony Standard Edition with the serializer enabled.
585
+ default in Symfony applications with the Serializer component enabled.
586
586
587
587
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
588
588
This normalizer reads the content of the class by calling the "getters"
@@ -672,8 +672,8 @@ The Serializer component provides several built-in encoders:
672
672
:class: `Symfony\\ Component\\ Serializer\\ Encoder\\ CsvEncoder `
673
673
This encoder encodes and decodes data in CSV _.
674
674
675
- All these encoders are enabled by default when using the Symfony Standard Edition
676
- with the serializer enabled .
675
+ All these encoders are enabled by default when using the Serializer component
676
+ in a Symfony application .
677
677
678
678
The ``JsonEncoder ``
679
679
~~~~~~~~~~~~~~~~~~~
@@ -908,8 +908,8 @@ Here, we set it to 2 for the ``$child`` property:
908
908
</serializer >
909
909
910
910
The metadata loader corresponding to the chosen format must be configured in
911
- order to use this feature. It is done automatically when using the Symfony
912
- Standard Edition . When using the standalone component, refer to
911
+ order to use this feature. It is done automatically when using the Serializer component
912
+ in a Symfony application . When using the standalone component, refer to
913
913
:ref: `the groups documentation <component-serializer-attributes-groups >` to
914
914
learn how to do that.
915
915
@@ -1138,11 +1138,11 @@ context option::
1138
1138
Recursive Denormalization and Type Safety
1139
1139
-----------------------------------------
1140
1140
1141
- The Serializer Component can use the :doc: `PropertyInfo Component </components/property_info >` to denormalize
1141
+ The Serializer component can use the :doc: `PropertyInfo Component </components/property_info >` to denormalize
1142
1142
complex types (objects). The type of the class' property will be guessed using the provided
1143
1143
extractor and used to recursively denormalize the inner data.
1144
1144
1145
- When using the Symfony Standard Edition , all normalizers are automatically configured to use the registered extractors.
1145
+ When using this component in a Symfony application , all normalizers are automatically configured to use the registered extractors.
1146
1146
When using the component standalone, an implementation of :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyTypeExtractorInterface `,
1147
1147
(usually an instance of :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyInfoExtractor `) must be passed as the 4th
1148
1148
parameter of the ``ObjectNormalizer ``::
@@ -1217,9 +1217,14 @@ between the possible objects. In practice, when using the Serializer component,
1217
1217
pass a :class: `Symfony\\ Component\\ Serializer\\ Mapping\\ ClassDiscriminatorResolverInterface `
1218
1218
implementation to the :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `.
1219
1219
1220
- Consider an application that defines an abstract ``CodeRepository `` class
1221
- extended by ``GitHubCodeRepository `` and ``BitBucketCodeRepository `` classes.
1222
- This example shows how to serialize and deserialize those objects::
1220
+ The Serializer component provides an implementation of ``ClassDiscriminatorResolverInterface ``
1221
+ called :class: `Symfony\\ Component\\ Serializer\\ Mapping\\ ClassDiscriminatorFromClassMetadata `
1222
+ which uses the class metadata factory and a mapping configuration to serialize
1223
+ and deserialize objects of the correct class.
1224
+
1225
+ When using this component inside a Symfony application and the class metadata factory is enabled
1226
+ as explained in the :ref: `Attributes Groups section <component-serializer-attributes-groups >`,
1227
+ this is already set up and you only need to provide the configuration. Otherwise::
1223
1228
1224
1229
// ...
1225
1230
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -1231,25 +1236,15 @@ This example shows how to serialize and deserialize those objects::
1231
1236
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
1232
1237
1233
1238
$discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);
1234
- $discriminator->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
1235
- 'github' => GitHubCodeRepository::class,
1236
- 'bitbucket' => BitBucketCodeRepository::class,
1237
- ]));
1238
1239
1239
1240
$serializer = new Serializer(
1240
1241
array(new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator)),
1241
1242
array('json' => new JsonEncoder())
1242
1243
);
1243
1244
1244
- $serialized = $serializer->serialize(new GitHubCodeRepository());
1245
- // {"type": "github"}
1246
-
1247
- $repository = $serializer->deserialize($serialized, CodeRepository::class, 'json');
1248
- // instanceof GitHubCodeRepository
1249
-
1250
- If the class metadata factory is enabled as explained in the
1251
- :ref: `Attributes Groups section <component-serializer-attributes-groups >`, you
1252
- can use this simpler configuration:
1245
+ Now configure your discriminator class mapping. Consider an application that
1246
+ defines an abstract ``CodeRepository `` class extended by ``GitHubCodeRepository ``
1247
+ and ``BitBucketCodeRepository `` classes:
1253
1248
1254
1249
.. configuration-block ::
1255
1250
@@ -1295,6 +1290,14 @@ can use this simpler configuration:
1295
1290
</class >
1296
1291
</serializer >
1297
1292
1293
+ Once configured, the serializer uses the mapping to pick the correct class::
1294
+
1295
+ $serialized = $serializer->serialize(new GitHubCodeRepository());
1296
+ // {"type": "github"}
1297
+
1298
+ $repository = $serializer->deserialize($serialized, CodeRepository::class, 'json');
1299
+ // instanceof GitHubCodeRepository
1300
+
1298
1301
Performance
1299
1302
-----------
1300
1303
@@ -1317,7 +1320,7 @@ and return ``true`` when
1317
1320
is called.
1318
1321
1319
1322
.. note ::
1320
-
1323
+
1321
1324
All built-in :ref: `normalizers and denormalizers <component-serializer-normalizers >`
1322
1325
as well the ones included in `API Platform `_ natively implement this interface.
1323
1326
@@ -1337,7 +1340,7 @@ Learn more
1337
1340
1338
1341
.. seealso ::
1339
1342
1340
- A popular alternative to the Symfony Serializer Component is the third-party
1343
+ A popular alternative to the Symfony Serializer component is the third-party
1341
1344
library, `JMS serializer `_ (versions before ``v1.12.0 `` were released under
1342
1345
the Apache license, so incompatible with GPLv2 projects).
1343
1346
0 commit comments