You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: components/serializer.rst
+20-14Lines changed: 20 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -981,35 +981,42 @@ will be thrown. The type enforcement of the properties can be disabled by settin
981
981
the serializer context option ``ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT``
982
982
to ``true``.
983
983
984
-
Serializing interfaces and abstract classes
984
+
Serializing Interfaces and Abstract Classes
985
985
-------------------------------------------
986
986
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"*.
990
990
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`.
993
995
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::
996
999
997
-
$discriminatorResolver = new ClassDiscriminatorResolver();
998
-
$discriminatorResolver->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
1000
+
$discriminator = new ClassDiscriminatorResolver();
1001
+
$discriminator->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
999
1002
'github' => GitHubCodeRepository::class,
1000
1003
'bitbucket' => BitBucketCodeRepository::class,
1001
1004
]));
1002
1005
1003
-
$serializer = new Serializer(array(new ObjectNormalizer(null, null, null, null, $discriminatorResolver)), array('json' => new JsonEncoder()));
0 commit comments