Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

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

$serializer = SerializerBuilder::create()->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy())->build();

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'));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top