Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Fix the interface/abstract classes documentation #9873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1168,8 +1168,8 @@ serialize and deserialize these objects using a *"discriminator class mapping"*.

The discriminator is the field (in the serialized string) used to differentiate
between the possible objects. In practice, when using the Serializer component,
pass the :class:`Symfony\\Component\\Serializer\\Mapping\\ClassDiscriminatorResolver`
to the :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`.
pass a :class:`Symfony\\Component\\Serializer\\Mapping\\ClassDiscriminatorResolverInterface`
implementation to the :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`.

Consider an application that defines an abstract ``CodeRepository`` class
extended by ``GitHubCodeRepository`` and ``BitBucketCodeRepository`` classes.
Expand All @@ -1178,18 +1178,20 @@ This example shows how to serialize and deserialize those objects::
// ...
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolver;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$discriminator = new ClassDiscriminatorResolver();
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

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

$serializer = new Serializer(
array(new ObjectNormalizer(null, null, null, null, $discriminator)),
array(new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator)),
array('json' => new JsonEncoder())
);

Expand Down