|
20 | 20 | import com.alibaba.fastjson.JSONArray; |
21 | 21 | import com.alibaba.fastjson.JSONObject; |
22 | 22 |
|
| 23 | +import zuo.biao.apijson.server.NotNull; |
| 24 | + |
23 | 25 | /**parser for response |
24 | 26 | * @author Lemon |
25 | 27 | * @see #getObject |
@@ -64,7 +66,7 @@ public JSONResponse(JSONObject object) { |
64 | 66 | public static final String MSG_SUCCEED = "success"; //成功 |
65 | 67 | public static final String MSG_SERVER_ERROR = "Internal Server Error!"; //服务器内部错误 |
66 | 68 |
|
67 | | - |
| 69 | + |
68 | 70 | public static final String KEY_CODE = "code"; |
69 | 71 | public static final String KEY_MSG = "msg"; |
70 | 72 | public static final String KEY_COUNT = "count"; |
@@ -197,14 +199,14 @@ public JSONResponse getJSONResponse(String key) { |
197 | 199 | return getObject(key, JSONResponse.class); |
198 | 200 | } |
199 | 201 | //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 | + // } |
208 | 210 | //状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
209 | 211 |
|
210 | 212 |
|
@@ -235,7 +237,7 @@ public <T> T getObject(String key, Class<T> clazz) { |
235 | 237 | * @return |
236 | 238 | */ |
237 | 239 | 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); |
239 | 241 | } |
240 | 242 |
|
241 | 243 | /** |
@@ -291,7 +293,7 @@ public static <T> List<T> getList(JSONObject object, Class<T> clazz) { |
291 | 293 | * @return |
292 | 294 | */ |
293 | 295 | 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); |
295 | 297 | } |
296 | 298 |
|
297 | 299 | /** |
@@ -322,7 +324,7 @@ public static JSONArray getArray(JSONObject object) { |
322 | 324 | * @return |
323 | 325 | */ |
324 | 326 | 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)); |
326 | 328 | } |
327 | 329 |
|
328 | 330 |
|
@@ -352,13 +354,13 @@ public static JSONObject format(final JSONObject object) { |
352 | 354 | value = object.get(key); |
353 | 355 |
|
354 | 356 | if (value instanceof JSONArray) {//JSONArray,遍历来format内部项 |
355 | | - formatedObject.put(replaceArray(key), format((JSONArray) value)); |
| 357 | + formatedObject.put(formatArrayKey(key), format((JSONArray) value)); |
356 | 358 | } |
357 | 359 | else if (value instanceof JSONObject) {//JSONObject,往下一级提取 |
358 | | - formatedObject.put(getSimpleName(key), format((JSONObject) value)); |
| 360 | + formatedObject.put(formatObjectKey(key), format((JSONObject) value)); |
359 | 361 | } |
360 | 362 | else {//其它Object,直接填充 |
361 | | - formatedObject.put(getSimpleName(key), value); |
| 363 | + formatedObject.put(formatOtherKey(key), value); |
362 | 364 | } |
363 | 365 | } |
364 | 366 | } |
@@ -397,35 +399,108 @@ else if (value instanceof JSONObject) {//JSONObject,往下一级提取 |
397 | 399 | return formatedArray; |
398 | 400 | } |
399 | 401 |
|
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 |
403 | 405 | */ |
404 | | - public static String replaceArray(String key) { |
| 406 | + public static String formatArrayKey(String key) { |
405 | 407 | 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"); |
407 | 409 | } |
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[] ,一般都符合变量命名规范 |
409 | 416 | } |
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 |
413 | 421 | */ |
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 ,一般都符合变量命名规范 |
416 | 429 | } |
417 | 430 |
|
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 | + /**格式化名称 |
419 | 440 | * @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 |
420 | 445 | * @return name => name; name:alias => alias |
421 | 446 | */ |
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); |
427 | 461 | } |
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; |
429 | 504 | } |
430 | 505 |
|
431 | 506 |
|
|
0 commit comments