简体   繁体   English

JMS Serializer不会公开一个属性

[英]JMS Serializer doesn't expose one property

I making a RESTful app with Symfony and FOSRestBundle. 我用Symfony和FOSRestBundle创建了一个RESTful应用程序。 FOSRestBundle uses JMS Serializer to serialize data to json format. FOSRestBundle使用JMS Serializer将数据序列化为json格式。 I have everything working with one little issue. 我有一切都在处理一个小问题。

This is my Entity class 这是我的Entity类

/**
 * 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: 这是我得到的json:

{
           "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. 在我的实体中,$ uuid是唯一具有@Exclude注释的属性,并且没有按预期存在,但也缺少$ sign属性。 As You see I put @Expose annotation to $sign but changed nothing. 如你所见,我把@Expose注释放到$ sign但没有改变。 I tried using @ExclusionPolicy("all") and expose all except for uuid but I'm getting 我尝试使用@ExclusionPolicy(“all”)并公开除了uuid以外的所有内容但我得到了

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 我发现了一些信息,因为它是一些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 对于FOSRestBundle,您可以在设置中定义它

fos_rest:
    view:
        serialize_null: true

forgottenbas 'es solution for FOSRestBundle didn't work for me. 忘记FOSRestBundle解决方案对我不起作用。 I have found a solution here https://github.com/FriendsOfSymfony/FOSRestBundle/pull/480 我在这里找到了一个解决方案https://github.com/FriendsOfSymfony/FOSRestBundle/pull/480

Use serializer section in your config, not view : 在配置中使用serializer部分,而不是view

fos_rest:
    serializer:
        serialize_null: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM