0

I'm trying to deserialize a json string to an entity to be used with doctrine. For some reason some of my properties are being ignored.

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]

I'm passing this json to the serializer:

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

and calling it like so:

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

For some reason the only properties being respected are board, file, name and notes. All of the other fields are being ignored for some reason. Any input would be appreciated.

matt
  • 320
  • 4
  • 15

1 Answers1

1

@SerializedName

This annotation can be defined on a property to define the serialized name for a property. If this is not defined, the property will be translated from camel-case to a lower-cased underscored name, e.g. camelCase -> camel_case.

You can read here for more information: http://jmsyst.com/libs/serializer/master/reference/annotations

l13
  • 529
  • 3
  • 11
  • I actualIy figured it out earlier this morning. I able to resolve the issue adding the following to my parameters.yml file which would set the naming strategy for all fields and not need to set the individual serialized property name. `jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy` I'm accepting your answer on the account that it will also remedy the issue i was having. Thank you very much for taking the time to answer :) – matt Mar 04 '18 at 18:28