简体   繁体   English

JMS序列化@VirtualProperty不起作用

[英]JMS Serialize @VirtualProperty doesn't work

I'm trying to add a method to serialize, but JMS Serialize does not show the field. 我正在尝试添加一种序列化方法,但是JMS序列化不显示该字段。

use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\VirtualProperty;
use JMS\Serializer\Annotation\SerializedName;

class Ads
{   
    /**
     * @VirtualProperty
     * @Type("string")
     * @SerializedName("Foo")
     * @Groups({"manage"})
     */
    public function foo(){
        $foo = 'foo';
        return $foo;
    }
    ...
}

And then: 然后:

use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;

...

$context = new SerializationContext();
$context->setGroups(array('manage'));

$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$jsonContent = $serializer->serialize($ad, 'json', $context);

I have not seen any examples of how to use VirtualProperty. 我还没有看到任何有关如何使用VirtualProperty的示例。

Is the syntax correct? 语法正确吗? What is wrong? 怎么了?

Thank you. 谢谢。

I've noticed the problem. 我注意到了问题。 There was created before the object "$ ad". 在“ $ ad”对象之前创建了一个。 My fault. 我的错。 Virtual property functioning properly. 虚拟属性运行正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM