11

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 _.

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

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

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

petekaner
  • 8,071
  • 5
  • 29
  • 52

5 Answers5

42

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

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

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();
Chase
  • 9,289
  • 5
  • 51
  • 77
  • 1
    no, that doesnt make any difference just removes the separador, so employeeIdentifier gets employeeidentifier – petekaner Apr 21 '17 at 07:36
  • @petekaner did a little more digging found what you are looking for – Chase Apr 21 '17 at 18:19
  • seems like it should work, but I get this error You have requested a non-existent parameter "jms_serializer.identical_property_naming_strategy.class". – petekaner Apr 22 '17 at 15:28
  • @petekaner I updated the answer with a different method. – Chase Apr 24 '17 at 22:03
  • Thanks this helped me – Seeker Nov 01 '17 at 14:25
  • You may also change strategy not by overriding a service alias, but instead changing your config, e.g. in `config.yml` `jms_serializer: property_naming: id: 'jms_serializer.identical_property_naming_strategy'` – Phantom May 01 '18 at 14:50
  • 3
    added to `config > packages > jms_serializer.yaml` in SF4; does not seem to work – Sam Feb 03 '19 at 09:17
  • While this works, it doesn't allow the serialized names from being overridden. For instance, `` outputs `idPublic` and not `id`. – user1032531 Mar 30 '19 at 12:31
  • This helped me, using JMSSerializer 2.4 – Ste May 14 '19 at 19:42
  • Worked for me with this config in `config/packages/jms_serializer.yaml` : jms_serializer: property_naming: id: jms_serializer.identical_property_naming_strategy and removing the cache manually https://github.com/schmittjoh/serializer/issues/1037 – Kvn91 Apr 08 '21 at 17:23
5

Having upgraded jms/serilizer-bundle from 1.1 to 2.2 the parameter hack described above did not work. You can override the service definition as follows:

#app/config/services.yml
services:
    ....
    jms_serializer.serialized_name_annotation_strategy:
        class: JMS\Serializer\Naming\SerializedNameAnnotationStrategy
        arguments:
            - '@jms_serializer.identical_property_naming_strategy'
Ian Foulds
  • 59
  • 1
  • 4
2

Worked for me (Symfony 4.4 and JMS ^3.8) with this config in config/packages/jms_serializer.yaml :

jms_serializer:
    property_naming:
        id: jms_serializer.identical_property_naming_strategy

and removing the cache manually

https://github.com/schmittjoh/serializer/issues/1037

Kvn91
  • 176
  • 1
  • 10
1

I found a way to do it but it's not the best way I think, there's an annotation SerializedName wich allows you to override the property serialization. The problem is that you have to do it one by one on every property with camel case, here's the documentation: YAML: http://jmsyst.com/libs/serializer/master/reference/yml_reference Annotation: http://jmsyst.com/libs/serializer/master/reference/annotations#serializedname

petekaner
  • 8,071
  • 5
  • 29
  • 52
0

I had to add the following to parameters.yml instead of config.yml:

jms_serializer.serialized_name_annotation_strategy.class: JMS\Serializer\Naming\SerializedNameAnnotationStrategy

Ste
  • 591
  • 1
  • 9
  • 20