You just need to create a class that implements JMS\Serializer\Exclusion\ExclusionStrategyInterface
<?php namespace JMS\Serializer\Exclusion; use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; use JMS\Serializer\Context; interface ExclusionStrategyInterface { public function shouldSkipClass(ClassMetadata $metadata, Context $context); public function shouldSkipProperty(PropertyMetadata $property, Context $context); }
In your case, you can implement your own logic in the shouldSkipProperty method and always return false for shouldSkipClass .
An example implementation can be found in the JMS / Serializer repository.
We refer to the created service as acme.my_exclusion_strategy_service below.
In the action of your controller:
<?php use Symfony\Component\HttpFoundation\Response; use JMS\Serializer\SerializationContext; // .... $context = SerializationContext::create() ->addExclusionStrategy($this->get('acme.my_exclusion_strategy_service')); $serial = $this->get('jms_serializer')->serialize($object, 'json', $context); return new Response($serial, Response::HTTP_OK, array('Content-Type' => 'application/json'));
Or if you use FOSRestBundle
<?php use FOS\RestBundle\View; use JMS\Serializer\SerializationContext; // .... $context = SerializationContext::create() ->addExclusionStrategy($this->get('acme.my_exclusion_strategy_service')) $view = new View($object); $view->setSerializationContext($context); // or you can create your own view factory that handles the creation // of the context for you return $this->get('fos_rest.view_handler')->handle($view);
rolebi
source share