safe
safe copied to clipboard
Manage JSON_THROW_ON_ERROR conflict
Hi,
Consider the following code using the standard json_decode PHP function
\json_decode('"incorrect json');
echo 'Error : ' . json_last_error() . PHP_EOL;
\json_decode('"correct json agin"');
echo 'Error : ' . json_last_error() . PHP_EOL;
It has the expected behavior.
Error : 3
Error : 0
Now have a look at this the following code using the standard json_decode PHP function
\json_decode('"incorrect json');
echo 'Error : ' . json_last_error() . PHP_EOL;
\json_decode('"correct json"', false, 512, JSON_THROW_ON_ERROR);
echo 'Error : ' . json_last_error() . PHP_EOL;
It has comes to a weird behavior.
Error : 3
Error : 3
Due to the above, the Safe\json_decode function has this bug :
try {
json_decode('"incorrect json');
} catch (\Exception $e) {
echo 'Error : ' . json_last_error() . PHP_EOL;
}
json_decode('"correct json"', false, 512, JSON_THROW_ON_ERROR);
This is throwing because of json_last_error() not being updated when using JSON_THROW_ON_ERROR.
For the record I found this bug because of a vendor using standard json_decode and then me calling safe version with JSON_THROW_ON_ERROR.