I making a RESTful app with Symfony and FOSRestBundle. FOSRestBundle uses JMS Serializer to serialize data to json format. I have everything working with one little issue.
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 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 entity $uuid is the only property haveing @Exclude annotation and is not there as expected but there is $sign property missing as well. As You see I put @Expose annotation to $sign but changed nothing. I tried using @ExclusionPolicy("all") and expose all except for uuid but I'm getting
Warning: json_encode(): recursion detected in E:\workspace\htdocs\tomalo\vendor\jms\serializer\src\JMS\Serializer\JsonSerializationVisitor.php line 29
I found some information as it is some php bug
any idea what is wrong and how to fix it?
You can serialize nulls as empty strings. Guess it help you a bit
$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
forgottenbas'es solution for FOSRestBundle
didn't work for me. I have found a solution here https://github.com/FriendsOfSymfony/FOSRestBundle/pull/480
Use serializer
section in your config, not view
:
fos_rest:
serializer:
serialize_null: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With