JAVA校验JSON数据格式

在此博文基础上增添了更多校验功能https://blog.csdn.net/weixin_42540829/article/details/88326880

   public static final class RouteTableJsonValidator {

        /**
         * 数组指针
         */
        private static int index;
        /**
         * 字符串
         */
        private static String value;
        /**
         * 指针当前字符
         */
        private static char curchar;

        /**
         * 工具类非公有构造函数
         */
        private RouteTableJsonValidator() {
        }

        /**
         * @param rawValue 字符串参数
         * @return boolean 是否是JSON
         */
        public static boolean isJSON(String rawValue) throws Exception {
                index = 0;
                value = rawValue;
                switch (nextClean()) {
                    case '[':
                        if (nextClean() == ']') {
                            return true;
                        }
                        back();
                        return validateArray();
                    case '{':
                        if (nextClean() == '}') {
                            return true;
                        }
                        back();
                        return validateObject();
                    default:
                        return false;
                }
        }

        /**
         * @return char 下一个有效实义字符 char<=' ' char!=127
         * @throws JSONException 自定义JSON异常
         */
        public static char nextClean() throws JSONException {
            skipComment:
            do {
                next();
                if (curchar == '/') { // 跳过//类型与/*类型注释 遇回车或者null为注释内容结束
                    switch (next()) {
                        case 47: // '/'
                            do {
                                curchar = next();
                            } while (curchar != '\n' && curchar != '\r' && curchar != 0);
                            continue;
                        case 42: // '*'
                            do {
                                do {
                                    next();
                                    if (curchar == 0) {
                                        throw syntaxError("Unclosed comment");
                                    }
                                } while (curchar != '*');
                                if (next() == '/') {
                                    continue skipComment;
                                }
                                back();
                            } while (true);
                    }
                    back();
                    return '/';
                }
                if (curchar != '#') { //跳过#类型注释 遇回车或者null为注释内容结束
                    break;
                }
                do {
                    next();
                } while (curchar != '\n' && curchar != '\r' && curchar != 0);
            } while (true);
            if (curchar != 0 && (curchar <= ' ' || curchar == 127)) {
                throw syntaxError("JSON can not contain control character!");
            }
            return curchar;
        }

        /**
         * @return char 下一个字符
         */
        public static char next() {
            if (index < 0 || index >= value.length()) {
                return '\0';
            }
            curchar = value.charAt(index);
            if (curchar <= 0) {
                return '\0';
            } else {
                index++;
                return curchar;
            }
        }

        /**
         * 将指针移至上一个字符,回退一位
         */
        public static void back() { //异常在next中进行返回null
            index--;
        }

        /**
         * @param message 异常自定义信息
         * @return JSONException 自定义JSON异常
         */
        public static JSONException syntaxError(String message) {
            return new JSONException((new StringBuilder(String.valueOf(message))).toString());
        }

        /**
         * @return boolean 是否是JSONArray
         * @throws JSONException 自定义JSON异常
         */
        public static boolean validateArray() throws JSONException {
            do {
                //入口为合法 [ array 起点
                nextClean(); //下一位有效字符,跳过注释
                if (curchar == ']') { //空array 直接闭合返回
                    return true;
                } else if (curchar == ',') { //null
                    continue;
                } else if (curchar == '"') { //String
                    validateString();
                } else if (curchar == '-' || (curchar >= 48 && curchar <= 57)) { // number
                    validateNumber();
                } else if (curchar == '{') { // object
                    if (!validateObject()) { //递归校验
                        return false;
                    }
                } else if (curchar == '[') { // array
                    if (!validateArray()) { //递归校验
                        return false;
                    }
                } else if (curchar == 't' || curchar == 'f' || curchar == 'n') { // boolean and JSONNull
                    validateBooleanAndNull();
                } else {
                    return false;
                }
                switch (nextClean()) {
                    case ',':
                        continue;
                    case ']':
                        return true;
                    default:
                        return false;
                }
            } while (true);
        }

        /**
         * @return boolean 是否是JSONObject
         * @throws JSONException 自定义JSON异常
         */
        public static boolean validateObject() throws JSONException {
            do {
                nextClean();
                if (curchar == '}') {
                    return true;
                } else if (curchar == '"') { //String
                    validateString();
                } else {
                    return false;
                }
                if (nextClean() != ':') {
                    return false;
                }
                nextClean();
                if (curchar == ',') { //null
                    throw syntaxError("Missing value");
                } else if (curchar == '"') { //String
                    validateString();
                } else if (curchar == '-' || (curchar >= 48 && curchar <= 57)) { // number
                    validateNumber();
                } else if (curchar == '{') { // object
                    if (!validateObject()) {
                        return false;
                    }
                } else if (curchar == '[') { // array
                    if (!validateArray()) {
                        return false;
                    }
                } else if (curchar == 't' || curchar == 'f' || curchar == 'n') { // boolean and JSONNull
                    validateBooleanAndNull();
                } else {
                    return false;
                }
                switch (nextClean()) {
                    case ',':
                        continue;
                    case '}':
                        return true;
                    default:
                        return false;
                }
            } while (true);
        }

        /**
         * @throws JSONException 自定义JSON异常
         */
        public static void validateString() throws JSONException {
            StringBuilder sb = new StringBuilder();
            do {
                curchar = next(); //JSON对字符串中的转义项有严格规定
                sb.append(curchar);
                if (curchar == '\\') {
                    if ("\"\\/bfnrtu".indexOf(next()) < 0) {
                        throw syntaxError("Invalid escape string");
                    }
                    if (curchar == 'u') { //校验unicode格式 后跟4位16进制 0-9 a-f A-F
                        for (int i = 0; i < 4; i++) {
                            next();
                            if (curchar < 48 || (curchar > 57 && curchar < 65) || (curchar > 70 && curchar < 97)
                                    || curchar > 102) {
                                throw syntaxError("Invalid hexadecimal digits");
                            }
                        }
                    }
                }
            } while (curchar >= ' ' && "\":{[,#/".indexOf(curchar)< 0 && curchar != 127);
            if (curchar == 0) { //仅正常闭合双引号可通过
                throw syntaxError("Unclosed quot");
            } else if (curchar != '"') {
                throw syntaxError("Invalid string {\""+ sb +"}, missing quot ");
            } else if (value.charAt(index)=='"') {
                throw syntaxError("Missing comma after string: \"" + sb);
            } else if (value.charAt(index)==':' ) {
                String str = sb.substring(0, sb.length() - 1);
//                if (!validateRouteTableKey(sb.charAt(0), str)) {
//                    throw syntaxError("Invalid RouteTable KEY:\"" + sb);
//                }
                validateRouteTableValue(str);
            }
        }

        /**
         * @throws JSONException 自定义JSON异常
         */
        public static void validateNumber() throws JSONException {
            StringBuilder sb = new StringBuilder();
            if (curchar == '-') { //可选负号
                curchar = next();
            }
            if (curchar > 48 && curchar <= 57) { //整数部分
                do {
                    sb.append(curchar);
                    curchar = next();
                } while (curchar >= 48 && curchar <= 57);
            } else if (curchar == 48) {
                curchar = next();
            } else {
                throw syntaxError("Invalid number");
            }
            if (curchar == '.') { //小数部分
                do { //.后可不跟数字 如 5. 为合法数字
                    curchar = next();
                } while (curchar >= 48 && curchar <= 57);
            }
            if (curchar == 'e' || curchar == 'E') { //科学计数部分
                curchar = next();
                if (curchar == '+' || curchar == '-') {
                    curchar = next();
                }
                if (curchar < 48 || curchar > 57) {
                    throw syntaxError("Invalid number");
                }
                do {
                    curchar = next();
                } while (curchar >= 48 && curchar <= 57);
            }
            if (curchar == '"') {
                throw syntaxError("Missing comma after number: " + sb);
            }
            back(); //指针移至数字值最后一位,取下一位即判断是,或者],或者是合法注释
        }

        public static void validateRouteTableValue(String key) throws JSONException {
            int a = index;
            char c,d ;
            List<String> num_list = Arrays.asList("port");
            List<String> boolean_list = Arrays.asList("useSSL", "default_allow");
            do {
                ++a;
                c = value.charAt(a);
            } while (c == ' ' || c == '"');

            StringBuilder sb = new StringBuilder();
            do {
                d = value.charAt(a);
                sb.append(d);
                a++;
            } while (d !=' ' && ",]}\"".indexOf(d) <0);
            String str = sb.substring(0,sb.length()-1);

            if (num_list.contains(key) && !(c == '-' || (c >= 48 && c <= 57))) {
                throw syntaxError("RouteTable KEY:" + key + " match NumberType");
            }
            if (boolean_list.contains(key) && !(c == 't' || c == 'f' || c == 'n')) {
                throw syntaxError("RouteTable KEY:" + key + " match BooleanType");
            }
            String port_reg = "^([0-5]?\\d{0,4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$";
            if("port".equals(key) && !str.matches(port_reg)){
                throw syntaxError("Invalid Port : " + str);
            }
            String ip_reg = "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$";
            if("ip".equals(key) && !str.matches(ip_reg)){
                throw syntaxError("Invalid ip : " + str);
            }
        }

        public static boolean validateRouteTableKey(char firstChar, String str) throws JSONException {
            if ("".equals(str)) return false;
            List<String> a_list = Arrays.asList("allow");
            List<String> c_list = Arrays.asList("certChainFile","caFile","coordinator");
            List<String> d_list = Arrays.asList("default", "default_allow","deny");
            List<String> f_list = Arrays.asList("from");
            List<String> h_list = Arrays.asList("host");
            List<String> i_list = Arrays.asList("ip");
            List<String> n_list = Arrays.asList("negotiationType","caFile");
            List<String> p_list = Arrays.asList("permission", "port", "privateKeyFile");
            List<String> r_list = Arrays.asList("route_table","role");
            List<String> s_list = Arrays.asList("serving");
            List<String> t_list = Arrays.asList("to");
            List<String> u_list = Arrays.asList("useSSL");
            switch (firstChar) {
                case ' ':
                    return false;
                case 'a':
                    return a_list.contains(str);
                case 'f':
                    return f_list.contains(str);
                case 't':
                    return t_list.contains(str);
                case 'i':
                    return i_list.contains(str);
                case 'h':
                    return h_list.contains(str);
                case 's':
                    return s_list.contains(str);
                case 'u':
                    return u_list.contains(str);
                case 'c':
                    return c_list.contains(str);
                case 'n':
                    return n_list.contains(str);
                case 'r':
                    return r_list.contains(str);
                case 'd':
                    return d_list.contains(str);
                case 'p':
                    return p_list.contains(str);
                default:
                    return true;
            }

        }

        /**
         * @throws JSONException 自定义JSON异常
         */
        public static void validateBooleanAndNull() throws JSONException {
            StringBuilder sb = new StringBuilder();
            do {
                sb.append(curchar);
                curchar = next();
            } while (curchar >= ' ' && "\",]#/}".indexOf(curchar) < 0 && curchar != 127);
            if (!"null".equals(sb.toString()) && !"true".equals(sb.toString()) && !"false".equals(sb.toString())) {
                throw syntaxError("Boolean/null spelling errors : " + sb);
            }
            if (curchar == '"') {
                throw syntaxError("Missing comma after Boolean: " + sb);
            }
            back();
        }
    }

    public static void main(String[] args) {
//        String str = "{\"route_table\": {\"default\":{\"default\":[{\"ip\":\"127.0.0.1\",\"port\":9999,\"useSSL\":false}]},\"10000\":{\"default\":[{\"ip\":\"127.0.0.1\",\"port\":8889}],\"serving\":[{\"ip\":\"127.0.0.1\",\"port\":8080}]},\"123\":[{\"host\":\"10.35.27.23\",\"port\":8888,\"useSSL\":false,\"negotiationType\":\"\",\"certChainFile\":\"\",\"privateKeyFile\":\"\",\"caFile\":\"\"}]},\"permission\":{\"default_allow\":true}}";
        String str = "{\"route_table\":{\"default\":{\"default\":[{\"ip\":\"127.0.0.1\",\"port\":2345}]},\"10000\":{\"default\":[{\"ip\":\"127.0.0.1\",\"port\":8889}],\"serving\":[{\"ip\":\"127.0.0.1\",\"port\":8080}]}},\"permission\":{\"default_allow\":true,\"allow\":[{\"from\":{\"coordinator\":\"9999\",\"role\":\"guest\"},\"to\":{\"coordinator\":\"10000\",\"role\":\"host\"}}],\"deny\":[{\"from\":{\"coordinator\":\"9999\",\"role\":\"guest\"},\"to\":{\"coordinator\":\"10000\",\"role\":\"host\"}}]}}";
        try {
            if (RouteTableJsonValidator.isJSON(str)) {
                String s = JsonUtil.formatJson(str);
                System.out.println(s);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java是一种面向对象的编程语言,目前在应用程序开发中被广泛使用。校验JSON格式是Java中一项非常重要的任务,在开发中经常需要对JSON数据进行格式验证。JSONJavaScript对象表示法的缩写,它是一种轻量级的数据交换格式。 在Java中,我们可以使用标准的JSON库来校验JSON格式。例如,可以使用Jackson、Gson或JSON-lib等库来解析JSON数据并验证其格式是否正确。这些库都提供了丰富的API来校验JSON格式,并且易于使用。 下面是使用Jackson库进行JSON格式校验的示例代码: ```java import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.exc.MismatchedInputException; public class JsonValidator { private static ObjectMapper objectMapper = new ObjectMapper(); public static boolean isValidJson(String jsonStr) { try { JsonNode jsonNode = objectMapper.readTree(jsonStr); return true; } catch (MismatchedInputException e) { return false; } catch (Exception e) { return false; } } } ``` 该代码会将JSON字符串传入isValidJson()方法中,使用Jackson库的readTree()方法来解析JSON数据,并返回解析结果。如果解析失败,则说明JSON格式不正确,方法会返回false。如果解析成功,则说明JSON格式正确,方法返回true。 使用上述代码可以轻松地校验JSON格式。当然,也可以在需要的情况下进行定制化的校验,例如校验JSON键名、键值类型等,以适应各种业务需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值