mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-04 18:42:40 +00:00
Handle json_encode failure
This commit is contained in:
parent
05aba7afb6
commit
08620704be
2 changed files with 38 additions and 2 deletions
|
@ -326,5 +326,25 @@ class JsonViewTest extends CakeTestCase {
|
|||
$View = new JsonView($Controller);
|
||||
$output = $View->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* JsonViewTest::testRenderJSONBoolFalse()
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderJSONBoolFalse() {
|
||||
$Request = new CakeRequest();
|
||||
$Response = new CakeResponse();
|
||||
$Controller = new Controller($Request, $Response);
|
||||
|
||||
// non utf-8 stuff
|
||||
$data = false;
|
||||
|
||||
$Controller->set($data);
|
||||
$Controller->set('_serialize', 'data');
|
||||
$View = new JsonView($Controller);
|
||||
$output = $View->render();
|
||||
$this->assertSame('null', $output);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -124,6 +124,10 @@ class JsonView extends View {
|
|||
|
||||
/**
|
||||
* Serialize view vars
|
||||
* For JSON documents, any errors are output using;
|
||||
* PHP 5 >= 5.5.0 : json_last_error_msg()
|
||||
* PHP 5 >= 5.3.0 : json_last_error()
|
||||
* PHP 5 <= 5.2.9 : Generic fall back error
|
||||
*
|
||||
* @param array $serialize The viewVars that need to be serialized
|
||||
* @return string The serialized data
|
||||
|
@ -145,10 +149,22 @@ class JsonView extends View {
|
|||
}
|
||||
|
||||
if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) {
|
||||
return json_encode($data, JSON_PRETTY_PRINT);
|
||||
$json = json_encode($data, JSON_PRETTY_PRINT);
|
||||
} else {
|
||||
$json = json_encode($data);
|
||||
}
|
||||
|
||||
return json_encode($data);
|
||||
if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) {
|
||||
if (function_exists('json_last_error_msg')) {
|
||||
$error = json_last_error_msg();
|
||||
} else {
|
||||
$error = __('JSON encoding failed: Error code %s', json_last_error());
|
||||
}
|
||||
throw new CakeException($error);
|
||||
} else if ($json === false) {
|
||||
throw new CakeException(__('Failed to parse JSON'));
|
||||
}
|
||||
return $json;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue