JMS Serializer:如何将驼峰式大小写用于属性 [英] JMS Serializer: how to use camel case for properties

查看:51
本文介绍了JMS Serializer:如何将驼峰式大小写用于属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FOS Rest 包和 JMS Serializer 来创建 REST Api.问题是我想将 JSON 响应中的属性名称保留为驼峰式,而不是使用 _.

I'm using FOS Rest bundle and JMS Serializer to create a REST Api. The problem is I would like to keep the property names in the JSON response camel cased instead of using _.

例如,我有一个名为employeeIdentifier 的属性,默认情况下会转换为employee_identifier.

For example, I have a property called employeeIdentifier, by default that gets converted to employee_identifier.

我看到配置中有一个选项可以禁用小写并去掉 _,但后来它变成了 EmployeeIdentifier.

I saw that there's an option in the config to disable the lowercase and get rid of the _, but then it becomes EmployeeIdentifier.

JMS Serializer 有没有办法保留属性的原始名称?提前致谢

Is there any way that JMS Serializer keeps the original name of the property? Thanks in advance

推荐答案

我找到了一种全局执行的方法,如果您想保持属性名称不变,则需要使用 IdenticalPropertyNamingStrategy

I found a way to do it globally, if you want to keep the property names as is you need to use the IdenticalPropertyNamingStrategy

有几种方法可以做到这一点,首先通过更改配置(感谢@Phantom):

There are several ways to accomplish this, first by changing the config(Thanks @Phantom):

#config.yml
jms_serializer:
    property_naming: 
        id: 'jms_serializer.identical_property_naming_strategy'

其次,您可以为此覆盖默认别名

Second, you could override the default alias for this

services:
    jms_serializer.naming_strategy:
        alias: jms_serializer.identical_property_naming_strategy

bundle 定义了这些 https://github.com/schmittjoh/JMSSerializerBundle/blob/master/Resources/config/services.xml 所以你应该能够覆盖它们

The bundle defines these https://github.com/schmittjoh/JMSSerializerBundle/blob/master/Resources/config/services.xml so you should be able to override them

另一种方法是在初始化构建器时:

Another way to do it is when you initialize the builder:

$serializebuilder = JMS\Serializer\SerializerBuilder::create();
$serializebuilder->setPropertyNamingStrategy(new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy());
$serializer = $serializebuilder->build();

这篇关于JMS Serializer:如何将驼峰式大小写用于属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