Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer\Encoder; Chris@0: Chris@14: use Symfony\Component\Serializer\Exception\NotEncodableValueException; Chris@0: Chris@0: /** Chris@0: * Decodes JSON data. Chris@0: * Chris@0: * @author Sander Coolen Chris@0: */ Chris@0: class JsonDecode implements DecoderInterface Chris@0: { Chris@14: protected $serializer; Chris@14: Chris@0: private $associative; Chris@0: private $recursionDepth; Chris@0: Chris@0: /** Chris@0: * Constructs a new JsonDecode instance. Chris@0: * Chris@0: * @param bool $associative True to return the result associative array, false for a nested stdClass hierarchy Chris@0: * @param int $depth Specifies the recursion depth Chris@0: */ Chris@0: public function __construct($associative = false, $depth = 512) Chris@0: { Chris@0: $this->associative = $associative; Chris@0: $this->recursionDepth = (int) $depth; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Decodes data. Chris@0: * Chris@0: * @param string $data The encoded JSON string to decode Chris@0: * @param string $format Must be set to JsonEncoder::FORMAT Chris@0: * @param array $context An optional set of options for the JSON decoder; see below Chris@0: * Chris@0: * The $context array is a simple key=>value array, with the following supported keys: Chris@0: * Chris@0: * json_decode_associative: boolean Chris@0: * If true, returns the object as associative array. Chris@0: * If false, returns the object as nested stdClass Chris@0: * If not specified, this method will use the default set in JsonDecode::__construct Chris@0: * Chris@0: * json_decode_recursion_depth: integer Chris@0: * Specifies the maximum recursion depth Chris@0: * If not specified, this method will use the default set in JsonDecode::__construct Chris@0: * Chris@0: * json_decode_options: integer Chris@0: * Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher Chris@0: * Chris@0: * @return mixed Chris@0: * Chris@14: * @throws NotEncodableValueException Chris@0: * Chris@0: * @see http://php.net/json_decode json_decode Chris@0: */ Chris@17: public function decode($data, $format, array $context = []) Chris@0: { Chris@0: $context = $this->resolveContext($context); Chris@0: Chris@0: $associative = $context['json_decode_associative']; Chris@0: $recursionDepth = $context['json_decode_recursion_depth']; Chris@0: $options = $context['json_decode_options']; Chris@0: Chris@0: $decodedData = json_decode($data, $associative, $recursionDepth, $options); Chris@0: Chris@14: if (JSON_ERROR_NONE !== json_last_error()) { Chris@14: throw new NotEncodableValueException(json_last_error_msg()); Chris@0: } Chris@0: Chris@0: return $decodedData; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supportsDecoding($format) Chris@0: { Chris@0: return JsonEncoder::FORMAT === $format; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Merges the default options of the Json Decoder with the passed context. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: private function resolveContext(array $context) Chris@0: { Chris@17: $defaultOptions = [ Chris@0: 'json_decode_associative' => $this->associative, Chris@0: 'json_decode_recursion_depth' => $this->recursionDepth, Chris@0: 'json_decode_options' => 0, Chris@17: ]; Chris@0: Chris@0: return array_merge($defaultOptions, $context); Chris@0: } Chris@0: }