4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
14 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15 use Symfony\Component\Serializer\Exception\LogicException;
18 * A normalizer that uses an objects own JsonSerializable implementation.
20 * @author Fred Cox <mcfedr@gmail.com>
22 class JsonSerializableNormalizer extends AbstractNormalizer
27 public function normalize($object, $format = null, array $context = array())
29 if ($this->isCircularReference($object, $context)) {
30 return $this->handleCircularReference($object);
33 if (!$object instanceof \JsonSerializable) {
34 throw new InvalidArgumentException(sprintf('The object must implement "%s".', \JsonSerializable::class));
37 if (!$this->serializer instanceof NormalizerInterface) {
38 throw new LogicException('Cannot normalize object because injected serializer is not a normalizer');
41 return $this->serializer->normalize($object->jsonSerialize(), $format, $context);
47 public function supportsNormalization($data, $format = null)
49 return $data instanceof \JsonSerializable;
55 public function supportsDenormalization($data, $type, $format = null)
63 public function denormalize($data, $class, $format = null, array $context = array())
65 throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class));