Skip to content

Commit 633340d

Browse files
committed
Server:完善及优化JSONResponse代码
1 parent 2875661 commit 633340d

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/Controller.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,8 @@ public JSONObject postVerify(@RequestBody String request) {
252252
JSONObject verify = null;
253253
try {
254254
verify = response.getJSONObject(VERIFY_);
255-
} catch (Exception e) {
256-
// TODO: handle exception
257-
}
255+
} catch (Exception e) {}
256+
258257
if (verify == null || JSONResponse.isSuccess(verify.getIntValue(JSONResponse.KEY_CODE)) == false) {
259258
new DemoParser(DELETE, true).parseResponse(new JSONRequest(new Verify(type, phone)));
260259
return response;

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

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -399,43 +399,63 @@ else if (value instanceof JSONObject) {//JSONObject,往下一级提取
399399
return formatedArray;
400400
}
401401

402+
403+
/**获取表名称
404+
* @param fullName name 或 name:alias
405+
* @return name => name; name:alias => alias
406+
*/
407+
public static String getTableName(String fullName) {
408+
//key:alias -> alias; key:alias[] -> alias[]
409+
int index = fullName == null ? -1 : fullName.indexOf(":");
410+
return index < 0 ? fullName : fullName.substring(0, index);
411+
}
412+
413+
/**获取变量名
414+
* @param fullName
415+
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatColon = true, formatAt = true, formatHyphen = true, firstCase = true
416+
*/
417+
public static String getVariableName(String fullName) {
418+
return formatKey(fullName, true, true, true, true);
419+
}
420+
402421
/**格式化数组的名称 key[] => keyList; key:alias[] => aliasList; Table-column[] => tableColumnList
403422
* @param key empty ? "list" : key + "List" 且首字母小写
404-
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatAt = false, formatColon = true, formatHyphen = true, firstCase = true
423+
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatColon = false, formatAt = false, formatHyphen = true, firstCase = true
405424
*/
406425
public static String formatArrayKey(String key) {
407426
if (isArrayKey(key)) {
408-
key = StringUtil.addSuffix(key.substring(0, key.lastIndexOf(KEY_ARRAY)), "list");
427+
key = StringUtil.addSuffix(key.substring(0, key.length() - 2), "list");
409428
}
410429
int index = key == null ? -1 : key.indexOf(":");
411430
if (index >= 0) {
412431
return key.substring(index + 1); //不处理自定义的
413432
}
414-
433+
415434
return formatKey(key, false, false, true, true); //节约性能,除了表对象 Table-column:alias[] ,一般都符合变量命名规范
416435
}
417436

418437
/**格式化对象的名称 name => name; name:alias => alias
419438
* @param key name 或 name:alias
420-
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatAt = false, formatColon = true, formatHyphen = false, firstCase = true
439+
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatColon = false, formatAt = false, formatHyphen = false, firstCase = true
421440
*/
422441
public static String formatObjectKey(String key) {
423442
int index = key == null ? -1 : key.indexOf(":");
424443
if (index >= 0) {
425444
return key.substring(index + 1); //不处理自定义的
426445
}
427-
446+
428447
return formatKey(key, false, false, false, true); //节约性能,除了表对象 Table:alias ,一般都符合变量命名规范
429448
}
430449

431450
/**格式化普通值的名称 name => name; name:alias => alias
432451
* @param fullName name 或 name:alias
433-
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatAt = true, formatColon = false, formatHyphen = false, firstCase = false
452+
* @return {@link #formatKey(String, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = false
434453
*/
435454
public static String formatOtherKey(String fullName) {
436-
return formatKey(fullName, true, false, false, false); //节约性能,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
455+
return formatKey(fullName, false, true, false, false); //节约性能,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
437456
}
438-
457+
458+
439459
/**格式化名称
440460
* @param fullName name 或 name:alias
441461
* @param formatAt 去除前缀 @ , @a => a
@@ -444,25 +464,25 @@ public static String formatOtherKey(String fullName) {
444464
* @param firstCase 第一个单词首字母小写,后面的首字母大写, Ab => ab ; A-b-Cd => aBCd
445465
* @return name => name; name:alias => alias
446466
*/
447-
public static String formatKey(String fullName, boolean formatAt, boolean formatColon, boolean formatHyphen, boolean firstCase) {
467+
public static String formatKey(String fullName, boolean formatColon, boolean formatAt, boolean formatHyphen, boolean firstCase) {
448468
if (fullName == null) {
449469
Log.w(TAG, "formatKey fullName == null >> return null;");
450470
return null;
451471
}
452-
453-
if (formatAt) { //关键词只去掉前缀,不格式化单词,例如 @a-b 返回 a-b ,最后不会调用 setter
454-
fullName = formatAt(fullName);
455-
}
472+
456473
if (formatColon) {
457474
fullName = formatColon(fullName);
458475
}
476+
if (formatAt) { //关键词只去掉前缀,不格式化单词,例如 @a-b 返回 a-b ,最后不会调用 setter
477+
fullName = formatAt(fullName);
478+
}
459479
if (formatHyphen) {
460480
fullName = formatHyphen(fullName, firstCase);
461481
}
462482

463483
return firstCase ? StringUtil.firstCase(fullName) : fullName; //不格式化普通 key:value (value 不为 [], {}) 的 key
464484
}
465-
485+
466486
/**"@key" => "key"
467487
* @param key
468488
* @return
@@ -478,15 +498,15 @@ public static String formatColon(@NotNull String key) {
478498
int index = key.indexOf(":");
479499
return index < 0 ? key : key.substring(index + 1);
480500
}
481-
501+
482502
/**A-b-cd-Efg => ABCdEfg
483503
* @param key
484504
* @return
485505
*/
486506
public static String formatHyphen(@NotNull String key, boolean firstCase) {
487507
boolean first = true;
488508
int index;
489-
509+
490510
String name = "";
491511
String part;
492512
do {

0 commit comments

Comments
 (0)