Symfony 4在Normalizer中禁用CamelCase

时间:2019-06-06 14:26:27

标签: symfony serializer

在Symfony 4中,我使用symfony/serialize r将对象规范化为数组:

 $encoders     = [new JsonEncoder()];
 $normalizers  = [new ObjectNormalizer(null,null)];                   
 $this->serializer = new Serializer($normalizers, $encoders);



$normalized = $this->serializer->normalize($user); // this normalize user and change the prop name to dateOfCreation

我的用户Entity如下:

   class User {
       /**
       * @ORM\Column(type="datetime", nullable=true)
       * @SerializedName("date_of_creationo")
       */
       private date_of_creation;

     }

如何禁用camelCase转换?

1 个答案:

答案 0 :(得分:0)

您可以尝试将CamelCaseToSnakeCaseNameConverter用作规范化器

$normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());