Last active
December 21, 2015 01:39
-
-
Save dazz/6229477 to your computer and use it in GitHub Desktop.
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 | |
$tests = array( | |
array('{"test":""}', 2), | |
array('{"test": {}}', 3), | |
array('{"test": {}, "test2": ""}', 3), | |
array('{"test": {}, "test2": {}}', 3), | |
array('{"test": {}, "test2": {"test3": ""}}', 3), | |
array('{"test": {}, "test2": {"test3": {}}}', 4), | |
); | |
function decode($json, $depth) | |
{ | |
var_dump($json); | |
var_dump('depth: '.$depth); | |
json_decode($json, false, $depth); | |
var_dump(decodeException(json_last_error())); | |
} | |
function decodeException($code) | |
{ | |
$message = ""; | |
switch ($code) { | |
case JSON_ERROR_NONE: | |
$message = "JSON_ERROR_NONE: No Error"; | |
break; | |
case JSON_ERROR_DEPTH: | |
$message = 'JSON_ERROR_DEPTH: The maximum stack depth has been exceeded'; | |
break; | |
case JSON_ERROR_STATE_MISMATCH: | |
$message = 'JSON_ERROR_STATE_MISMATCH: Invalid or malformed JSON'; | |
break; | |
case JSON_ERROR_CTRL_CHAR: | |
$message = 'JSON_ERROR_CTRL_CHAR: Control character error, possibly incorrectly encoded'; | |
break; | |
case JSON_ERROR_UTF8: | |
$message = 'JSON_ERROR_UTF8: Malformed UTF-8 characters, possibly incorrectly encoded'; | |
break; | |
case JSON_ERROR_SYNTAX: | |
$message = 'JSON_ERROR_SYNTAX: The JSON syntax is not correct'; | |
break; | |
default: | |
$message = 'Syntax error '.$code; | |
} | |
return $message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment