[英]Symfony 5.4 - SerializedName attribute/annotation not working
[英]Symfony - set @SerializedName() dynamically
有没有办法访问序列化器元数据,类似于学说映射/类元数据? Doctrine 允许您通过来自EventSubscriber
的loadClassMetadata($args)
编辑映射信息。
我想编辑我的@SerializedName("foo")
注释预序列化。
对于上下文,属性(以及因此实体)位于 symfony 包中,需要根据主要应用程序设置进行重命名。
我使用的是 symfonys 序列化程序,而不是 JMS。
是否可以在注释中调用函数,如下所示: @SerializedName(getName())
? 那可以解决我的问题。
提议的解决方案并没有让我走得太远,尽管我找到了一些对我有用的东西。
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()));
}
在我的 function 中,它在实际序列化之前调用:
$classMetadata = $this->classMetadataFactory->getMetadataFor('fully qualified classname');
$attributesMetadata = $classMetadata->getAttributesMetadata();
foreach ($attributesMetadata as $attr) {
$attr->addGroup(['exampleGroup']);
$attr->setSerializedName('examplename');
}
这样我就可以动态地更改元数据。
为此class创建自定义规范器并根据需要更改它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.