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.
Advertisement
Answer
The proposed solution didn’t get me far, though I found something that worked for me.
use SymfonyComponentSerializerMappingFactoryClassMetadataFactory; use SymfonyComponentSerializerMappingFactoryClassMetadataFactoryInterface; use SymfonyComponentSerializerMappingLoaderAnnotationLoader; use DoctrineCommonAnnotationsAnnotationReader; 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.