I wish to default to outputting camel case names, and this answer provides an excellent solution.
$serializer= \JMS\Serializer\SerializerBuilder::create()
->setDebug(true)
->addMetadataDir(APP_ROOT.'/config/serializer')
->setPropertyNamingStrategy(new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy())
->build();
I also wish to occasionally override the serialized name, however, when doing it the traditional way using serialized-name
, the serializer does not follow the instructions, and outputs idPublic
instead of the desired id
. 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. other_property
) and need to be individually configured which is not desirable.
<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
<class name="Fully\Qualfied\ClassName" exclusion-policy="ALL">
<property name="idPublic" serialized-name="id" expose="true"/>
<property name="name" expose="true"/>
<property name="otherProperty" expose="true"/>
</class>
</serializer>
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Fully\Qualfied\ClassName" table="systems">
<id name="id" type="integer">
<generator strategy="IDENTITY"/>
</id>
<field name="idPublic" type="integer" column="id_public"/>
<field name="name" type="string"/>
<field name="otherProperty" type="string" length="45" column="other_property"/>
</entity>
</doctrine-mapping>
As an alternate approach, I even tried to eliminating the normal property and using a virtual property instead, but it too resulted in the name idPublic
.
<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
<class name="Fully\Qualfied\ClassName" exclusion-policy="ALL">
<virtual-property method="getIdPublic" serialized-name="id" expose="true"/>
<property name="name" expose="true"/>
<property name="otherProperty" expose="true"/>
</class>
</serializer>
How can one override JMS Serializer's global configuration to use identical names?