Skip to content

Commit 2875661

Browse files
committed
Server:Structure支持~校验正则;优化JSONResponse格式化key的处理;
1 parent 6c58560 commit 2875661

File tree

2 files changed

+109
-34
lines changed

2 files changed

+109
-34
lines changed

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/JSONResponse.java

Lines changed: 108 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.alibaba.fastjson.JSONArray;
2121
import com.alibaba.fastjson.JSONObject;
2222

23+
import zuo.biao.apijson.server.NotNull;
24+
2325
/**parser for response
2426
* @author Lemon
2527
* @see #getObject
@@ -64,7 +66,7 @@ public JSONResponse(JSONObject object) {
6466
public static final String MSG_SUCCEED = "success"; //成功
6567
public static final String MSG_SERVER_ERROR = "Internal Server Error!"; //服务器内部错误
6668

67-
69+
6870
public static final String KEY_CODE = "code";
6971
public static final String KEY_MSG = "msg";
7072
public static final String KEY_COUNT = "count";
@@ -197,14 +199,14 @@ public JSONResponse getJSONResponse(String key) {
197199
return getObject(key, JSONResponse.class);
198200
}
199201
//cannot get javaBeanDeserizer
200-
// /**获取内部的JSONResponse
201-
// * @param response
202-
// * @param key
203-
// * @return
204-
// */
205-
// public static JSONResponse getJSONResponse(JSONObject response, String key) {
206-
// return response == null ? null : response.getObject(key, JSONResponse.class);
207-
// }
202+
// /**获取内部的JSONResponse
203+
// * @param response
204+
// * @param key
205+
// * @return
206+
// */
207+
// public static JSONResponse getJSONResponse(JSONObject response, String key) {
208+
// return response == null ? null : response.getObject(key, JSONResponse.class);
209+
// }
208210
//状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
209211

210212

@@ -235,7 +237,7 @@ public <T> T getObject(String key, Class<T> clazz) {
235237
* @return
236238
*/
237239
public static <T> T getObject(JSONObject object, String key, Class<T> clazz) {
238-
return toObject(object == null ? null : object.getJSONObject(key), clazz);
240+
return toObject(object == null ? null : object.getJSONObject(formatObjectKey(key)), clazz);
239241
}
240242

241243
/**
@@ -291,7 +293,7 @@ public static <T> List<T> getList(JSONObject object, Class<T> clazz) {
291293
* @return
292294
*/
293295
public static <T> List<T> getList(JSONObject object, String key, Class<T> clazz) {
294-
return object == null ? null : JSON.parseArray(object.getString(replaceArray(key)), clazz);
296+
return object == null ? null : JSON.parseArray(object.getString(formatArrayKey(key)), clazz);
295297
}
296298

297299
/**
@@ -322,7 +324,7 @@ public static JSONArray getArray(JSONObject object) {
322324
* @return
323325
*/
324326
public static JSONArray getArray(JSONObject object, String key) {
325-
return object == null ? null : object.getJSONArray(replaceArray(key));
327+
return object == null ? null : object.getJSONArray(formatArrayKey(key));
326328
}
327329

328330

@@ -352,13 +354,13 @@ public static JSONObject format(final JSONObject object) {
352354
value = object.get(key);
353355

354356
if (value instanceof JSONArray) {//JSONArray,遍历来format内部项
355-
formatedObject.put(replaceArray(key), format((JSONArray) value));
357+
formatedObject.put(formatArrayKey(key), format((JSONArray) value));
356358
}
357359
else if (value instanceof JSONObject) {//JSONObject,往下一级提取
358-
formatedObject.put(getSimpleName(key), format((JSONObject) value));
360+
formatedObject.put(formatObjectKey(key), format((JSONObject) value));
359361
}
360362
else {//其它Object,直接填充
361-
formatedObject.put(getSimpleName(key), value);
363+
formatedObject.put(formatOtherKey(key), value);
362364
}
363365
}
364366
}
@@ -397,35 +399,108 @@ else if (value instanceof JSONObject) {//JSONObject,往下一级提取
397399
return formatedArray;
398400
}
399401

