Question

I'm using JMS serializer as the JMSSerializerBundle for Symfony2.

I serialize my entities this way :

$this->serializer = $this->container->get('jms_serializer');
$json = $this->serializer->serialize($values, 'json');

The bundle use automatically the ref config that match the bundle/entity name :

MyBundlePath/Resources/config/serializer/Entity.Name.yml

In my case, i need to provide two different configuration for the same entity class, is there any way to specify which yml/xml file to use to the serializer ?

Edit: nifr provided the solution, the group features

In my YAML conf case, you just need to add the groups param

Entity.ClassName:
    ...
    properties:
        some-property:
            ...
            groups: [first, second]

Then specify it in the serialize call :

$serializer->serialize($values, 'json', SerializationContext::create()->setGroups(array('my-group-name')));
Was it helpful?

Solution

That's what serialization groups are meant for. They're similar to validation groups in the validator component.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top