php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77997 json_last_error should work even if JSON_THROW_ON_ERROR
Submitted: 2019-05-09 15:53 UTC Modified: 2024-09-05 20:52 UTC
From: danack@php.net Assigned:
Status: Duplicate Package: JSON related
PHP Version: 7.3.5 OS:
Private report: No CVE-ID: None
 [2019-05-09 15:53 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-05-09 15:56 UTC] requinix@php.net
-Summary: Json_last_error hugely suprising +Summary: json_last_error should work even if JSON_THROW_ON_ERROR -Package: *General Issues +Package: JSON related
 [2019-05-09 16:13 UTC] 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 16:50 UTC] danack@php.net
Yes, discussion started, http://news.php.net/php.internals/105653
 [2019-05-23 23:41 UTC] ajf@php.net
-Type: Bug +Type: Feature/Change Request
 [2019-05-24 00:47 UTC] ajf@php.net
I changed this to a feature request given it is decidedly deliberate behaviour.
 [2024-09-05 20:52 UTC] cmb@php.net
-Status: Open +Status: Duplicate
 [2024-09-05 20:52 UTC] cmb@php.net
Closing in favor of <https://github.com/php/php-src/issues/10166>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC