Created
July 28, 2010 19:13
-
-
Save kaja47/495888 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
/** | |
* Decodes a JSON string. | |
* @param string | |
* @param boolean | |
* @param integer | |
* @return mixed | |
*/ | |
public static function decode($json, $assoc = FALSE, $depth = 512) | |
{ | |
$json = (string) $json; | |
$value = json_decode($json, $assoc, $depth); | |
if ($value === NULL && $json !== '' && strcasecmp($json, 'null')) { // '' do not clean json_last_error | |
$error = PHP_VERSION_ID >= 50300 ? json_last_error() : 0; | |
throw new JsonException(isset(self::$messages[$error]) ? self::$messages[$error] : 'Unknown error', $error); | |
} | |
return $value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment