php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68992 json_encode stacks exceptions thrown by JsonSerializable classes
Submitted: 2015-02-05 13:17 UTC Modified: 2016-08-29 14:12 UTC
From: knuteirik at leirahjelle dot net Assigned: bukka (profile)
Status: Closed Package: JSON related
PHP Version: Irrelevant OS: OS X
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: knuteirik at leirahjelle dot net
New email:
PHP Version: OS:

 

 [2015-02-05 13:17 UTC] knuteirik at leirahjelle dot net
Description:
------------
When calling json_encode on an array of classes implementing JsonSerializable and all of the classes throw an exception, the original exception appears on the end of an exception chain that is equal to the length of the array + 1.

Test script:
---------------
<?php
class MyClass implements JsonSerializable {
    public function jsonSerialize() {
        throw new Exception('Not implemented!');
    }
}
$classes = [];
for($i = 0; $i < 5; $i++) {
    $classes[] = new MyClass();
}

try {
    json_encode($classes);
} catch(Exception $e) {
    do {
        printf("%s (%d) [%s]\n", $e->getMessage(), $e->getCode(), get_class($e));
    }
    while($e = $e->getPrevious());
}

Expected result:
----------------
Failed calling MyClass::jsonSerialize() (0) [Exception]
Not implemented! (0) [Exception]

Actual result:
--------------
Failed calling MyClass::jsonSerialize() (0) [Exception]
Failed calling MyClass::jsonSerialize() (0) [Exception]
Failed calling MyClass::jsonSerialize() (0) [Exception]
Failed calling MyClass::jsonSerialize() (0) [Exception]
Failed calling MyClass::jsonSerialize() (0) [Exception]
Not implemented! (0) [Exception]

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-17 19:07 UTC] bukka@php.net
-Assigned To: +Assigned To: bukka
 [2016-05-17 19:07 UTC] bukka@php.net
This is related to https://bugs.php.net/bug.php?id=70275 from the implementation PoV. It's caused by the fact that we don't return after the first error. I think that stacking is correct just if JSON_PARTIAL_OUTPUT_ON_ERROR is defined but otherwise there should be just the first exception and encoding finished after that.
 [2016-08-29 14:01 UTC] bukka@php.net
Automatic comment on behalf of bukka
Revision: http://git.php.net/?p=php-src.git;a=commit;h=27acbcf4df1ddac593f37fc4f9bb30fdfb456377
Log: Add test for fixed bug #68992
 [2016-08-29 14:01 UTC] bukka@php.net
-Status: Assigned +Status: Closed
 [2016-08-29 14:03 UTC] bukka@php.net
Automatic comment on behalf of bukka
Revision: http://git.php.net/?p=php-src.git;a=commit;h=27acbcf4df1ddac593f37fc4f9bb30fdfb456377
Log: Add test for fixed bug #68992
 [2016-08-29 14:12 UTC] bukka@php.net
It's been fixed (ignore the linked commit - all commits are in this PR: https://github.com/php/php-src/pull/2101 ).

Due to nature of the fix, it can't go to bug fixing release but it will be part of PHP 7.1/
 [2016-10-17 10:09 UTC] bwoebi@php.net
Automatic comment on behalf of bukka
Revision: http://git.php.net/?p=php-src.git;a=commit;h=27acbcf4df1ddac593f37fc4f9bb30fdfb456377
Log: Add test for fixed bug #68992
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 13:01:29 2024 UTC