@@ -424,7 +424,7 @@ Converting Property Names when Serializing and Deserializing
424424Sometimes serialized attributes must be named differently than properties
425425or getter/setter methods of PHP classes.
426426
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
428428names to serialized names: The Name Converter System.
429429
430430Given you have the following object::
@@ -594,7 +594,7 @@ There are several types of normalizers available:
594594 ``firstName ``).
595595
596596 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.
598598
599599:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
600600 This normalizer reads the content of the class by calling the "getters"
@@ -684,8 +684,8 @@ The Serializer component provides several built-in encoders:
684684:class: `Symfony\\ Component\\ Serializer\\ Encoder\\ CsvEncoder `
685685 This encoder encodes and decodes data in CSV _.
686686
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 .
689689
690690The ``JsonEncoder ``
691691~~~~~~~~~~~~~~~~~~~
@@ -925,8 +925,8 @@ Here, we set it to 2 for the ``$child`` property:
925925 </serializer >
926926
927927 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
930930:ref: `the groups documentation <component-serializer-attributes-groups >` to
931931learn how to do that.
932932
@@ -1160,11 +1160,11 @@ context option::
11601160Recursive Denormalization and Type Safety
11611161-----------------------------------------
11621162
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
11641164complex types (objects). The type of the class' property will be guessed using the provided
11651165extractor and used to recursively denormalize the inner data.
11661166
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.
11681168When using the component standalone, an implementation of :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyTypeExtractorInterface `,
11691169(usually an instance of :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyInfoExtractor `) must be passed as the 4th
11701170parameter of the ``ObjectNormalizer ``::
@@ -1239,9 +1239,14 @@ between the possible objects. In practice, when using the Serializer component,
12391239pass a :class: `Symfony\\ Component\\ Serializer\\ Mapping\\ ClassDiscriminatorResolverInterface `
12401240implementation to the :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `.
12411241
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::
12451250
12461251 // ...
12471252 use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -1253,25 +1258,15 @@ This example shows how to serialize and deserialize those objects::
12531258 $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
12541259
12551260 $discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);
1256- $discriminator->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
1257- 'github' => GitHubCodeRepository::class,
1258- 'bitbucket' => BitBucketCodeRepository::class,
1259- ]));
12601261
12611262 $serializer = new Serializer(
12621263 array(new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator)),
12631264 array('json' => new JsonEncoder())
12641265 );
12651266
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:
12751270
12761271.. configuration-block ::
12771272
@@ -1317,6 +1312,14 @@ can use this simpler configuration:
13171312 </class >
13181313 </serializer >
13191314
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+
13201323Performance
13211324-----------
13221325
@@ -1339,7 +1342,7 @@ and return ``true`` when
13391342is called.
13401343
13411344 .. note ::
1342-
1345+
13431346 All built-in :ref: `normalizers and denormalizers <component-serializer-normalizers >`
13441347 as well the ones included in `API Platform `_ natively implement this interface.
13451348
@@ -1359,7 +1362,7 @@ Learn more
13591362
13601363.. seealso ::
13611364
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
13631366 library, `JMS serializer `_ (versions before ``v1.12.0 `` were released under
13641367 the Apache license, so incompatible with GPLv2 projects).
13651368
0 commit comments