JMS Serializer: how to use camel case for properties

jmsserializer virtual property
jmsserializer doctrine
jmsserializer discriminator
jms deserialize
annotation serializer
jmsserializercontext
jms serializer readonly
symfony serializer

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

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();

Default conversion of camelCase to underscores is counterintuitive , As I was using GSON which converts camel-case properties to underscore JSON There is also schmittjoh/JMSSerializerBundle#234 which� @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.

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'

JMS Serializer: serialize camel-cased fields without them being , JMS Serializer: serialize camel-cased fields without them being renamed. 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. Change the whole naming strategy. How to Use the Serializer¶. Symfony provides a serializer to serialize/deserialize to and from objects and different formats (e.g. JSON or XML). Before using it, read the Serializer component docs to get familiar with its philosophy and the normalizers and encoders terminology.

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

Annotations - serializer Documentation (master), If this is not defined, the property will be translated from camel-case to a lower- cased <?php use JMS\Serializer\Annotation\Accessor; class User { private $id; � When I wrote the serializer, I needed to pass PHP objects to a Java application which did some work on them, and then returned the result. As I was using GSON which converts camel-case properties to underscore JSON literals, it made sense to mirror that behavior.

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

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

The Serializer Component (Symfony Docs), CamelCase to snake_case; Configure name conversion using metadata. Serializing Boolean Attributes; Using Callbacks to Serialize Properties with Object� SignalR : use camel case Tag: serialization , json.net , signalr , deserialization I would like to know if there is a way to configure SignalR so that the client functions in the hub return objects using camel case.

Handling CamelCase with FOSRestBundle, The default naming strategy of JMSSerializerBundle is "camel_case", which means your body containing properties with underscores can lead to boilerplate code. use FOS\RestBundle\Controller\Annotations as Rest; use� When dealing with objects that are fairly similar or share properties, you may use interfaces or abstract classes. The Serializer component allows you to serialize and deserialize these objects using a “discriminator class mapping”. The discriminator is the field (in the serialized string) used to differentiate between the possible objects.

Using a Serializer > Symfony RESTful API: Course 1, The one we'll use is called "JMS Serializer" and there's a bundle for it called There's a push inside Symfony to make it eventually replace JMS Serialize for most use-cases. But in the dumped response, our property names are back! krtv/yii2-jms-serializer. This extension provides a Serializer component, that would allow you to use JMS Serializer library with Yii framework 2.0. JMSSerializer allows you to (de-)serialize data of any complexity. Currently, this extension supports JSON and XML. It also provides you with a rich tool-set to adapt the output to your specific needs.

Override JMS Serializer global camelCase, If I remove the global override to using identical property names, id is outputted as desired, however, obviously all the other properties are snake-case (i.e.� JMS Serializer exclude public properties. Ask Question Asked 4 years, 5 months ago. Active 4 years, Add jms_serializer.camel_case_naming_strategy.class:

Comments
  • no, that doesnt make any difference just removes the separador, so employeeIdentifier gets employeeidentifier
  • @petekaner did a little more digging found what you are looking for
  • 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 I updated the answer with a different method.
  • added to config > packages > jms_serializer.yaml in SF4; does not seem to work