Skip to content

Instantly share code, notes, and snippets.

@kaja47
Created July 28, 2010 19:13
Show Gist options
  • Save kaja47/495888 to your computer and use it in GitHub Desktop.
Save kaja47/495888 to your computer and use it in GitHub Desktop.
/**
* 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