標籤:io os ar 使用 java for sp on 2014
最近項目中遇到JSON格式驗證的問題,由於請求介面中可能存在新舊版本相容問題,老版本的用戶端,情況就不說了,糟透了,各種格式都有,看起來像JSON,但是……呵呵。
所以需要做相容,就得把之前不規範的東西規範一下,並且驗證其正確性;工具類如下:
/** * Project Name:v3a-b2c * File Name:JsonValidator.java * Package Name:com.v3a.util * Date:2014年10月26日下午1:59:59 * Copyright (c) 2014, [email protected] All Rights Reserved. **/package com.v3a.util;import java.text.CharacterIterator;import java.text.StringCharacterIterator;import java.util.List;import java.util.regex.Pattern;import org.apache.commons.lang.StringUtils;import com.google.common.collect.Lists;/** * ClassName:JsonValidator <br/> * Function: 用於校正一個字串是否是合法的JSON格式. <br/> * Date: 2014年10月26日 下午1:59:59 <br/> * 用法: new JsonValidator().validate(“JOSN字串”) * @author 勳輝 * @version * @since JDK 1.7 * @see */public class JsonValidator {private CharacterIterator it;private char c;private int col;public JsonValidator() {}/** * 驗證一個字串是否是合法的JSON串 * @param input 要驗證的字串 * @return true-合法 ,false-非法 */public boolean validate(String input) {input = input.trim();boolean ret = valid(input);return ret;}private boolean valid(String input) {/** * 相容不規範的JSON格式(使用單引號的方式),將單引號替換為雙引號 */input =formartJson(input);if ("".equals(input))return true;boolean ret = true;it = new StringCharacterIterator(input);c = it.first();col = 1;if (!value()) {ret = error("value", 1);} else {skipWhiteSpace();if (c != CharacterIterator.DONE) {ret = error("end", col);}}return ret;}private boolean value() {return literal("true") || literal("false") || literal("null") || string() || number() || object() || array();}private boolean literal(String text) {CharacterIterator ci = new StringCharacterIterator(text);char t = ci.first();if (c != t)return false;int start = col;boolean ret = true;for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) {if (t != nextCharacter()) {ret = false;break;}}nextCharacter();if (!ret)error("literal " + text, start);return ret;}private boolean array() {return aggregate(‘[‘, ‘]‘, false);}private boolean object() {return aggregate(‘{‘, ‘}‘, true);}private boolean aggregate(char entryCharacter, char exitCharacter, boolean prefix) {if (c != entryCharacter)return false;nextCharacter();skipWhiteSpace();if (c == exitCharacter) {nextCharacter();return true;}for (;;) {if (prefix) {int start = col;if (!string())return error("string", start);skipWhiteSpace();if (c != ‘:‘)return error("colon", col);nextCharacter();skipWhiteSpace();}if (value()) {skipWhiteSpace();if (c == ‘,‘) {nextCharacter();} else if (c == exitCharacter) {break;} else {return error("comma or " + exitCharacter, col);}} else {return error("value", col);}skipWhiteSpace();}nextCharacter();return true;}private boolean number() {if (!Character.isDigit(c) && c != ‘-‘)return false;int start = col;if (c == ‘-‘)nextCharacter();if (c == ‘0‘) {nextCharacter();} else if (Character.isDigit(c)) {while (Character.isDigit(c))nextCharacter();} else {return error("number", start);}if (c == ‘.‘) {nextCharacter();if (Character.isDigit(c)) {while (Character.isDigit(c))nextCharacter();} else {return error("number", start);}}if (c == ‘e‘ || c == ‘E‘) {nextCharacter();if (c == ‘+‘ || c == ‘-‘) {nextCharacter();}if (Character.isDigit(c)) {while (Character.isDigit(c))nextCharacter();} else {return error("number", start);}}return true;}private boolean string() {if (c != ‘"‘)return false;int start = col;boolean escaped = false;for (nextCharacter(); c != CharacterIterator.DONE; nextCharacter()) {if (!escaped && c == ‘\\‘) {escaped = true;} else if (escaped) {if (!escape()) {return false;}escaped = false;} else if (c == ‘"‘) {nextCharacter();return true;}}return error("quoted string", start);}private boolean escape() {int start = col - 1;if (" \\\"/bfnrtu".indexOf(c) < 0) {return error("escape sequence \\\",\\\\,\\/,\\b,\\f,\\n,\\r,\\t or \\uxxxx ", start);}if (c == ‘u‘) {if (!ishex(nextCharacter()) || !ishex(nextCharacter()) || !ishex(nextCharacter())|| !ishex(nextCharacter())) {return error("unicode escape sequence \\uxxxx ", start);}}return true;}private boolean ishex(char d) {return "0123456789abcdefABCDEF".indexOf(d) >= 0;}private char nextCharacter() {c = it.next();++col;return c;}private void skipWhiteSpace() {while (Character.isWhitespace(c)) {nextCharacter();}}private boolean error(String type, int col) {System.out.printf("type: %s, col: %s%s", type, col, System.getProperty("line.separator"));return false;}public static void main(String[] args) {String json ="{firstName : Brett, ‘lastName‘:‘McLaughlin‘, ‘email‘: ‘aaaa‘}";boolean valid = new JsonValidator().valid(json);System.out.println(valid);}public static String formartJson(String json) {String replaceAll = json.trim().replaceAll("‘", "\"").replace(" ", "");char[] charArray = replaceAll.toCharArray();String regEx = "^[A-Za-z]+$";Pattern pat = Pattern.compile(regEx);List<Integer> list = Lists.newLinkedList();StringBuilder builder = new StringBuilder(replaceAll);for (int i = 0; i < charArray.length-1; i++) {if (i < charArray.length) {String current = String.valueOf(charArray[i]).trim();if(StringUtils.isEmpty(current.trim())){builder.deleteCharAt(i);continue;}else{boolean flagcurrent = pat.matcher(current).find();String last = String.valueOf(charArray[i + 1]).trim();boolean flaglast = pat.matcher(last).find();/* * 如果當前是標點,則是false,如果是字母則是true,下一個(last)是字母的,則繼續,如果是標點則檢查是不是雙引號, * 不是則記下標誌位 */if (flagcurrent) {if (!flaglast) {if (!check(last))list.add(i+1);}}else{/* * 如果當前不是標點,則檢查下一個(last)是不是字串,如果是,則看當前標點是不是雙引號,不是,則記錄位置 * 如果當前不是字串並且last是字串的情況下,檢查當前是不是引號 * */if(!flagcurrent&&flaglast){if(!check(current)) {list.add(i+1);}}}}}}//StringBuilder builder = new StringBuilder(replaceAll);for (int i = 0; i < list.size(); i++) {builder.insert(list.get(i)+i, "\"");}return builder.toString();}/** * 檢查字元是不是JSON中的標準標點 * check * @author 勳輝 * @param checkString * @return * @since JDK 1.7 */public static boolean check(String checkString) {boolean flag = true;switch (checkString) {case "{":flag = false;break;case "}":flag = false;break;case "[":flag = false;break;case ",":flag = false;break;case ":":flag = false;break;case "\"":flag = true;break;}return flag;}}
JSON格式化以及JSON驗證工具