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;
14 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
19 * @author Sander Coolen <sander@jibber.nl>
21 class JsonDecode implements DecoderInterface
24 * Specifies if the returned result should be an associative array or a nested stdClass object hierarchy.
31 * Specifies the recursion depth.
35 private $recursionDepth;
37 private $lastError = JSON_ERROR_NONE;
39 protected $serializer;
42 * Constructs a new JsonDecode instance.
44 * @param bool $associative True to return the result associative array, false for a nested stdClass hierarchy
45 * @param int $depth Specifies the recursion depth
47 public function __construct($associative = false, $depth = 512)
49 $this->associative = $associative;
50 $this->recursionDepth = (int) $depth;
54 * Returns the last decoding error (if any).
58 * @deprecated since version 2.5, to be removed in 3.0.
59 * The {@self decode()} method throws an exception if error found.
60 * @see http://php.net/manual/en/function.json-last-error.php json_last_error
62 public function getLastError()
64 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the decode() method instead to get the last JSON decoding error.', E_USER_DEPRECATED);
66 return $this->lastError;
72 * @param string $data The encoded JSON string to decode
73 * @param string $format Must be set to JsonEncoder::FORMAT
74 * @param array $context An optional set of options for the JSON decoder; see below
76 * The $context array is a simple key=>value array, with the following supported keys:
78 * json_decode_associative: boolean
79 * If true, returns the object as associative array.
80 * If false, returns the object as nested stdClass
81 * If not specified, this method will use the default set in JsonDecode::__construct
83 * json_decode_recursion_depth: integer
84 * Specifies the maximum recursion depth
85 * If not specified, this method will use the default set in JsonDecode::__construct
87 * json_decode_options: integer
88 * Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher
92 * @throws UnexpectedValueException
94 * @see http://php.net/json_decode json_decode
96 public function decode($data, $format, array $context = array())
98 $context = $this->resolveContext($context);
100 $associative = $context['json_decode_associative'];
101 $recursionDepth = $context['json_decode_recursion_depth'];
102 $options = $context['json_decode_options'];
104 if (\PHP_VERSION_ID >= 50400) {
105 $decodedData = json_decode($data, $associative, $recursionDepth, $options);
107 $decodedData = json_decode($data, $associative, $recursionDepth);
110 if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
111 throw new UnexpectedValueException(json_last_error_msg());
120 public function supportsDecoding($format)
122 return JsonEncoder::FORMAT === $format;
126 * Merges the default options of the Json Decoder with the passed context.
128 * @param array $context
132 private function resolveContext(array $context)
134 $defaultOptions = array(
135 'json_decode_associative' => $this->associative,
136 'json_decode_recursion_depth' => $this->recursionDepth,
137 'json_decode_options' => 0,
140 return array_merge($defaultOptions, $context);