9

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gustek
  • 3,680
  • 2
  • 22
  • 36
  • 1
    i would recommend you configuring your serialization and doctrine mappings in seperate files ( xml / yml ) instead of using annotations because it allows easier extending and bundle inheritance but its a matter of personal preference... just my few cents – Nicolai Fröhlich May 23 '13 at 06:57
  • Thanks, I will consider. It is really nice Symfony offers few formats for writing configuration and stuff but it is double edged sword. – Gustek May 23 '13 at 07:05

2 Answers2

9

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
Alexey B.
  • 11,965
  • 2
  • 49
  • 73
  • I have set serialize_null in config but had no effect. – Gustek May 23 '13 at 20:42
  • what version of jms/serializer-bundle you are using? Try to serialize entity with pure serializer as in the first example – Alexey B. May 24 '13 at 04:54
  • I have dev-master in composer updated just now nothing changed. If I use serilizer directly as in Your first example it works, though it gives null as a value not empty strings as I would prefer. Thanks for help, I made it accept in client as it is now and continue with this. – Gustek May 24 '13 at 17:04
  • Glad I could help, so you can mark it as answer. You can find view_handler and see why not apply the settings in FOSRestBundle. – Alexey B. May 24 '13 at 17:36
  • It does not work with null values, for XML it uses xsi:nil but for JSON it just excludes null how do I include null values, keys basically? – Geshan May 28 '13 at 05:40
  • Need more info to help you. This setting usually work for json. Try it with pure serializer. – Alexey B. May 28 '13 at 06:08
  • @AlexeyB. Is there any way, I can serialize null value to empty string ? I am curious as our mobile developers insist to do that for some exception generating in their end. – Jeet Jul 22 '15 at 15:45
  • @AlexeyB. is it possible without fos_rest to change the context globally that I don't have to specify the context for each call? – lony Oct 01 '15 at 09:11
7

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
Dmytro
  • 5,443
  • 2
  • 52
  • 50