Skip to content

Instantly share code, notes, and snippets.

@boekkooi
Created January 7, 2016 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boekkooi/5a7d59981739fa0ea5f0 to your computer and use it in GitHub Desktop.
Save boekkooi/5a7d59981739fa0ea5f0 to your computer and use it in GitHub Desktop.
JsonSerializableNormalizer

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 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment