3

I have a symfony serializer that i initialize this way :

$this->serializer = new Serializer(
    [
        new ArrayDenormalizer(),
        new UidNormalizer(),
        new BackedEnumNormalizer(),
        new DateTimeNormalizer(),
        new ObjectNormalizer(
            classMetadataFactory: new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())),
            nameConverter: new CamelCaseToSnakeCaseNameConverter(),
            propertyTypeExtractor: new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()])
        ),
    ],
    [new JsonEncoder()]
);

and a method on my object, i want to serialize with a specific name (getRrule being an existant method so i can't just rename it)

#[Serializer\Groups(['prestation:default'])]
#[Serializer\SerializedName('rrule')]
public function getRruleRfcString(): ?string
{
    return $this->rrule?->rfcString();
}

but when serialized i get : "rrule_rfc_string" => null

instead of : "rrule" => null

also the serialization group is working fine so i dont know why only one attribute out of two are working...

1 Answer 1

5

I just resolved it:

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

$this->serializer = new Serializer(
    [
        new ArrayDenormalizer(),
        new UidNormalizer(),
        new BackedEnumNormalizer(),
        new DateTimeNormalizer(),
        new ObjectNormalizer(
            classMetadataFactory: new ClassMetadataFactory(
                new AnnotationLoader(new AnnotationReader())
            ),
            nameConverter: new MetadataAwareNameConverter(
                $classMetadataFactory,
                new CamelCaseToSnakeCaseNameConverter()
            ),
            propertyTypeExtractor: new PropertyInfoExtractor(
                [],
                [
                    new PhpDocExtractor(),
                    new ReflectionExtractor()
                ]
            )
        ),
    ],
    [new JsonEncoder()]
);

Just needed to add a MetadataAwareNameConverter plus put the CamelCaseToSnakeCaseNameConverter as fallback name converter

Sign up to request clarification or add additional context in comments.

2 Comments

Unless you are using the serializer as a standalone component please use it as described in symfony.com/doc/current/…. Symfony as a framework comes with a preconfigured out of the box serializer service you can inject which has all these things set up which avoids a lot of hassle.
Thank you this answer was a lifesaver for some reason the autowired version of the Serializer was without the MetadataAwareNameConverter and the docs are somewhat limited on how to configure how the service is built. This got me far enough to test and get unstuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.