Bug #68992 [Opn]: json_encode stacks exceptions thrown by JsonSerializable classes
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: Open
Type: Bug
Package: JSON related
Operating System: OS X
PHP Version: Irrelevant
-Assigned To:
+Assigned To: bukka
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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
Thread (4 messages)