Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Serializer\Encoder;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Decodes JSON data.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Sander Coolen <sander@jibber.nl>
|
Chris@0
|
20 */
|
Chris@0
|
21 class JsonDecode implements DecoderInterface
|
Chris@0
|
22 {
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Specifies if the returned result should be an associative array or a nested stdClass object hierarchy.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var bool
|
Chris@0
|
27 */
|
Chris@0
|
28 private $associative;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Specifies the recursion depth.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var int
|
Chris@0
|
34 */
|
Chris@0
|
35 private $recursionDepth;
|
Chris@0
|
36
|
Chris@0
|
37 private $lastError = JSON_ERROR_NONE;
|
Chris@0
|
38
|
Chris@0
|
39 protected $serializer;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Constructs a new JsonDecode instance.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param bool $associative True to return the result associative array, false for a nested stdClass hierarchy
|
Chris@0
|
45 * @param int $depth Specifies the recursion depth
|
Chris@0
|
46 */
|
Chris@0
|
47 public function __construct($associative = false, $depth = 512)
|
Chris@0
|
48 {
|
Chris@0
|
49 $this->associative = $associative;
|
Chris@0
|
50 $this->recursionDepth = (int) $depth;
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Decodes data.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @param string $data The encoded JSON string to decode
|
Chris@0
|
57 * @param string $format Must be set to JsonEncoder::FORMAT
|
Chris@0
|
58 * @param array $context An optional set of options for the JSON decoder; see below
|
Chris@0
|
59 *
|
Chris@0
|
60 * The $context array is a simple key=>value array, with the following supported keys:
|
Chris@0
|
61 *
|
Chris@0
|
62 * json_decode_associative: boolean
|
Chris@0
|
63 * If true, returns the object as associative array.
|
Chris@0
|
64 * If false, returns the object as nested stdClass
|
Chris@0
|
65 * If not specified, this method will use the default set in JsonDecode::__construct
|
Chris@0
|
66 *
|
Chris@0
|
67 * json_decode_recursion_depth: integer
|
Chris@0
|
68 * Specifies the maximum recursion depth
|
Chris@0
|
69 * If not specified, this method will use the default set in JsonDecode::__construct
|
Chris@0
|
70 *
|
Chris@0
|
71 * json_decode_options: integer
|
Chris@0
|
72 * Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher
|
Chris@0
|
73 *
|
Chris@0
|
74 * @return mixed
|
Chris@0
|
75 *
|
Chris@0
|
76 * @throws UnexpectedValueException
|
Chris@0
|
77 *
|
Chris@0
|
78 * @see http://php.net/json_decode json_decode
|
Chris@0
|
79 */
|
Chris@0
|
80 public function decode($data, $format, array $context = array())
|
Chris@0
|
81 {
|
Chris@0
|
82 $context = $this->resolveContext($context);
|
Chris@0
|
83
|
Chris@0
|
84 $associative = $context['json_decode_associative'];
|
Chris@0
|
85 $recursionDepth = $context['json_decode_recursion_depth'];
|
Chris@0
|
86 $options = $context['json_decode_options'];
|
Chris@0
|
87
|
Chris@0
|
88 $decodedData = json_decode($data, $associative, $recursionDepth, $options);
|
Chris@0
|
89
|
Chris@0
|
90 if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
|
Chris@0
|
91 throw new UnexpectedValueException(json_last_error_msg());
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 return $decodedData;
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 /**
|
Chris@0
|
98 * {@inheritdoc}
|
Chris@0
|
99 */
|
Chris@0
|
100 public function supportsDecoding($format)
|
Chris@0
|
101 {
|
Chris@0
|
102 return JsonEncoder::FORMAT === $format;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Merges the default options of the Json Decoder with the passed context.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @param array $context
|
Chris@0
|
109 *
|
Chris@0
|
110 * @return array
|
Chris@0
|
111 */
|
Chris@0
|
112 private function resolveContext(array $context)
|
Chris@0
|
113 {
|
Chris@0
|
114 $defaultOptions = array(
|
Chris@0
|
115 'json_decode_associative' => $this->associative,
|
Chris@0
|
116 'json_decode_recursion_depth' => $this->recursionDepth,
|
Chris@0
|
117 'json_decode_options' => 0,
|
Chris@0
|
118 );
|
Chris@0
|
119
|
Chris@0
|
120 return array_merge($defaultOptions, $context);
|
Chris@0
|
121 }
|
Chris@0
|
122 }
|