Last active
September 4, 2018 22:06
-
-
Save hitswa/8c77c77e9743a15496b8080ca019a1df to your computer and use it in GitHub Desktop.
conversion of Array to JSON safely
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function utf8ize($d) { | |
if (is_array($d)) { | |
foreach ($d as $k => $v) { | |
$d[$k] = utf8ize($v); | |
} | |
} else if (is_string ($d)) { | |
// return utf8_encode($d); | |
return iconv(mb_detect_encoding($d), "UTF-8", $d); | |
} | |
return $d; | |
} | |
function safe_json_encode($value){ | |
if (version_compare(PHP_VERSION, '5.4.0') >= 0) { | |
// $encoded = json_encode($value, JSON_PRETTY_PRINT); | |
// applying multiple filters as provided in following link [now tested output yet] | |
// http://php.net/manual/en/function.json-encode.php | |
$encoded = json_encode($value, JSON_PRETTY_PRINT | JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE); | |
} else { | |
$encoded = json_encode($value); | |
} | |
/* | |
List of possible exceptions : | |
0 JSON_ERROR_NONE | |
1 JSON_ERROR_DEPTH | |
2 JSON_ERROR_STATE_MISMATCH | |
3 JSON_ERROR_CTRL_CHAR | |
4 JSON_ERROR_SYNTAX | |
5 JSON_ERROR_UTF8 | |
6 JSON_ERROR_RECURSION | |
7 JSON_ERROR_INF_OR_NAN | |
8 JSON_ERROR_UNSUPPORTED_TYPE | |
*/ | |
switch (json_last_error()) { | |
case JSON_ERROR_NONE: | |
return $encoded; | |
case JSON_ERROR_DEPTH: | |
throw new Exception( 'Maximum stack depth exceeded: {code: ' . json_last_error() . ', message: ' . json_last_error_msg() .'}' ); | |
case JSON_ERROR_STATE_MISMATCH: | |
throw new Exception( 'Underflow or the modes mismatch: {code: ' . json_last_error() . ', message: ' . json_last_error_msg() .'}' ); | |
case JSON_ERROR_CTRL_CHAR: | |
throw new Exception( 'Unexpected control character found: {code: ' . json_last_error() . ', message: ' . json_last_error_msg() .'}' ); | |
case JSON_ERROR_SYNTAX: | |
throw new Exception( 'Syntax error, malformed JSON: {code: ' . json_last_error() . ', message: ' . json_last_error_msg() .'}' ); | |
case JSON_ERROR_UTF8: | |
// throw new Exception( 'Malformed UTF-8 characters, possibly incorrectly encoded' ); | |
$clean = utf8ize($value); | |
return safe_json_encode($clean); | |
case JSON_ERROR_RECURSION: | |
throw new Exception( 'JSON error ristriction: {code: ' . json_last_error() . ', message: ' . json_last_error_msg() .'}' ); | |
case JSON_ERROR_INF_OR_NAN: | |
throw new Exception( 'Infinite value or NaN (not a number) found in Json: {code: ' . json_last_error() . ', message: ' . json_last_error_msg() .'}' ); | |
/* # possible solution but taking too much time in response so avoiding this | |
json_encode(unserialize(str_replace(array('NAN;','INF;'),'0;',serialize($value)))); */ | |
case JSON_ERROR_UNSUPPORTED_TYPE: | |
throw new Exception( 'JSON error unsupported type: {code: ' . json_last_error() . ', message: ' . json_last_error_msg() .'}' ); | |
default: | |
throw new Exception( 'Unknown error: {code: ' . json_last_error() . ', message: ' . json_last_error_msg() .'}' ); | |
} | |
} | |
$arr = []; | |
try { | |
$json = safe_json_encode($arr); | |
} catch( Exception $e ) { | |
print_r($e); die; | |
} | |
header('Content-Type: application/json'); | |
echo $json; | |
exit(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment