From 710b6aa2e0857529fea12360087f18f5d53c5aa5 Mon Sep 17 00:00:00 2001 From: Marcin Michalski Date: Thu, 5 Sep 2024 18:12:03 +0200 Subject: [PATCH] Skip `json_last_error` check when `JSON_THROW_ON_ERROR` flag is set in `json_decode` --- lib/special_cases.php | 2 +- tests/JsonDecodeTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/JsonDecodeTest.php diff --git a/lib/special_cases.php b/lib/special_cases.php index 25c5e476..8b9e12ff 100644 --- a/lib/special_cases.php +++ b/lib/special_cases.php @@ -37,7 +37,7 @@ function json_decode(string $json, bool $assoc = false, int $depth = 512, int $flags = 0): mixed { $data = \json_decode($json, $assoc, $depth, $flags); - if (JSON_ERROR_NONE !== json_last_error()) { + 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 @@ +