Skip to content

Commit

Permalink
fallback implementation of json_last_error_msg
Browse files Browse the repository at this point in the history
  • Loading branch information
robmcvey committed Mar 14, 2015
1 parent bcb6549 commit 10cfb87
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/View/JsonView.php
Expand Up @@ -150,9 +150,9 @@ protected function _serialize($serialize) {
}

if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) {
$json = @json_encode($data, JSON_PRETTY_PRINT);
$json = json_encode($data, JSON_PRETTY_PRINT);
} else {
$json = @json_encode($data);
$json = json_encode($data);
}

if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) {
Expand Down
22 changes: 22 additions & 0 deletions lib/Cake/basics.php
Expand Up @@ -1061,3 +1061,25 @@ function convertSlash($string) {
}

}

if (!function_exists('json_last_error_msg')) {

/**
* Provides the fallback implementation of json_last_error_msg() available in PHP 5.5 and above.
*
* @return string Error message.
*/
function json_last_error_msg() {
static $errors = array(
JSON_ERROR_NONE => '',
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);
$error = json_last_error();
return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})";
}

}

0 comments on commit 10cfb87

Please sign in to comment.