英文:
Method with a SerializedName attribute getting ignored when object is serialized with symfony serializer
问题
我有一个Symfony序列化器,我是这样初始化的:
$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()]
);
还有一个在我的对象上的方法,我想要以特定的名称进行序列化(getRrule
是一个已存在的方法,所以我不能只是重命名它):
#[Serializer\Groups(['prestation:default'])]
#[Serializer\SerializedName('rrule')]
public function getRruleRfcString(): ?string
{
return $this->rrule ? $this->rrule->rfcString() : null;
}
但是当序列化时,我得到了:"rrule_rfc_string" => null
而不是:"rrule" => null
,而且序列化组也正常工作,所以我不知道为什么只有两个属性中的一个起作用...
英文:
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
得分: 1
我刚刚解决了它:
$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()]
);
只需添加一个 MetadataAwareNameConverter 并将 CamelCaseToSnakeCaseNameConverter 作为回退名称转换器。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论