Req #77997 [Opn]: json_last_error should work even if JSON_THROW_ON_ERROR

From: Date: Fri, 24 May 2019 00:47:44 +0000
Subject: Req #77997 [Opn]: json_last_error should work even if JSON_THROW_ON_ERROR
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220978@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77997&edit=1 ID: 77997 Updated by: ajf@php.net Reported by: danack@php.net Summary: json_last_error should work even if JSON_THROW_ON_ERROR Status: Open Type: Feature/Change Request Package: JSON related PHP Version: 7.3.5 Block user comment: N Private report: N New Comment: I changed this to a feature request given it is decidedly deliberate behaviour. Previous Comments: ------------------------------------------------------------------------ [2019-05-09 16:50:28] danack@php.net Yes, discussion started, http://news.php.net/php.internals/105653 ------------------------------------------------------------------------ [2019-05-09 16:13:45] cmb@php.net This behavior is explicitly defined by the respective RFC[1]: | When passed this flag, the error behaviour of these functions is | changed. The global error state is left untouched, […] Changes to this behavior should at the very least be discussed on the internals mailing list. [1] <https://wiki.php.net/rfc/json_throw_on_error#proposal ------------------------------------------------------------------------ [2019-05-09 15:53:27] danack@php.net Description: ------------ When using JSON_THROW_ON_ERROR, json_last_error no longer refers to the previous function call, but instead the last json_(encode|decode) that doesn't have the JSON_THROW_ON_ERROR flag set. Test script: --------------- $foo = json_decode('[bar'); // many lines of code. $bar = json_encode('Hello world!', JSON_THROW_ON_ERROR); if (json_last_error() !== JSON_ERROR_NONE) { // Note, this is not true. The encoding was fine, but // json_last_error gets the error from the json_decode before throw new Exception("encoding went wrong!"); } echo "All is fine"; Expected result: ---------------- json_last_error() gives the error for the last call, regardless of the flag. Actual result: -------------- json_last_error() give the error for the previous that did not have JSON_THROW_ON_ERROR set. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77997&edit=1

« previous php.bugs (#220978) next »