fallback implementation of json_last_error_msg

This commit is contained in:
Rob McVey 2015-03-14 09:13:31 +00:00
parent bcb6549565
commit 10cfb878be
2 changed files with 24 additions and 2 deletions

View file

@ -150,9 +150,9 @@ class JsonView extends View {
}
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) {

View file

@ -1061,3 +1061,25 @@ if (!function_exists('convertSlash')) {
}
}
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})";
}
}