Skip to content

Instantly share code, notes, and snippets.

@dazz
Last active December 21, 2015 01:39
Show Gist options
  • Save dazz/6229477 to your computer and use it in GitHub Desktop.
Save dazz/6229477 to your computer and use it in GitHub Desktop.
<?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