Bug #68992 [Csd]: json_encode stacks exceptions thrown by JsonSerializable classes
| From: | bukka@php.net | Date: | Mon, 29 Aug 2016 14:12:26 +0000 |
| Subject: | Bug #68992 [Csd]: json_encode stacks exceptions thrown by JsonSerializable classes | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-203652@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68992&edit=1
ID: 68992
Updated by: bukka@php.net
Reported by: knuteirik at leirahjelle dot net
Summary: json_encode stacks exceptions thrown by
JsonSerializable classes
Status: Closed
Type: Bug
Package: JSON related
Operating System: OS X
PHP Version: Irrelevant
Assigned To: bukka
Block user comment: N
Private report: N
New Comment:
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/
Previous Comments:
------------------------------------------------------------------------
[2016-08-29 14:03:30] 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:47] 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-05-17 19:07:35] 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.
------------------------------------------------------------------------
[2015-02-05 13:17:04] 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]
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68992&edit=1