JSMSerialier игнорирует некоторые свойства

Я пытаюсь десериализовать строку json в объект, который будет использоваться с доктриной. По какой-то причине некоторые из моих свойств игнорируются.

AppBundle\Entity\BoardSong.php

<?php

namespace AppBundle\Entity;

/**
 * BoardSound
 */
class BoardSound
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var Board
     */
    private $board;

    /**
     * @var File
     */
    private $file;

    /**
     * @var string
     */
    private $name;

    /**
     * @var int
     */
    private $startTime;

    /**
     * @var int
     */
    private $endTime;

    /**
     * @var string
     */
    private $backgroundColor;

    /**
     * @var string
     */
    private $borderColor;

    /**
     * @var string
     */
    private $textColor;

    /**
     * @var int
     */
    private $displayOrder;

    /**
     * @var string
     */
    private $note;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     * @return BoardSound
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return Board
     */
    public function getBoard()
    {
        return $this->board;
    }

    /**
     * @param Board $board
     * @return BoardSound
     */
    public function setBoard($board)
    {
        $this->board = $board;
        return $this;
    }

    /**
     * @return File
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * @param File $file
     * @return BoardSound
     */
    public function setFile($file)
    {
        $this->file = $file;
        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     * @return BoardSound
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return int
     */
    public function getStartTime()
    {
        return $this->startTime;
    }

    /**
     * @param int $startTime
     * @return BoardSound
     */
    public function setStartTime($startTime)
    {
        $this->startTime = $startTime;
        return $this;
    }

    /**
     * @return int
     */
    public function getEndTime()
    {
        return $this->endTime;
    }

    /**
     * @param int $endTime
     * @return BoardSound
     */
    public function setEndTime($endTime)
    {
        $this->endTime = $endTime;
        return $this;
    }

    /**
     * @return string
     */
    public function getBackgroundColor()
    {
        return $this->backgroundColor;
    }

    /**
     * @param string $backgroundColor
     * @return BoardSound
     */
    public function setBackgroundColor($backgroundColor)
    {
        $this->backgroundColor = $backgroundColor;
        return $this;
    }

    /**
     * @return string
     */
    public function getBorderColor()
    {
        return $this->borderColor;
    }

    /**
     * @param string $borderColor
     * @return BoardSound
     */
    public function setBorderColor($borderColor)
    {
        $this->borderColor = $borderColor;
        return $this;
    }

    /**
     * @return string
     */
    public function getTextColor()
    {
        return $this->textColor;
    }

    /**
     * @param string $textColor
     * @return BoardSound
     */
    public function setTextColor($textColor)
    {
        $this->textColor = $textColor;
        return $this;
    }

    /**
     * @return int
     */
    public function getDisplayOrder()
    {
        return $this->displayOrder;
    }

    /**
     * @param int $displayOrder
     * @return BoardSound
     */
    public function setDisplayOrder($displayOrder)
    {
        $this->displayOrder = $displayOrder;
        return $this;
    }

    /**
     * @return string
     */
    public function getNote()
    {
        return $this->note;
    }

    /**
     * @param string $note
     * @return BoardSound
     */
    public function setNote($note)
    {
        $this->note = $note;
        return $this;
    }
}

AppBundle\Resources\config\serializer\Entity.BoardSound.yml

AppBundle\Entity\BoardSound:
  properties:
    id:
      type: integer
      groups: [rpc]
    name:
      type: string
      groups: [rpc]
    file:
      type: AppBundle\Entity\File
      groups: [rpc]
    board:
      type: AppBundle\Entity\Board
      groups: [rpc]
    note:
      type: string
      groups: [rpc]
    backgroundColor:
      type: string
      groups: [rpc]
    borderColor:
      type: string
      groups: [rpc]
    textColor:
      type: string
      groups: [rpc]

Я передаю этот json сериализатору:

{
    "file": {
        "id": "1"
    },
    "name": "Some Name",
    "note": "asdfasdfasdfasdf",
    "backgroundColor": "#ffffff",
    "boarderColor": "#000000",
    "textColor": "#000000",
    "board": {
        "id": 1
    }
}

и называя это так:

$serializer->deserialize($jsonString,'AppBundle\Entity\BoardSound', 'json');

По какой-то причине учитываются только свойства доски, файла, имени и заметок. Все остальные поля почему-то игнорируются. Мы будем признательны за любой вклад.


person matt    schedule 04.03.2018    source источник


Ответы (1)


@SerializedName

Эту аннотацию можно определить для свойства, чтобы определить сериализованное имя свойства. Если это не определено, свойство будет переведено из верблюжьего регистра в имя с подчеркиванием в нижнем регистре, например. верблюжий регистр -> верблюжий_кейс.

Вы можете прочитать здесь для получения дополнительной информации: http://jmsyst.com/libs/serializer/master/reference/annotations

person l13    schedule 04.03.2018
comment
На самом деле я понял это сегодня утром. Я смог решить эту проблему, добавив следующее в мой файл parameters.yml, который установил бы стратегию именования для всех полей и не нуждался бы в установке отдельного сериализованного имени свойства. jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy Я принимаю ваш ответ, поскольку он также решит проблему, с которой я столкнулся. Большое спасибо, что нашли время ответить :) - person matt; 04.03.2018