JMS Serializer: serialize camel-cased fields without them being renamed

Using JMSSerializer (or its bundle) serialized field are renamed by default to the underscore-separated words naming convention. If like me you prefer camel-case naming and you want fields serialized as it, there’s two solutions.

1. Set serialize name for each fields

You can use the @SerializedName annotation, serialized-name XML property or the serialized_name YAML configuration to fix the name that will be used for each fields.

2. Change the whole naming strategy

If like me you don’t want to set the serialized name for each field, you can change the naming strategy by your custom one or the simple IdenticalPropertyNamingStrategy that don’t rename fields.

The first solution is to set the naming strategy while creating your serializer using the SerializerBuilder:

The second solution if you use the bundle is to override the jms_serializer.camel_case_naming_strategy.class parameter in your app/config/parameters.yml file.


4 thoughts on “JMS Serializer: serialize camel-cased fields without them being renamed”

  1. jms_serializer.camel_case_naming_strategy.class: JMSSerializerNamingIdenticalPropertyNamingStrategy =>
    jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy

  2. Hi, how should I configure JMS serializer bundle to get response with lower cased property names when objects are passed for serialization?
    This config didn’t work for me. As far as I understand it only sets the strategy of camel casing, but didn’t set naming strategy as camel case. If I’m wrong please correct me.
    jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy

  3. Great tip!
    I suggest putting the setting in config.yml under ‘parameters’ as this is not something related to environment, so it should be added in version control.

Leave a Reply to Roman Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">