JsonDecode.php 3.5 KB

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