php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54058 json_last_error() doesn't work properly with arrays/objects
Submitted: 2011-02-21 07:05 UTC Modified: 2011-02-21 09:09 UTC
From: slusarz at curecanti dot org Assigned: scottmac (profile)
Status: Closed Package: JSON related
PHP Version: 5.3.5 OS: Linux
Private report: No CVE-ID: None
 [2011-02-21 07:05 UTC] slusarz at curecanti dot org
Description:
------------
json_last_error() doesn't work properly in arrays/objects.  If an error occurs when encoding a property/key, and it isn't the LAST property/key, json_last_error() is reset to 0.

json_last_error() should report the last parsing error from the last json_encode() call AS AN ENTIRETY, not the last internal encoding step that takes place inside the function.

Test script:
---------------
$bad_utf8 = quoted_printable_decode('=B0');

json_encode($bad_utf8);
print "ERROR: " . json_last_error() . "\n";

$a = new stdclass;
$a->foo = quoted_printable_decode('=B0');
json_encode($a);
print "ERROR: " . json_last_error() . "\n";

$b = new stdclass;
$b->foo = $bad_utf8;
$b->bar = 1;
json_encode($b);
print "ERROR: " . json_last_error() . "\n";

$c = array(
    'foo' => $bad_utf8,
    'bar' => 1
);
json_encode($c);
print "ERROR: " . json_last_error() . "\n";


Expected result:
----------------
ERROR: 5
ERROR: 5
ERROR: 5
ERROR: 5

Actual result:
--------------
ERROR: 5
ERROR: 5
ERROR: 0
ERROR: 0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-21 09:09 UTC] scottmac@php.net
Automatic comment from SVN on behalf of scottmac
Revision: http://svn.php.net/viewvc/?view=revision&revision=308529
Log: Fix Bug #54058, invalid utf-8 doesn't set json_encode() in all cases
 [2011-02-21 09:09 UTC] scottmac@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: scottmac
 [2011-02-21 09:09 UTC] scottmac@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 10:01:30 2024 UTC