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@14
|
14 use Symfony\Component\Serializer\Exception\NotEncodableValueException;
|
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@14
|
23 protected $serializer;
|
Chris@14
|
24
|
Chris@0
|
25 private $associative;
|
Chris@0
|
26 private $recursionDepth;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Constructs a new JsonDecode instance.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @param bool $associative True to return the result associative array, false for a nested stdClass hierarchy
|
Chris@0
|
32 * @param int $depth Specifies the recursion depth
|
Chris@0
|
33 */
|
Chris@0
|
34 public function __construct($associative = false, $depth = 512)
|
Chris@0
|
35 {
|
Chris@0
|
36 $this->associative = $associative;
|
Chris@0
|
37 $this->recursionDepth = (int) $depth;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Decodes data.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param string $data The encoded JSON string to decode
|
Chris@0
|
44 * @param string $format Must be set to JsonEncoder::FORMAT
|
Chris@0
|
45 * @param array $context An optional set of options for the JSON decoder; see below
|
Chris@0
|
46 *
|
Chris@0
|
47 * The $context array is a simple key=>value array, with the following supported keys:
|
Chris@0
|
48 *
|
Chris@0
|
49 * json_decode_associative: boolean
|
Chris@0
|
50 * If true, returns the object as associative array.
|
Chris@0
|
51 * If false, returns the object as nested stdClass
|
Chris@0
|
52 * If not specified, this method will use the default set in JsonDecode::__construct
|
Chris@0
|
53 *
|
Chris@0
|
54 * json_decode_recursion_depth: integer
|
Chris@0
|
55 * Specifies the maximum recursion depth
|
Chris@0
|
56 * If not specified, this method will use the default set in JsonDecode::__construct
|
Chris@0
|
57 *
|
Chris@0
|
58 * json_decode_options: integer
|
Chris@0
|
59 * Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher
|
Chris@0
|
60 *
|
Chris@0
|
61 * @return mixed
|
Chris@0
|
62 *
|
Chris@14
|
63 * @throws NotEncodableValueException
|
Chris@0
|
64 *
|
Chris@0
|
65 * @see http://php.net/json_decode json_decode
|
Chris@0
|
66 */
|
Chris@17
|
67 public function decode($data, $format, array $context = [])
|
Chris@0
|
68 {
|
Chris@0
|
69 $context = $this->resolveContext($context);
|
Chris@0
|
70
|
Chris@0
|
71 $associative = $context['json_decode_associative'];
|
Chris@0
|
72 $recursionDepth = $context['json_decode_recursion_depth'];
|
Chris@0
|
73 $options = $context['json_decode_options'];
|
Chris@0
|
74
|
Chris@0
|
75 $decodedData = json_decode($data, $associative, $recursionDepth, $options);
|
Chris@0
|
76
|
Chris@14
|
77 if (JSON_ERROR_NONE !== json_last_error()) {
|
Chris@14
|
78 throw new NotEncodableValueException(json_last_error_msg());
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 return $decodedData;
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * {@inheritdoc}
|
Chris@0
|
86 */
|
Chris@0
|
87 public function supportsDecoding($format)
|
Chris@0
|
88 {
|
Chris@0
|
89 return JsonEncoder::FORMAT === $format;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Merges the default options of the Json Decoder with the passed context.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @return array
|
Chris@0
|
96 */
|
Chris@0
|
97 private function resolveContext(array $context)
|
Chris@0
|
98 {
|
Chris@17
|
99 $defaultOptions = [
|
Chris@0
|
100 'json_decode_associative' => $this->associative,
|
Chris@0
|
101 'json_decode_recursion_depth' => $this->recursionDepth,
|
Chris@0
|
102 'json_decode_options' => 0,
|
Chris@17
|
103 ];
|
Chris@0
|
104
|
Chris@0
|
105 return array_merge($defaultOptions, $context);
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|