| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Encoder; |
| 13 |
|
| 14 |
use Seld\JsonLint\JsonParser; |
| 15 |
use Symfony\Component\Serializer\Exception\NotEncodableValueException; |
| 16 |
use Symfony\Component\Serializer\Exception\UnsupportedException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class JsonDecode implements DecoderInterface |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public const ASSOCIATIVE = 'json_decode_associative'; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public const DETAILED_ERROR_MESSAGES = 'json_decode_detailed_errors'; |
| 34 |
|
| 35 |
public const OPTIONS = 'json_decode_options'; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public const RECURSION_DEPTH = 'json_decode_recursion_depth'; |
| 41 |
|
| 42 |
private array $defaultContext = [ |
| 43 |
self::ASSOCIATIVE => false, |
| 44 |
self::DETAILED_ERROR_MESSAGES => false, |
| 45 |
self::OPTIONS => 0, |
| 46 |
self::RECURSION_DEPTH => 512, |
| 47 |
]; |
| 48 |
|
| 49 |
public function __construct(array $defaultContext = []) |
| 50 |
{ |
| 51 |
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function decode(string $data, string $format, array $context = []): mixed |
| 84 |
{ |
| 85 |
$associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE]; |
| 86 |
$recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH]; |
| 87 |
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS]; |
| 88 |
|
| 89 |
try { |
| 90 |
$decodedData = json_decode($data, $associative, $recursionDepth, $options); |
| 91 |
} catch (\JsonException $e) { |
| 92 |
throw new NotEncodableValueException($e->getMessage(), 0, $e); |
| 93 |
} |
| 94 |
|
| 95 |
if (\JSON_THROW_ON_ERROR & $options) { |
| 96 |
return $decodedData; |
| 97 |
} |
| 98 |
|
| 99 |
if (\JSON_ERROR_NONE === json_last_error()) { |
| 100 |
return $decodedData; |
| 101 |
} |
| 102 |
$errorMessage = json_last_error_msg(); |
| 103 |
|
| 104 |
if (!($context[self::DETAILED_ERROR_MESSAGES] ?? $this->defaultContext[self::DETAILED_ERROR_MESSAGES])) { |
| 105 |
throw new NotEncodableValueException($errorMessage); |
| 106 |
} |
| 107 |
|
| 108 |
if (!class_exists(JsonParser::class)) { |
| 109 |
throw new UnsupportedException(\sprintf('Enabling "%s" serializer option requires seld/jsonlint. Try running "composer require seld/jsonlint".', self::DETAILED_ERROR_MESSAGES)); |
| 110 |
} |
| 111 |
|
| 112 |
throw new NotEncodableValueException((new JsonParser())->lint($data)?->getMessage() ?: $errorMessage); |
| 113 |
} |
| 114 |
|
| 115 |
public function supportsDecoding(string $format): bool |
| 116 |
{ |
| 117 |
return JsonEncoder::FORMAT === $format; |
| 118 |
} |
| 119 |
} |