public static function decode($string, $returnArrays = false)
    {
        $result = json_decode($string, $returnArrays);

        if (function_exists('json_last_error')) {
            switch (json_last_error()) {
                case JSON_ERROR_NONE:
                    return $result;

                case JSON_ERROR_DEPTH:
                    throw new JSONException('json_decode - Maximum stack depth exceeded');

                case JSON_ERROR_CTRL_CHAR:
                    throw new JSONException('json_decode - Unexpected control character found');

                case JSON_ERROR_SYNTAX:
                    throw new JSONException('json_decode - Syntax error, malformed JSON');

                default:
                    throw new JSONException('Invalid JSON');
            }
        } elseif ($result === null) {
            throw new JSONException('Invalid JSON');
        }

        return $result;
    }