具有SerializedName属性的方法在使用Symfony序列化器序列化对象时被忽略。

huangapple go评论55阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月5日 16:48:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定