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?
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.
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.
If you just want to use the camel case version once, without annotations, use the IdenticalPropertyNamingStrategy
:
$serializer = SerializerBuilder::create()->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy())->build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With