Skip to content

Commit ee8fa8e

Browse files
committed
Merge branch 'pull/10254' into 4.1
2 parents 088e659 + 9bf3b47 commit ee8fa8e

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

components/serializer.rst

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ Converting Property Names when Serializing and Deserializing
424424
Sometimes serialized attributes must be named differently than properties
425425
or 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
428428
names to serialized names: The Name Converter System.
429429

430430
Given you have the following object::
@@ -582,7 +582,7 @@ There are several types of normalizers available:
582582
``firstName``).
583583

584584
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.
586586

587587
:class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer`
588588
This normalizer reads the content of the class by calling the "getters"
@@ -672,8 +672,8 @@ The Serializer component provides several built-in encoders:
672672
:class:`Symfony\\Component\\Serializer\\Encoder\\CsvEncoder`
673673
This encoder encodes and decodes data in CSV_.
674674

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.
677677

678678
The ``JsonEncoder``
679679
~~~~~~~~~~~~~~~~~~~
@@ -908,8 +908,8 @@ Here, we set it to 2 for the ``$child`` property:
908908
</serializer>
909909
910910
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
913913
:ref:`the groups documentation <component-serializer-attributes-groups>` to
914914
learn how to do that.
915915

@@ -1138,11 +1138,11 @@ context option::
11381138
Recursive Denormalization and Type Safety
11391139
-----------------------------------------
11401140

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
11421142
complex types (objects). The type of the class' property will be guessed using the provided
11431143
extractor and used to recursively denormalize the inner data.
11441144

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.
11461146
When using the component standalone, an implementation of :class:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface`,
11471147
(usually an instance of :class:`Symfony\\Component\\PropertyInfo\\PropertyInfoExtractor`) must be passed as the 4th
11481148
parameter of the ``ObjectNormalizer``::
@@ -1217,9 +1217,14 @@ between the possible objects. In practice, when using the Serializer component,
12171217
pass a :class:`Symfony\\Component\\Serializer\\Mapping\\ClassDiscriminatorResolverInterface`
12181218
implementation to the :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`.
12191219

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::
12231228

12241229
// ...
12251230
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -1231,25 +1236,15 @@ This example shows how to serialize and deserialize those objects::
12311236
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
12321237

12331238
$discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);
1234-
$discriminator->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
1235-
'github' => GitHubCodeRepository::class,
1236-
'bitbucket' => BitBucketCodeRepository::class,
1237-
]));
12381239

12391240
$serializer = new Serializer(
12401241
array(new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator)),
12411242
array('json' => new JsonEncoder())
12421243
);
12431244

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:
12531248

12541249
.. configuration-block::
12551250

@@ -1295,6 +1290,14 @@ can use this simpler configuration:
12951290
</class>
12961291
</serializer>
12971292
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+
12981301
Performance
12991302
-----------
13001303

@@ -1317,7 +1320,7 @@ and return ``true`` when
13171320
is called.
13181321

13191322
.. note::
1320-
1323+
13211324
All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
13221325
as well the ones included in `API Platform`_ natively implement this interface.
13231326

@@ -1337,7 +1340,7 @@ Learn more
13371340

13381341
.. seealso::
13391342

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
13411344
library, `JMS serializer`_ (versions before ``v1.12.0`` were released under
13421345
the Apache license, so incompatible with GPLv2 projects).
13431346

0 commit comments

Comments
 (0)