2

is there a way to access serializer metadata, similar to doctrines mapping/classmetadata? Doctrine allows you to edit mapping information via loadClassMetadata($args) from an EventSubscriber.

I'd like to edit my @SerializedName("foo") annotation pre serialization.

For context, the attributes (and therefore Entites) are inside a symfony bundle and need to be renamed based on the main applications settings.

I'm using symfonys serializer, not JMS.

Is it possible to call functions inside annotaions, which would look like this: @SerializedName(getName())? That could solve my problem.

2 Answers 2

1

The proposed solution didn't get me far, though I found something that worked for me.

use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Doctrine\Common\Annotations\AnnotationReader;


public function __construct(ClassMetadataFactoryInterface $classMetadataFactory){
   $this->classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
}

in my function which is called before the actual serialization:

$classMetadata = $this->classMetadataFactory->getMetadataFor('fully qualified classname');
$attributesMetadata = $classMetadata->getAttributesMetadata();

foreach ($attributesMetadata as $attr) {
    $attr->addGroup(['exampleGroup']);
    $attr->setSerializedName('examplename');
}

That way I can change the metadata dynamically.

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

Comments

0

Create custom normalizer for this class and change it as you wish https://symfony.com/doc/current/serializer/custom_normalizer.html

1 Comment

thanks for answering, I will take a look at this tomorrow and see how far this gets me.

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.