Skip to content

Instantly share code, notes, and snippets.

@yesasha
Last active April 25, 2018 21:41
Show Gist options
  • Select an option

  • Save yesasha/5104dc2c12ed02ae4e27f0276f30ed37 to your computer and use it in GitHub Desktop.

Select an option

Save yesasha/5104dc2c12ed02ae4e27f0276f30ed37 to your computer and use it in GitHub Desktop.
json_last_error_msg function polyfill. http://php.net/manual/en/function.json-last-error-msg.php
<?php
define(JSON_ERROR_NONE, 0);
define(JSON_ERROR_DEPTH, 1);
define(JSON_ERROR_STATE_MISMATCH, 2);
define(JSON_ERROR_CTRL_CHAR, 3);
define(JSON_ERROR_SYNTAX, 4);
define(JSON_ERROR_UTF8, 5);
define(JSON_ERROR_RECURSION, 6);
define(JSON_ERROR_INF_OR_NAN, 7);
define(JSON_ERROR_UNSUPPORTED_TYPE, 8);
define(JSON_ERROR_INVALID_PROPERTY_NAME, 9);
define(JSON_ERROR_UTF16, 10);
/**
* json_last_error_msg function polyfill
* http://php.net/manual/en/function.json-last-error-msg.php
*
* @return string
*/
if (!function_exists('json_last_error_msg')) {
function json_last_error_msg () {
switch (json_last_error()) {
case JSON_ERROR_NONE: // 0 //
return 'No error has occurred';
case JSON_ERROR_DEPTH: // 1 //
return 'The maximum stack depth has been exceeded';
case JSON_ERROR_STATE_MISMATCH: // 2 //
return 'Invalid or malformed JSON';
case JSON_ERROR_CTRL_CHAR: // 3 //
return 'Control character error, possibly incorrectly encoded';
case JSON_ERROR_SYNTAX: // 4 //
return 'Syntax error';
case JSON_ERROR_UTF8: // 5 //
return 'Malformed UTF-8 characters, possibly incorrectly encoded'; // PHP 5.3.3
case JSON_ERROR_RECURSION: // 6 //
return 'One or more recursive references in the value to be encoded'; // PHP 5.5.0
case JSON_ERROR_INF_OR_NAN: // 7 //
return 'One or more NAN or INF values in the value to be encoded'; // PHP 5.5.0
case JSON_ERROR_UNSUPPORTED_TYPE: // 8 //
return 'A value of a type that cannot be encoded was given'; // PHP 5.5.0
case JSON_ERROR_INVALID_PROPERTY_NAME: // 9 //
return 'A property name that cannot be encoded was given'; // PHP 7.0.0
case JSON_ERROR_UTF16: // 10 //
return 'Malformed UTF-16 characters, possibly incorrectly encoded'; // PHP 7.0.0
default:
return 'Unknown error';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment