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\Encoder;
17 * @author Jordi Boggiano <j.boggiano@seld.be>
19 class JsonEncoder implements EncoderInterface, DecoderInterface
21 const FORMAT = 'json';
26 protected $encodingImpl;
31 protected $decodingImpl;
33 public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
35 $this->encodingImpl = $encodingImpl ?: new JsonEncode();
36 $this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
40 * Returns the last encoding error (if any).
44 * @deprecated since version 2.5, to be removed in 3.0. JsonEncode throws exception if an error is found.
46 public function getLastEncodingError()
48 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\Component\Serializer\Encoder\JsonEncode::encode() method instead to get the last JSON encoding error.', E_USER_DEPRECATED);
50 return $this->encodingImpl->getLastError();
54 * Returns the last decoding error (if any).
58 * @deprecated since version 2.5, to be removed in 3.0. JsonDecode throws exception if an error is found.
60 public function getLastDecodingError()
62 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\Component\Serializer\Encoder\JsonDecode::decode() method instead to get the last JSON decoding error.', E_USER_DEPRECATED);
64 return $this->decodingImpl->getLastError();
70 public function encode($data, $format, array $context = array())
72 return $this->encodingImpl->encode($data, self::FORMAT, $context);
78 public function decode($data, $format, array $context = array())
80 return $this->decodingImpl->decode($data, self::FORMAT, $context);
86 public function supportsEncoding($format)
88 return self::FORMAT === $format;
94 public function supportsDecoding($format)
96 return self::FORMAT === $format;
100 * Resolves json_last_error message.
104 * @deprecated since 2.8, to be removed in 3.0. Use json_last_error_msg() instead.
106 public static function getLastErrorMessage()
108 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use json_last_error_msg() instead.', E_USER_DEPRECATED);
110 return json_last_error_msg();