JMS Serializer does not disclose one property

I made a RESTful application with Symfony and FOSRestBundle. FOSRestBundle uses the JMS serializer to serialize data to json format. Everything works for me with one small problem.

This is my Entity class.

/** * Post * * @ORM\Table() * @ORM\Entity(repositoryClass="Tomalo\AdminBundle\Entity\PostRepository") * @ExclusionPolicy("none") */ class Post { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="content", type="text") * @Assert\NotBlank() */ private $content; /** * @var float * * @ORM\Column(name="lat", type="float") * @Assert\NotBlank() */ private $lat; /** * @var float * * @ORM\Column(name="lon", type="float") * @Assert\NotBlank() */ private $lon; /** * @var \DateTime * * @ORM\Column(name="date", type="datetime") */ private $date; /** * @var string * * @ORM\Column(name="sign", type="string", length=50, nullable=true) * @Expose */ private $sign; /** * @var integer * * @ORM\Column(name="status", type="integer") */ private $status=0; /** * @var integer * * @ORM\Column(name="points", type="integer") */ private $points=0; /** * @var string * * @ORM\Column(name="uuid", type="string", length=43) * @Assert\NotBlank() * @Exclude */ private $uuid; private $owner; //get/set method continue 

and this is the JSON I get:

 { "id": 5, "content": "zxcvzxcvzxc", "lat": 37.422005, "lon": -122.084095, "date": "2013-05-20T05:06:57+0100", "status": 0, "points": 0, "owner": 0 } 

In my essence, $ uuid is the only property that has the @Exclude annotation and is not there as expected, but the $ sign property is also missing. As you can see, I put the @Expose annotation in the $ sign, but nothing has changed. I tried using @ExclusionPolicy ("all") and expose everything except uuid, but I get

 Warning: json_encode(): recursion detected in E:\workspace\htdocs\tomalo\vendor\jms\serializer\src\JMS\Serializer\JsonSerializationVisitor.php line 29 

I found some information since this is a PHP error

Any ideas what is wrong and how to fix it?

+9
json symfony jmsserializerbundle fosrestbundle
source share
2 answers

You can serialize zeros as empty strings. I guess this will help you a little

 $context = new SerializationContext(); $context->setSerializeNull(true); $objectData = $serializer->serialize($object, 'json', $context); 

For FOSRestBundle you can define it in settings

 fos_rest: view: serialize_null: true 
+9
source share

forgetbas . The solution for FOSRestBundle did not work for me. I found a solution here https://github.com/FriendsOfSymfony/FOSRestBundle/pull/480

Use serializer in your configuration, not view :

 fos_rest: serializer: serialize_null: true 
+7
source share

All Articles