Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ac831b0

Browse filesBrowse files
Minor fixes and some tweaks
1 parent 61b9c53 commit ac831b0
Copy full SHA for ac831b0

File tree

1 file changed

+20
-14
lines changed
Filter options

1 file changed

+20
-14
lines changed

‎components/serializer.rst

Copy file name to clipboardExpand all lines: components/serializer.rst
+20-14Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -981,35 +981,42 @@ will be thrown. The type enforcement of the properties can be disabled by settin
981981
the serializer context option ``ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT``
982982
to ``true``.
983983

984-
Serializing interfaces and abstract classes
984+
Serializing Interfaces and Abstract Classes
985985
-------------------------------------------
986986

987-
When dealing with objects that are fairly similar or share properties, you'd usually use
988-
intefaces or abstract classes. The Serializer component allows you to serialize and deserialize
989-
these objects using a "discrimator class mapping".
987+
When dealing with objects that are fairly similar or share properties, you may
988+
use interfaces or abstract classes. The Serializer component allows you to
989+
serialize and deserialize these objects using a *"discrimator class mapping"*.
990990

991-
The discrimator is the field (in the serialized string) you are going to use to differentiate the
992-
different objects.
991+
The discriminator is the field (in the serialized string) used to differentiate
992+
between the possible objects. In practice, when using the Serializer component,
993+
pass the :class:`Symfony\\Component\\Serializer\\Mapping\\ClassDiscriminatorResolver`
994+
to the :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`.
993995

994-
When using the Serializer component, you need to give the :class:`Symfony\\Component\\Serializer\\Mapping\\ClassDiscriminatorResolver` to the :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`,
995-
like in the following example::
996+
Consider an application that defines an abstract ``CodeRepository`` class
997+
extended by ``GitHubCodeRepository`` and ``BitBucketCodeRepository`` classes.
998+
This example shows how to serialize and deserialize those objects::
996999

997-
$discriminatorResolver = new ClassDiscriminatorResolver();
998-
$discriminatorResolver->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
1000+
$discriminator = new ClassDiscriminatorResolver();
1001+
$discriminator->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
9991002
'github' => GitHubCodeRepository::class,
10001003
'bitbucket' => BitBucketCodeRepository::class,
10011004
]));
10021005

1003-
$serializer = new Serializer(array(new ObjectNormalizer(null, null, null, null, $discriminatorResolver)), array('json' => new JsonEncoder()));
1006+
$serializer = new Serializer(
1007+
array(new ObjectNormalizer(null, null, null, null, $discriminator)),
1008+
array('json' => new JsonEncoder())
1009+
);
10041010

10051011
$serialized = $serializer->serialize(new GitHubCodeRepository());
10061012
// {"type": "github"}
10071013

10081014
$repository = $serializer->unserialize($serialized, CodeRepository::class, 'json');
10091015
// instanceof GitHubCodeRepository
10101016

1011-
If you have enabled the class metadata factory as described in [Attributes Groups](#attributes-groups), you can
1012-
simply use the following configuration:
1017+
If the class metadata factory is enabled as explained in the
1018+
:ref:`Attributes Groups section <component-serializer-attributes-groups>`, you
1019+
can use this simpler configuration:
10131020

10141021
.. configuration-block::
10151022

@@ -1055,7 +1062,6 @@ simply use the following configuration:
10551062
</class>
10561063
</serializer>
10571064
1058-
10591065
Learn more
10601066
----------
10611067

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.