GlisWeb framework
_json.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
12 function jsonCheck($string) {
13  json_decode($string);
14  return (json_last_error() == JSON_ERROR_NONE);
15 }
16 
17 function jsonValidate($string)
18 {
19  // decode the JSON data
20  $result = json_decode($string);
21 
22  // switch and check possible JSON errors
23  switch (json_last_error()) {
24  case JSON_ERROR_NONE:
25  $error = ''; // JSON is valid // No error has occurred
26  break;
27  case JSON_ERROR_DEPTH:
28  $error = 'The maximum stack depth has been exceeded.';
29  break;
30  case JSON_ERROR_STATE_MISMATCH:
31  $error = 'Invalid or malformed JSON.';
32  break;
33  case JSON_ERROR_CTRL_CHAR:
34  $error = 'Control character error, possibly incorrectly encoded.';
35  break;
36  case JSON_ERROR_SYNTAX:
37  $error = 'Syntax error, malformed JSON.';
38  break;
39  // PHP >= 5.3.3
40  case JSON_ERROR_UTF8:
41  $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
42  break;
43  // PHP >= 5.5.0
44  case JSON_ERROR_RECURSION:
45  $error = 'One or more recursive references in the value to be encoded.';
46  break;
47  // PHP >= 5.5.0
48  case JSON_ERROR_INF_OR_NAN:
49  $error = 'One or more NAN or INF values in the value to be encoded.';
50  break;
51  case JSON_ERROR_UNSUPPORTED_TYPE:
52  $error = 'A value of a type that cannot be encoded was given.';
53  break;
54  default:
55  $error = 'Unknown JSON error occured.';
56  break;
57  }
58 
59  if ($error !== '') {
60  // throw the Exception or exit // or whatever :)
61  exit($error);
62  }
63 
64  // everything is OK
65  return $result;
66 }
67 
68 ?>
jsonValidate($string)
Definition: _json.tools.php:17
jsonCheck($string)
Definition: _json.tools.php:12