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
+75Lines changed: 75 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -981,6 +981,81 @@ 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
985
+
-------------------------------------------
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".
990
+
991
+
The discrimator is the field (in the serialized string) you are going to use to differentiate the
992
+
different objects.
993
+
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
+
997
+
$discriminatorResolver = new ClassDiscriminatorResolver();
998
+
$discriminatorResolver->addClassMapping(CodeRepository::class, new ClassDiscriminatorMapping('type', [
999
+
'github' => GitHubCodeRepository::class,
1000
+
'bitbucket' => BitBucketCodeRepository::class,
1001
+
]));
1002
+
1003
+
$serializer = new Serializer(array(new ObjectNormalizer(null, null, null, null, $discriminatorResolver)), array('json' => new JsonEncoder()));
0 commit comments