This gist is related to: symfony/symfony#13496
It will add a normalizer for object that implement \JsonSerializable be aware that using this will disable the serializer groups.
This gist is related to: symfony/symfony#13496
It will add a normalizer for object that implement \JsonSerializable be aware that using this will disable the serializer groups.
| <?php | |
| namespace Acme\Serializer\Normalizer; | |
| use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |
| class JsonSerializableNormalizer implements NormalizerInterface | |
| { | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function normalize($object, $format = null, array $context = array()) | |
| { | |
| return $object->jsonSerialize(); | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function supportsNormalization($data, $format = null) | |
| { | |
| return $data instanceof \JsonSerializable; | |
| } | |
| } |
| services: | |
| acme.serializer.normalizer.json_serializable: | |
| class: Acme\Serializer\Normalizer\JsonSerializableNormalizer | |
| tags: | |
| - { name: serializer.normalizer } |