JMS Serializer doesn't expose one property
Asked Answered
A

2

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?

Alongshore answered 23/5, 2013 at 6:48 Comment(2)
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 centsJohm
Thanks, I will consider. It is really nice Symfony offers few formats for writing configuration and stuff but it is double edged sword.Alongshore
S
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
Sloat answered 23/5, 2013 at 8:33 Comment(8)
I have set serialize_null in config but had no effect.Alongshore
what version of jms/serializer-bundle you are using? Try to serialize entity with pure serializer as in the first exampleSloat
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.Alongshore
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.Sloat
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?Flannel
Need more info to help you. This setting usually work for json. Try it with pure serializer.Sloat
@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.Fatma
@AlexeyB. is it possible without fos_rest to change the context globally that I don't have to specify the context for each call?Quintuplicate
W
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
Wynd answered 2/7, 2014 at 8:56 Comment(1)
Thank you, probably saved me hours of debugging! :-)Gendarmerie

© 2022 - 2024 — McMap. All rights reserved.