Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2: JMSSerializerBundle changes the attribute name from "className" to "class_name"

I'm using the JMSSerializerBundle to serialize my entity. but I have the following problem: the attribute name is "className" but in my Json object I get a "class_name".

this is my entity:

/**
 * Events
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Events
{
 /**
  * @var integer
  *
  * @ORM\Column(name="id", type="integer")
  * @ORM\Id
  * @ORM\GeneratedValue(strategy="AUTO")
  */
 private $id;

 ...

 /**
  * @var string
  *
  * @ORM\Column(name="className", type="string", length=255)
  */
 private $className;

 /**
  * Set className
  *
  * @param string $className
  * @return Events
  */
 public function setClassName($className)
 {
     $this->className = $className;

     return $this;
 }

 /**
  * Get className
  *
  * @return string 
  */
 public function getClassName()
 {
     return $this->className;
 }
 ...
}

this is my controller

  class myController extends Controller{

      public function loadAction($action){
        $request=$this->get('request');
        if($request->isXmlHttpRequest())
        {
         switch( $action ) {

            case 'load':
                 $resultat=$this->getDoctrine()->getManager()->getRepository('ECMUserBundle:Events')
                    ->findAll();
                $serializer = $this->get('jms_serializer');
                $resultat=$serializer->serialize($resultat, 'json');
                echo $resultat;
                exit();
                break;
            ...

and this my Json

 [{"id":90,"title":"holliday","start":"2014-03-25T01:00:00+0000","end":"2014-03-25T01:00:00+0000","class_name":"label-orange","allday":"true"}]

is this the logical behaviors?

like image 425
Akram Avatar asked Mar 29 '14 23:03

Akram


3 Answers

As @mike said, you can use @SerializedName annotation to change serialized property name to arbitrary string.

Also, if you want to change naming strategy on application level. You can use the following workaround:

config.yml

parameters:
    jms_serializer.serialized_name_annotation_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy

Also, check this issue.

like image 59
Slava Fomin II Avatar answered Dec 02 '22 20:12

Slava Fomin II


Check the documentation for the @SerializedName annotation:

http://jmsyst.com/libs/serializer/master/reference/annotations

@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.

like image 44
Mike Avatar answered Dec 02 '22 22:12

Mike


If you just want to use the camel case version once, without annotations, use the IdenticalPropertyNamingStrategy:

$serializer = SerializerBuilder::create()->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy())->build();
like image 35
Linkmichiel Avatar answered Dec 02 '22 21:12

Linkmichiel