Skip to content

Instantly share code, notes, and snippets.

@runejuhl
Created December 13, 2016 09:22
Show Gist options
  • Save runejuhl/59a46f8f072b14c3fb072b24a56873ba to your computer and use it in GitHub Desktop.
Save runejuhl/59a46f8f072b14c3fb072b24a56873ba to your computer and use it in GitHub Desktop.
class JSONError extends Exception {}
class JSONErrorDepth extends JSONError {}
class JSONErrorMalformed extends JSONError {}
class JSONErrorSyntax extends JSONError {}
function json_check_if_error() {
$error = json_last_error();
switch ($error) {
case JSON_ERROR_NONE:
return;
case JSON_ERROR_DEPTH:
throw new JSONErrorDepth();
break;
case JSON_ERROR_STATE_MISMATCH:
throw new JSONErrorMalformed();
break;
case JSON_ERROR_SYNTAX:
throw new JSONErrorSyntax();
break;
default:
throw new JSONError();
}
}
function from_json($string) {
$json = json_decode($string, true);
json_check_if_error();
return $json;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment