|  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: 2019-05-24 00:47 UTC
From: Assigned:
Status: Open Package: JSON related
PHP Version: 7.3.5 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2019-05-09 15:53 UTC]
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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-05-09 15:56 UTC]
-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]
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] <
 [2019-05-09 16:50 UTC]
Yes, discussion started,
 [2019-05-23 23:41 UTC]
-Type: Bug +Type: Feature/Change Request
 [2019-05-24 00:47 UTC]
I changed this to a feature request given it is decidedly deliberate behaviour.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Feb 23 14:01:30 2024 UTC