Skip to content

Commit 61b9c53

Browse files
committed
Add documentation about the (de)serialization of interfaces and abstract classes
1 parent 755f231 commit 61b9c53

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

components/serializer.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,81 @@ 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
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()));
1004+
1005+
$serialized = $serializer->serialize(new GitHubCodeRepository());
1006+
// {"type": "github"}
1007+
1008+
$repository = $serializer->unserialize($serialized, CodeRepository::class, 'json');
1009+
// instanceof GitHubCodeRepository
1010+
1011+
If you have enabled the class metadata factory as described in [Attributes Groups](#attributes-groups), you can
1012+
simply use the following configuration:
1013+
1014+
.. configuration-block::
1015+
1016+
.. code-block:: php-annotations
1017+
1018+
namespace App;
1019+
1020+
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
1021+
1022+
/**
1023+
* @DiscriminatorMap(typeProperty="type", mapping={
1024+
* "github"="App\GitHubCodeRepository",
1025+
* "bitbucket"="App\BitBucketCodeRepository"
1026+
* })
1027+
*/
1028+
interface CodeRepository
1029+
{
1030+
// ...
1031+
}
1032+
1033+
.. code-block:: yaml
1034+
1035+
App\CodeRepository:
1036+
discriminator_map:
1037+
type_property: type
1038+
mapping:
1039+
github: 'App\GitHubCodeRepository'
1040+
bitbucket: 'App\BitBucketCodeRepository'
1041+
1042+
.. code-block:: xml
1043+
1044+
<?xml version="1.0" ?>
1045+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
1046+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1047+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
1048+
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
1049+
>
1050+
<class name="App\CodeRepository">
1051+
<discriminator-map type-property="type">
1052+
<mapping type="github" class="App\GitHubCodeRepository" />
1053+
<mapping type="bitbucket" class="App\BitBucketCodeRepository" />
1054+
</discriminator-map>
1055+
</class>
1056+
</serializer>
1057+
1058+
9841059
Learn more
9851060
----------
9861061

0 commit comments

Comments
 (0)