JsonDecode.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\NotEncodableValueException;
  12. /**
  13. * Decodes JSON data.
  14. *
  15. * @author Sander Coolen <sander@jibber.nl>
  16. */
  17. class JsonDecode implements DecoderInterface
  18. {
  19. protected $serializer;
  20. /**
  21. * True to return the result as an associative array, false for a nested stdClass hierarchy.
  22. */
  23. public const ASSOCIATIVE = 'json_decode_associative';
  24. public const OPTIONS = 'json_decode_options';
  25. /**
  26. * Specifies the recursion depth.
  27. */
  28. public const RECURSION_DEPTH = 'json_decode_recursion_depth';
  29. private $defaultContext = [
  30. self::ASSOCIATIVE => false,
  31. self::OPTIONS => 0,
  32. self::RECURSION_DEPTH => 512,
  33. ];
  34. public function __construct(array $defaultContext = [])
  35. {
  36. $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
  37. }
  38. /**
  39. * Decodes data.
  40. *
  41. * @param string $data The encoded JSON string to decode
  42. * @param string $format Must be set to JsonEncoder::FORMAT
  43. * @param array $context An optional set of options for the JSON decoder; see below
  44. *
  45. * The $context array is a simple key=>value array, with the following supported keys:
  46. *
  47. * json_decode_associative: boolean
  48. * If true, returns the object as an associative array.
  49. * If false, returns the object as nested stdClass
  50. * If not specified, this method will use the default set in JsonDecode::__construct
  51. *
  52. * json_decode_recursion_depth: integer
  53. * Specifies the maximum recursion depth
  54. * If not specified, this method will use the default set in JsonDecode::__construct
  55. *
  56. * json_decode_options: integer
  57. * Specifies additional options as per documentation for json_decode
  58. *
  59. * @return mixed
  60. *
  61. * @throws NotEncodableValueException
  62. *
  63. * @see https://php.net/json_decode
  64. */
  65. public function decode(string $data, string $format, array $context = [])
  66. {
  67. $associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE];
  68. $recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH];
  69. $options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
  70. try {
  71. $decodedData = json_decode($data, $associative, $recursionDepth, $options);
  72. } catch (\JsonException $e) {
  73. throw new NotEncodableValueException($e->getMessage(), 0, $e);
  74. }
  75. if (\PHP_VERSION_ID >= 70300 && (\JSON_THROW_ON_ERROR & $options)) {
  76. return $decodedData;
  77. }
  78. if (\JSON_ERROR_NONE !== json_last_error()) {
  79. throw new NotEncodableValueException(json_last_error_msg());
  80. }
  81. return $decodedData;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function supportsDecoding(string $format)
  87. {
  88. return JsonEncoder::FORMAT === $format;
  89. }
  90. }