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

4

7

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?

Systematize answered 29/3, 2014 at 23:53 Comment(1)
Check this out also: florian.voutzinos.com/blog/…Cashew
D
5

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.

Dr answered 30/3, 2014 at 2:7 Comment(1)
thank you. I needed to add SerializedName annotations like this @SerializedName("className") and "use JMS\Serializer\Annotation\SerializedName;"Systematize
S
22

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.

Slippage answered 8/4, 2014 at 1:37 Comment(0)
D
5

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.

Dr answered 30/3, 2014 at 2:7 Comment(1)
thank you. I needed to add SerializedName annotations like this @SerializedName("className") and "use JMS\Serializer\Annotation\SerializedName;"Systematize
S
5

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

$serializer = SerializerBuilder::create()->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy())->build();
Spurn answered 12/4, 2016 at 13:3 Comment(0)
O
0

Inside Symfony, it make way more sense to use a compiler pass, as it avoid losing the @SerializedName annotation.

<?php

namespace AppBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class JMSSerializerCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $container->getDefinition('jms_serializer.serialized_name_annotation_strategy')
                  ->replaceArgument(0, new Reference('jms_serializer.identical_property_naming_strategy'));
    }
}
Outsell answered 2/2, 2018 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.