@@ -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::
@@ -594,7 +594,7 @@ There are several types of normalizers available:
594
594
``firstName ``).
595
595
596
596
The ``ObjectNormalizer `` is the most powerful normalizer. It is configured by
597
- default when using the Symfony Standard Edition with the serializer enabled.
597
+ default in Symfony applications with the Serializer component enabled.
598
598
599
599
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
600
600
This normalizer reads the content of the class by calling the "getters"
@@ -684,8 +684,8 @@ The Serializer component provides several built-in encoders:
684
684
:class: `Symfony\\ Component\\ Serializer\\ Encoder\\ CsvEncoder `
685
685
This encoder encodes and decodes data in CSV _.
686
686
687
- All these encoders are enabled by default when using the Symfony Standard Edition
688
- with the serializer enabled .
687
+ All these encoders are enabled by default when using the Serializer component
688
+ in a Symfony application .
689
689
690
690
The ``JsonEncoder ``
691
691
~~~~~~~~~~~~~~~~~~~
@@ -925,8 +925,8 @@ Here, we set it to 2 for the ``$child`` property:
925
925
</serializer >
926
926
927
927
The metadata loader corresponding to the chosen format must be configured in
928
- order to use this feature. It is done automatically when using the Symfony
929
- Standard Edition . When using the standalone component, refer to
928
+ order to use this feature. It is done automatically when using the Serializer component
929
+ in a Symfony application . When using the standalone component, refer to
930
930
:ref: `the groups documentation <component-serializer-attributes-groups >` to
931
931
learn how to do that.
932
932
@@ -1160,11 +1160,11 @@ context option::
1160
1160
Recursive Denormalization and Type Safety
1161
1161
-----------------------------------------
1162
1162
1163
- The Serializer Component can use the :doc: `PropertyInfo Component </components/property_info >` to denormalize
1163
+ The Serializer component can use the :doc: `PropertyInfo Component </components/property_info >` to denormalize
1164
1164
complex types (objects). The type of the class' property will be guessed using the provided
1165
1165
extractor and used to recursively denormalize the inner data.
1166
1166
1167
- When using the Symfony Standard Edition , all normalizers are automatically configured to use the registered extractors.
1167
+ When using this component in a Symfony application , all normalizers are automatically configured to use the registered extractors.
1168
1168
When using the component standalone, an implementation of :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyTypeExtractorInterface `,
1169
1169
(usually an instance of :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyInfoExtractor `) must be passed as the 4th
1170
1170
parameter of the ``ObjectNormalizer ``::
@@ -1239,9 +1239,14 @@ between the possible objects. In practice, when using the Serializer component,
1239
1239
pass a :class: `Symfony\\ Component\\ Serializer\\ Mapping\\ ClassDiscriminatorResolverInterface `
1240
1240
implementation to the :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `.
1241
1241
1242
- Consider an application that defines an abstract ``CodeRepository `` class
1243
- extended by ``GitHubCodeRepository `` and ``BitBucketCodeRepository `` classes.
1244
- This example shows how to serialize and deserialize those objects::
1242
+ The Serializer component provides an implementation of ``ClassDiscriminatorResolverInterface ``
1243
+ called :class: `Symfony\\ Component\\ Serializer\\ Mapping\\ ClassDiscriminatorFromClassMetadata `
1244
+ which uses the class metadata factory and a mapping configuration to serialize
1245
+ and deserialize objects of the correct class.
1246
+
1247
+ When using this component inside a Symfony application and the class metadata factory is enabled
1248
+ as explained in the :ref: `Attributes Groups section <component-serializer-attributes-groups >`,
1249
+ this is already set up and you only need to provide the configuration. Otherwise::
1245
1250
1246
1251
// ...
1247
1252
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -1253,25 +1258,15 @@ This example shows how to serialize and deserialize those objects::
1253
1258
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
1254
1259
1255
1260
$discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);
1256
- $discriminator->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
1257
- 'github' => GitHubCodeRepository::class,
1258
- 'bitbucket' => BitBucketCodeRepository::class,
1259
- ]));
1260
1261
1261
1262
$serializer = new Serializer(
1262
1263
array(new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator)),
1263
1264
array('json' => new JsonEncoder())
1264
1265
);
1265
1266
1266
- $serialized = $serializer->serialize(new GitHubCodeRepository());
1267
- // {"type": "github"}
1268
-
1269
- $repository = $serializer->deserialize($serialized, CodeRepository::class, 'json');
1270
- // instanceof GitHubCodeRepository
1271
-
1272
- If the class metadata factory is enabled as explained in the
1273
- :ref: `Attributes Groups section <component-serializer-attributes-groups >`, you
1274
- can use this simpler configuration:
1267
+ Now configure your discriminator class mapping. Consider an application that
1268
+ defines an abstract ``CodeRepository `` class extended by ``GitHubCodeRepository ``
1269
+ and ``BitBucketCodeRepository `` classes:
1275
1270
1276
1271
.. configuration-block ::
1277
1272
@@ -1317,6 +1312,14 @@ can use this simpler configuration:
1317
1312
</class >
1318
1313
</serializer >
1319
1314
1315
+ Once configured, the serializer uses the mapping to pick the correct class::
1316
+
1317
+ $serialized = $serializer->serialize(new GitHubCodeRepository());
1318
+ // {"type": "github"}
1319
+
1320
+ $repository = $serializer->deserialize($serialized, CodeRepository::class, 'json');
1321
+ // instanceof GitHubCodeRepository
1322
+
1320
1323
Performance
1321
1324
-----------
1322
1325
@@ -1339,7 +1342,7 @@ and return ``true`` when
1339
1342
is called.
1340
1343
1341
1344
.. note ::
1342
-
1345
+
1343
1346
All built-in :ref: `normalizers and denormalizers <component-serializer-normalizers >`
1344
1347
as well the ones included in `API Platform `_ natively implement this interface.
1345
1348
@@ -1359,7 +1362,7 @@ Learn more
1359
1362
1360
1363
.. seealso ::
1361
1364
1362
- A popular alternative to the Symfony Serializer Component is the third-party
1365
+ A popular alternative to the Symfony Serializer component is the third-party
1363
1366
library, `JMS serializer `_ (versions before ``v1.12.0 `` were released under
1364
1367
the Apache license, so incompatible with GPLv2 projects).
1365
1368
0 commit comments