400-
/**替换key+KEY_ARRAY为keyList
401-
* @param key
402-
* @return getSimpleName(isArrayKey(key) ? getArrayKey(...) : key) {@link #getSimpleName(String)}
402+
/**格式化数组的名称 key[] => keyList; key:alias[] => aliasList; Table-column[] => tableColumnList
403+
* @param key empty ? "list" : key + "List" 且首字母小写
404+
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatAt = false, formatColon = true, formatHyphen = true, firstCase = true
403405
*/
404-
public static String replaceArray(String key) {
406+
public static String formatArrayKey(String key) {
405407
if (isArrayKey(key)) {
406-
key = getArrayKey(key.substring(0, key.lastIndexOf(KEY_ARRAY)));
408+
key = StringUtil.addSuffix(key.substring(0, key.lastIndexOf(KEY_ARRAY)), "list");
407409
}
408-
return getSimpleName(key);
410+
int index = key == null ? -1 : key.indexOf(":");
411+
if (index >= 0) {
412+
return key.substring(index + 1); //不处理自定义的
413+
}
414+
415+
return formatKey(key, false, false, true, true); //节约性能,除了表对象 Table-column:alias[] ,一般都符合变量命名规范
409416
}
410-
/**获取列表变量名
411-
* @param key => StringUtil.getNoBlankString(key)
412-
* @return empty ? "list" : key + "List" 且首字母小写
417+
418+
/**格式化对象的名称 name => name; name:alias => alias
419+
* @param key name 或 name:alias
420+
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatAt = false, formatColon = true, formatHyphen = false, firstCase = true
413421
*/
414-
public static String getArrayKey(String key) {
415-
return StringUtil.addSuffix(key, "list");
422+
public static String formatObjectKey(String key) {
423+
int index = key == null ? -1 : key.indexOf(":");
424+
if (index >= 0) {
425+
return key.substring(index + 1); //不处理自定义的
426+
}
427+
428+
return formatKey(key, false, false, false, true); //节约性能,除了表对象 Table:alias ,一般都符合变量命名规范
416429
}
417430

418-
/**获取简单名称
431+
/**格式化普通值的名称 name => name; name:alias => alias
432+
* @param fullName name 或 name:alias
433+
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatAt = true, formatColon = false, formatHyphen = false, firstCase = false
434+
*/
435+
public static String formatOtherKey(String fullName) {
436+
return formatKey(fullName, true, false, false, false); //节约性能,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
437+
}
438+
439+
/**格式化名称
419440
* @param fullName name 或 name:alias
441+
* @param formatAt 去除前缀 @ , @a => a
442+
* @param formatColon 去除分隔符 : , A:b => b
443+
* @param formatHyphen 去除分隔符 - , A-b-cd-Efg => aBCdEfg
444+
* @param firstCase 第一个单词首字母小写,后面的首字母大写, Ab => ab ; A-b-Cd => aBCd
420445
* @return name => name; name:alias => alias
421446
*/
422-
public static String getSimpleName(String fullName) {
423-
//key:alias -> alias; key:alias[] -> alias[]
424-
int index = fullName == null ? -1 : fullName.indexOf(":");
425-
if (index >= 0) {
426-
fullName = fullName.substring(index + 1);
447+
public static String formatKey(String fullName, boolean formatAt, boolean formatColon, boolean formatHyphen, boolean firstCase) {
448+
if (fullName == null) {
449+
Log.w(TAG, "formatKey fullName == null >> return null;");
450+
return null;
451+
}
452+
453+
if (formatAt) { //关键词只去掉前缀,不格式化单词,例如 @a-b 返回 a-b ,最后不会调用 setter
454+
fullName = formatAt(fullName);
455+
}
456+
if (formatColon) {
457+
fullName = formatColon(fullName);
458+
}
459+
if (formatHyphen) {
460+
fullName = formatHyphen(fullName, firstCase);
427461
}
428-
return fullName;
462+
463+
return firstCase ? StringUtil.firstCase(fullName) : fullName; //不格式化普通 key:value (value 不为 [], {}) 的 key
464+
}
465+
466+
/**"@key" => "key"
467+
* @param key
468+
* @return
469+
*/
470+
public static String formatAt(@NotNull String key) {
471+
return key.startsWith("@") ? key.substring(1) : key;
472+
}
473+
/**key:alias => alias
474+
* @param key
475+
* @return
476+
*/
477+
public static String formatColon(@NotNull String key) {
478+
int index = key.indexOf(":");
479+
return index < 0 ? key : key.substring(index + 1);
480+
}
481+
482+
/**A-b-cd-Efg => ABCdEfg
483+
* @param key
484+
* @return
485+
*/
486+
public static String formatHyphen(@NotNull String key, boolean firstCase) {
487+
boolean first = true;
488+
int index;
489+
490+
String name = "";
491+
String part;
492+
do {
493+
index = key.indexOf("-");
494+
part = index < 0 ? key : key.substring(0, index);
495+
496+
name += firstCase && first == false ? StringUtil.firstCase(part, true) : part;
497+
key = key.substring(index + 1);
498+
499+
first = false;
500+
}
501+
while (index >= 0);
502+
503+
return name;
429504
}
430505

431506

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Structure.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ private static void verify(@NotNull String tk, @NotNull Object tv, @NotNull JSON
528528
if (tk.endsWith("$")) { //搜索
529529
sqlVerify("$", real, tk, tv, creator);
530530
}
531-
else if (tk.endsWith("?")) { //正则表达式
531+
else if (tk.endsWith("~") || tk.endsWith("?")) { //TODO 正则表达式, 以后可能取消支持 ? 作为 正则匹配 的功能符
532532
logic = new Logic(tk.substring(0, tk.length() - 1));
533533
rk = logic.getKey();
534534
rv = real.get(rk);

0 commit comments

Comments
 (0)