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;
14 use Symfony\Component\Serializer\Encoder\ChainDecoder;
15 use Symfony\Component\Serializer\Encoder\ChainEncoder;
16 use Symfony\Component\Serializer\Encoder\EncoderInterface;
17 use Symfony\Component\Serializer\Encoder\DecoderInterface;
18 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
20 use Symfony\Component\Serializer\Exception\LogicException;
21 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
24 * Serializer serializes and deserializes data.
26 * objects are turned into arrays by normalizers.
27 * arrays are turned into various output formats by encoders.
29 * $serializer->serialize($obj, 'xml')
30 * $serializer->decode($data, 'xml')
31 * $serializer->denormalize($data, 'Class', 'xml')
33 * @author Jordi Boggiano <j.boggiano@seld.be>
34 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
35 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
36 * @author Kévin Dunglas <dunglas@gmail.com>
38 class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
41 * @var Encoder\ChainEncoder
46 * @var Encoder\ChainDecoder
53 protected $normalizers = array();
58 protected $normalizerCache = array();
63 protected $denormalizerCache = array();
65 public function __construct(array $normalizers = array(), array $encoders = array())
67 foreach ($normalizers as $normalizer) {
68 if ($normalizer instanceof SerializerAwareInterface) {
69 $normalizer->setSerializer($this);
72 $this->normalizers = $normalizers;
75 $realEncoders = array();
76 foreach ($encoders as $encoder) {
77 if ($encoder instanceof SerializerAwareInterface) {
78 $encoder->setSerializer($this);
80 if ($encoder instanceof DecoderInterface) {
81 $decoders[] = $encoder;
83 if ($encoder instanceof EncoderInterface) {
84 $realEncoders[] = $encoder;
87 $this->encoder = new ChainEncoder($realEncoders);
88 $this->decoder = new ChainDecoder($decoders);
94 final public function serialize($data, $format, array $context = array())
96 if (!$this->supportsEncoding($format)) {
97 throw new UnexpectedValueException(sprintf('Serialization for the format %s is not supported', $format));
100 if ($this->encoder->needsNormalization($format)) {
101 $data = $this->normalize($data, $format, $context);
104 return $this->encode($data, $format, $context);
110 final public function deserialize($data, $type, $format, array $context = array())
112 if (!$this->supportsDecoding($format)) {
113 throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format));
116 $data = $this->decode($data, $format, $context);
118 return $this->denormalize($data, $type, $format, $context);
124 public function normalize($data, $format = null, array $context = array())
126 // If a normalizer supports the given data, use it
127 if ($normalizer = $this->getNormalizer($data, $format)) {
128 return $normalizer->normalize($data, $format, $context);
131 if (null === $data || is_scalar($data)) {
135 if (is_array($data) || $data instanceof \Traversable) {
136 $normalized = array();
137 foreach ($data as $key => $val) {
138 $normalized[$key] = $this->normalize($val, $format, $context);
144 if (is_object($data)) {
145 if (!$this->normalizers) {
146 throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
149 throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data)));
152 throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
158 public function denormalize($data, $type, $format = null, array $context = array())
160 return $this->denormalizeObject($data, $type, $format, $context);
166 public function supportsNormalization($data, $format = null)
168 return null !== $this->getNormalizer($data, $format);
174 public function supportsDenormalization($data, $type, $format = null)
176 return null !== $this->getDenormalizer($data, $type, $format);
180 * Returns a matching normalizer.
182 * @param mixed $data Data to get the serializer for
183 * @param string $format format name, present to give the option to normalizers to act differently based on formats
185 * @return NormalizerInterface|null
187 private function getNormalizer($data, $format)
189 foreach ($this->normalizers as $normalizer) {
190 if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
197 * Returns a matching denormalizer.
199 * @param mixed $data data to restore
200 * @param string $class the expected class to instantiate
201 * @param string $format format name, present to give the option to normalizers to act differently based on formats
203 * @return DenormalizerInterface|null
205 private function getDenormalizer($data, $class, $format)
207 foreach ($this->normalizers as $normalizer) {
208 if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format)) {
217 final public function encode($data, $format, array $context = array())
219 return $this->encoder->encode($data, $format, $context);
225 final public function decode($data, $format, array $context = array())
227 return $this->decoder->decode($data, $format, $context);
231 * Denormalizes data back into an object of the given class.
233 * @param mixed $data data to restore
234 * @param string $class the expected class to instantiate
235 * @param string $format format name, present to give the option to normalizers to act differently based on formats
236 * @param array $context The context data for this particular denormalization
240 * @throws LogicException
241 * @throws UnexpectedValueException
243 private function denormalizeObject($data, $class, $format, array $context = array())
245 if (!$this->normalizers) {
246 throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
249 if ($normalizer = $this->getDenormalizer($data, $class, $format)) {
250 return $normalizer->denormalize($data, $class, $format, $context);
253 throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
259 public function supportsEncoding($format)
261 return $this->encoder->supportsEncoding($format);
267 public function supportsDecoding($format)
269 return $this->decoder->supportsDecoding($format);