Permalink
Browse files

[HttpFoundation] Fix logic when JsonSerializable is missing

  • Loading branch information...
nicolas-grekas committed Sep 6, 2017
1 parent cf4f832 commit d1fe4153a16489d153e5660c8407789929d32ef1
Showing with 17 additions and 16 deletions.
  1. +17 −16 src/Symfony/Component/HttpFoundation/JsonResponse.php
@@ -127,30 +127,31 @@ public function setData($data = array())
// but doesn't provide the JsonSerializable interface.
set_error_handler(function () { return false; });
$data = @json_encode($data, $this->encodingOptions);
} else {
restore_error_handler();
} elseif (\PHP_VERSION_ID < 50500) {
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
// objects in a new exception that needs to be removed.
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
if (\PHP_VERSION_ID < 50500) {
// Clear json_last_error()
json_encode(null);
$errorHandler = set_error_handler('var_dump');
restore_error_handler();
set_error_handler(function () use ($errorHandler) {
if (JSON_ERROR_NONE === json_last_error()) {
return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
}
});
}
// Clear json_last_error()
json_encode(null);
$errorHandler = set_error_handler('var_dump');
restore_error_handler();
set_error_handler(function () use ($errorHandler) {
if (JSON_ERROR_NONE === json_last_error()) {
return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
}
});
$data = json_encode($data, $this->encodingOptions);
restore_error_handler();
} else {
$data = json_encode($data, $this->encodingOptions);
}
if (\PHP_VERSION_ID < 50500) {
} catch (\Error $e) {
if (\PHP_VERSION_ID < 50500 || !interface_exists('JsonSerializable', false)) {
restore_error_handler();
}
} catch (\Exception $e) {
if (\PHP_VERSION_ID < 50500) {
if (\PHP_VERSION_ID < 50500 || !interface_exists('JsonSerializable', false)) {
restore_error_handler();
}
if (interface_exists('JsonSerializable', false) && 'Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {

0 comments on commit d1fe415

Please sign in to comment.