1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.text.CharacterIterator; import java.text.StringCharacterIterator; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException;
public class JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(JsonValidator.class);
private final static Cache<String, JsonValidator> jsonValidatorCache = CacheBuilder.newBuilder() .initialCapacity(10) .concurrencyLevel(10) .build(); private CharacterIterator it; private char c; private int col;
private JsonValidator() { }
public static JsonValidator getJsonValidator() throws ExecutionException { return getJsonValidator("JsonValidator"); }
public static JsonValidator getJsonValidator(final String key) throws ExecutionException { return jsonValidatorCache.get(key, new Callable<JsonValidator>() { @Override public JsonValidator call() throws Exception { logger.info("[RBRequestParameter] - [getJsonValidator] - key:{} - 创建对象", key); return new JsonValidator(); } }); }
private static 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) throws ExecutionException {
boolean n = JsonValidator.getJsonValidator().validate("[\"703502\",\"703502\",\"703502\",\"703502\",\"703502\",\"703502\",\"703502\",\"703502\"]"); System.out.println(n); n = JsonValidator.getJsonValidator().validate("[\"906102\",\"906102\",\"906102\",\"906102\",\"906101\",\"906101\",\"906101\",\"906101\",\"906101\",\"906101\"]"); System.out.println(n); }
public boolean validate(String input) { input = input.trim(); boolean ret = valid(input); return ret; }
private boolean valid(String 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(c) >= 0; }
private char nextCharacter() { c = it.next(); ++col; return c; }
private void skipWhiteSpace() { while (Character.isWhitespace(c)) { nextCharacter(); } }
}
|