Skip to content

[Serializer] Selecting a particular Normalizer to use? #18066

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
mpdude opened this issue Mar 16, 2023 · 5 comments
Closed

[Serializer] Selecting a particular Normalizer to use? #18066

mpdude opened this issue Mar 16, 2023 · 5 comments

Comments

@mpdude
Copy link
Contributor

mpdude commented Mar 16, 2023

Hi there,

the documentation lists many different normalizers that are supported out of the box and – to my understanding – active by default when using the full stack framework.

But one question I am facing right now seems to be unanswered: Can I (how?) choose a particular normalizer for a given class?

Say, I have a MyBlogPost class that is included in data to be serialized, and I want the PropertyNormalizer to be used for it, not the ObjectNormalizer.

What is the recommended way to do this? I can try to write the docs, but need initial guidance.

Re. #17912 – are contributions for this section still pending the revamp?

@mpdude
Copy link
Contributor Author

mpdude commented Mar 23, 2023

@dunglas Maybe you can confirm if this 👇🏻 is the way to go?

<?php

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;

class MyBlogPostNormalizer implements NormalizerInterface
{
    public function __construct(private readonly PropertyNormalizer $propertyNormalizer)
    {
    }

    public function normalize(mixed $object, string $format = null, array $context = [])
    {
        return $this->propertyNormalizer->normalize($object, $format, $context);
    }

    public function supportsNormalization(mixed $data, string $format = null): bool
    {
        return $data instanceof DateRange;
    }
}

@alexandre-daubois
Copy link
Member

I think this is something that should be addressed to discussions. However, what I can tell is this is not the solution because injecting ObjectNormalizer and PropertyNormalizer is deprecated since 6.2. You may instantiate PropertyNormalizer manually though (but you cannot extend it as it was made final in 6.3)

@carsonbot
Copy link
Collaborator

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link
Collaborator

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

@carsonbot
Copy link
Collaborator

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants