From acffa2ed50205b964272b2fa18435d384b7f1563 Mon Sep 17 00:00:00 2001 From: Shish Date: Thu, 28 Nov 2024 08:27:21 +0000 Subject: [PATCH] json_decode fixes (Breaking the diff against shish/safe into smaller parts for easier reviewing) --- lib/special_cases.php | 8 ++++---- tests/JsonDecodeTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 tests/JsonDecodeTest.php diff --git a/lib/special_cases.php b/lib/special_cases.php index a4e85a93..12cc0424 100644 --- a/lib/special_cases.php +++ b/lib/special_cases.php @@ -24,7 +24,7 @@ * Wrapper for json_decode that throws when an error occurs. * * @param string $json JSON data to parse - * @param bool $assoc When true, returned objects will be converted + * @param bool $associative When true, returned objects will be converted * into associative arrays. * @param int<1, max> $depth User specified recursion depth. * @param int $flags Bitmask of JSON decode options. @@ -33,10 +33,10 @@ * @throws JsonException if the JSON cannot be decoded. * @link http://www.php.net/manual/en/function.json-decode.php */ -function json_decode(string $json, bool $assoc = false, int $depth = 512, int $flags = 0): mixed +function json_decode(string $json, bool $associative = false, int $depth = 512, int $flags = 0): mixed { - $data = \json_decode($json, $assoc, $depth, $flags); - if (JSON_ERROR_NONE !== json_last_error()) { + $data = \json_decode($json, $associative, $depth, $flags); + if (!($flags & JSON_THROW_ON_ERROR) && JSON_ERROR_NONE !== json_last_error()) { throw JsonException::createFromPhpError(); } return $data; diff --git a/tests/JsonDecodeTest.php b/tests/JsonDecodeTest.php new file mode 100644 index 00000000..661fe178 --- /dev/null +++ b/tests/JsonDecodeTest.php @@ -0,0 +1,16 @@ +