switching to full JSON API without POST array use, ensure all JSON operations are done with error detection
This commit is contained in:
32
lib/Json.php
32
lib/Json.php
@@ -33,9 +33,39 @@ class Json
|
||||
public static function encode($input)
|
||||
{
|
||||
$jsonString = json_encode($input);
|
||||
self::_detectError();
|
||||
return $jsonString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with the contents as described in the given JSON input
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param string $input
|
||||
* @throws Exception
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($input)
|
||||
{
|
||||
$array = json_decode($input, true);
|
||||
self::_detectError();
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects JSON errors and raises an exception if one is found
|
||||
*
|
||||
* @access private
|
||||
* @static
|
||||
* @throws Exception
|
||||
* @return void
|
||||
*/
|
||||
private static function _detectError()
|
||||
{
|
||||
$errorCode = json_last_error();
|
||||
if ($errorCode === JSON_ERROR_NONE) {
|
||||
return $jsonString;
|
||||
return;
|
||||
}
|
||||
|
||||
$message = 'A JSON error occurred';
|
||||
|
||||
Reference in New Issue
Block a user