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

Skip to content

[Serializer] Custom normalizer update #18603

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

Closed
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
11 changes: 7 additions & 4 deletions serializer/custom_normalizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ Creating a New Normalizer
Imagine you want add, modify, or remove some properties during the serialization
process. For that you'll have to create your own normalizer. But it's usually
preferable to let Symfony normalize the object, then hook into the normalization
to customize the normalized data. To do that, leverage the ``ObjectNormalizer``::
to customize the normalized data. To do that, leverage the
``NormalizerAwareInterface`` and the ``NormalizerAwareTrait``. This will give
you access to a ``$normalizer`` property which takes care of most of the
normalization process::

// src/Serializer/TopicNormalizer.php
namespace App\Serializer;

use App\Entity\Topic;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

class TopicNormalizer implements NormalizerInterface
class TopicNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

public function __construct(
private UrlGeneratorInterface $router,
private ObjectNormalizer $normalizer,
) {
}

Expand Down