JMS Serialize @VirtualProperty not working

I am trying to add a method for serialization, but JMS Serialize does not display this field.

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 examples of using VirtualProperty.

Is the syntax correct? What's wrong?

Thanks.

+7
source share
1 answer

I noticed a problem. Before the object, "$ ad" was created. My fault. Virtual property is functioning properly.

+1
source

All Articles