I'm using jms/serializer-bundle with Symfony, and a serialization configuration in YML form, to serialize some Doctrine objects as JSON.
An existing API means that some of the labels were named with underscores (e.g. "created_on")
createdOn:
expose: true
groups:
label
serialized_name: created_on
...
"created_on": "2014-03-06T14:28:17+0000",
but I'd like to tidy this up for a new API version and return responses in camelCase for new API calls.
I had hoped/thought that I could use the JMS Serializer "Until" and "Since" configurations to reference my Entity's properties twice, one with the old serialized_name and one with the new
createdOn:
expose: true
groups:
label
serialized_name: created_on
until: 1.9.x
createdOn:
expose: true
groups:
label
serialized_name: createdOn
since: 2.0
...
"createdOn": "2014-03-06T14:28:17+0000",
but obviously this does not work. I also tried turning the "2.1" versions into virtual_properties, and using different group names, but I couldn't get that to work either.
If I were using the plain SerializerBuilder/SerializationContext classes then it looks like I could do something like:
$serializer = SerializerBuilder::create();
$serializer->setPropertyNamingStrategy(
new SerializedNameAnnotationStrategy(
new CamelCaseNamingStrategy()
)
);
to force everything to camelCase anyway.
but I'm not. I'm using the jms_serializer service, relying upon its infrastructure to support the serialization groups I need, and I can't see any way through dependency_injection to build my own version of that, which adopts a different naming strategy, for my 'new' API responses.
Has anyone got a way to do this?
来源:https://stackoverflow.com/questions/27151442/changing-jms-serialization-json-label-names-according-to-api-level