private static function parseJSON($json)
{
try {
// Format JSON
$json = '{' . $json . '}';
// Finds a word that is at the beginning, in quotation marks or not and replaces
$match = null;
preg_match('~["]?([[:alnum:]]+)["]?[\\s\\n\\r\\t]{0,}?:~', $json, $match);
$command = $match[1];
if ($command != 'query') {
$json = preg_replace('~' . $command . '~', 'query', $json, 1);
}
// Converts the encoding
$json = mb_detect_encoding($json, 'UTF-8', true) ? $json : utf8_encode($json);
// Replaces the excess whitespace and line breaks
$json = preg_replace(array('~\\s+~', '~[\\r\\n]+~'), array(' ', ''), $json);
// Fixes whitespace bug
$json = preg_replace('~(}[\\s]?,[\\s]?{)~', '},{', $json);
// Formats the JSON string
$json = preg_replace('~\\s?(,?[{,])\\s*([^"]+?)\\s*:\\s?~', '$1"$2":', $json);
// Decoded array
$jarr = json_decode($json, true);
if (version_compare(PHP_VERSION, '5.3.5') >= 0) {
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
$json_error = self::$exception['json-error-depth'];
break;
case JSON_ERROR_STATE_MISMATCH:
$json_error = self::$exception['json-error-state-mismatch'];
break;
case JSON_ERROR_CTRL_CHAR:
$json_error = self::$exception['json-error-ctrl-char'];
break;
case JSON_ERROR_SYNTAX:
$json_error = self::$exception['json-error-syntax'];
break;
}
} else {
$json_error = self::$exception['json-error-syntax'];
}
if (is_null($jarr)) {
throw new \PDOException($json_error);
}
return $jarr;
} catch (\PDOException $e) {
self::stackTrace($e);
}
}