GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — APIJSON/src/main/java/apijson/JSONResponse.java at main · APIJSON/APIJSON Expand file tree/ Copy pathJSONResponse.javaMore file actions executable file ·665 lines (604 loc) · 19.7 KB / Copy pathJSONResponse.javaFile metadata and controlsexecutable file ·665 lines (604 loc) · 19.7 KB 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 /*Copyright (C) 2026 the APIJSON group. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.util.*; /**parser for response * @author Lemon * @see #getObject * @see #getList * @use JSONResponse response = new JSONResponse(json); * <br> User user = response.getObject(User.class);//not a must * <br> List<Comment> commenntList = response.getList("Comment[]", Comment.class);//not a must */ public interface JSONResponse<M extends Map<String, Object>, L extends List<Object>> extends JSONMap<M, L> { static final String TAG = "JSONResponse"; // 节约性能和减少 bug,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试 /**格式化带 - 中横线的单词 */ public static boolean IS_FORMAT_HYPHEN = false; /**格式化带 _ 下划线的单词 */ public static boolean IS_FORMAT_UNDERLINE = false; /**格式化带 $ 美元符的单词 */ public static boolean IS_FORMAT_DOLLAR = false; //default JSONResponse() { // super(); //} //default JSONResponse(Object json) { // this(parseObject(json)); //} //default JSONResponse(Object json, JSONParser<M, L> parser) { // this(parseObject(json, parser)); //} //default JSONResponse(Map<String, Object> object) { // super(format(object)); //} //default JSONResponse(M object, JSONCreator<M, L> creator) { // super(format(object, creator)); //} //状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final int CODE_SUCCESS = 200; //成功 public static final int CODE_UNSUPPORTED_ENCODING = 400; //编码错误 public static final int CODE_ILLEGAL_ACCESS = 401; //权限错误 public static final int CODE_UNSUPPORTED_OPERATION = 403; //禁止操作 public static final int CODE_NOT_FOUND = 404; //未找到 public static final int CODE_ILLEGAL_ARGUMENT = 406; //参数错误 public static final int CODE_NOT_LOGGED_IN = 407; //未登录 public static final int CODE_TIME_OUT = 408; //超时 public static final int CODE_CONFLICT = 409; //重复,已存在 public static final int CODE_CONDITION_ERROR = 412; //条件错误,如密码错误 public static final int CODE_UNSUPPORTED_TYPE = 415; //类型错误 public static final int CODE_OUT_OF_RANGE = 416; //超出范围 public static final int CODE_NULL_POINTER = 417; //对象为空 public static final int CODE_SERVER_ERROR = 500; //服务器内部错误 public static final String MSG_SUCCEED = "success"; //成功 public static final String MSG_SERVER_ERROR = "Internal Server Error!"; //服务器内部错误 public static String KEY_OK = "ok"; public static String KEY_CODE = "code"; public static String KEY_MSG = "msg"; public static final String KEY_COUNT = "count"; public static final String KEY_TOTAL = "total"; public static final String KEY_INFO = "info"; //详细的分页信息 public static final String KEY_FIRST = "first"; //是否为首页 public static final String KEY_LAST = "last"; //是否为尾页 public static final String KEY_MAX = "max"; //最大页码 public static final String KEY_MORE = "more"; //是否有更多 /**获取状态 * @return */ default int getCode() { try { return JSON.getIntValue(this, KEY_CODE); } catch (Exception e) { //empty } return 0; } /**获取状态 * @return */ public static int getCode(Map<String, Object> reponse) { try { return JSON.getIntValue(reponse, KEY_CODE); } catch (Exception e) { //empty } return 0; } /**获取状态描述 * @return */ default String getMsg() { return JSON.getString(this, KEY_MSG); } /**获取状态描述 * @param response * @return */ public static String getMsg(Map<String, Object> response) { return response == null ? null : JSON.getString(response, KEY_MSG); } /**获取id * @return */ default long getId() { try { return JSON.getLongValue(this, getIdKey()); } catch (Exception e) { //empty } return 0; } /**获取数量 * @return */ default int getCount() { try { return JSON.getIntValue(this, KEY_COUNT); } catch (Exception e) { //empty } return 0; } /**获取总数 * @return */ default int getTotal() { try { return JSON.getIntValue(this, KEY_TOTAL); } catch (Exception e) { //empty } return 0; } /**是否成功 * @return */ default boolean isSuccess() { return isSuccess(getCode()); } /**是否成功 * @param code * @return */ public static boolean isSuccess(int code) { return code == CODE_SUCCESS; } /**是否成功 * @param response * @return */ public static boolean isSuccess(JSONResponse<?, ?> response) { return response != null && response.isSuccess(); } /**是否成功 * @param response * @return */ public static boolean isSuccess(Map<String, Object> response) { return response != null && isSuccess(JSON.getIntValue(response, KEY_CODE)); } /**校验服务端是否存在table * @return */ default boolean isExist() { return isExist(getCount()); } /**校验服务端是否存在table * @param count * @return */ public static boolean isExist(int count) { return count > 0; } /**校验服务端是否存在table * @param response * @return */ public static boolean isExist(JSONResponse<?, ?> response) { return response != null && response.isExist(); } public static boolean isExist(Map<String, Object> response) { return response != null && isExist(JSON.getIntValue(response, KEY_COUNT)); } /**获取内部的JSONResponse * @param key * @return */ default JSONResponse<M, L> getJSONResponse(String key) { return getObject(key, JSONResponse.class); } //cannot get javaBeanDeserizer // /**获取内部的JSONResponse // * @param response // * @param key // * @return // */ // public static JSONResponse getJSONResponse(JSONRequest response, String key) { // return response == null ? null : response.getObject(key, JSONResponse.class); // } //状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> /** * key = clazz.getSimpleName() * @param clazz * @return */ default <T> T getObject(Class<T> clazz) { return getObject(clazz == null ? "" : clazz.getSimpleName(), clazz); } /** * @param key * @param clazz * @return */ default <T> T getObject(String key, Class<T> clazz) { return getObject(this, key, clazz); } /** * @param object * @param key * @param clazz * @return */ public static <T> T getObject( Map<String, Object> object, String key, Class<T> clazz) { return toObject(object == null ? null : JSON.get(object, formatObjectKey(key)), clazz); } /** * @param clazz * @return */ default <T> T toObject(Class<T> clazz) { return toObject(this, clazz); } /** * @param object * @param clazz * @return */ public static <T, M extends Map<String, Object>, L extends List<Object>> T toObject( Map<String, Object> object, Class<T> clazz) { return JSON.parseObject(object, clazz); } /** * arrayObject = this * @param key * @return */ default <T> List<T> getList(String key) { return JSON.getList(this, key); } /** * key = KEY_ARRAY * @param object * @return */ public static <T> List<T> getList(Map<String, Object> object) { return JSON.getList(object, KEY_ARRAY); } /** * @param object * @param key * @param clazz * @return */ public static <T, M extends Map<String, Object>> List<T> getList(Map<String, Object> object, String key, Class<T> clazz) { return object == null ? null : JSON.parseArray(JSON.getString(object, formatArrayKey(key)), clazz); } /** * key = KEY_ARRAY * @return */ default <L extends List<Object>> L getArray() { return getArray(KEY_ARRAY); } /** * @param key * @return */ default <L extends List<Object>> L getArray(String key) { return getArray(this, key); } /** * @param object * @return */ public static <L extends List<Object>> L getArray(Map<String, Object> object) { return getArray(object, KEY_ARRAY); } /** * key = KEY_ARRAY * @param object * @param key * @return */ public static <L extends List<Object>> L getArray(Map<String, Object> object, String key) { return object == null ? null : JSON.get(object, formatArrayKey(key)); } // /** // * @return // */ // default JSONRequest format() { // return format(this); // } /**格式化key名称 * @param object * @return */ public static <M extends Map<String, Object>, L extends List<Object>> M format(final M object) { //太长查看不方便,不如debug Log.i(TAG, "format object = \n" + JSON.toJSONString(object)); if (object == null || object.isEmpty()) { Log.i(TAG, "format object == null || object.isEmpty() >> return object;"); return object; } M formatedObject = JSON.createJSONObject(); Set<String> set = object.keySet(); if (set != null) { Object value; for (String key : set) { value = object.get(key); if (value instanceof List<?>) {//JSONList,遍历来format内部项 formatedObject.put(formatArrayKey(key), format((L) value)); } else if (value instanceof Map<?, ?>) {//JSONRequest,往下一级提取 formatedObject.put(formatObjectKey(key), format((M) value)); } else {//其它Object,直接填充 formatedObject.put(formatOtherKey(key), value); } } } //太长查看不方便,不如debug Log.i(TAG, "format return formatedObject = " + JSON.toJSONString(formatedObject)); return formatedObject; } /**格式化key名称 * @param array * @return */ public static <M extends Map<String, Object>, L extends List<Object>> L format(final L array) { //太长查看不方便,不如debug Log.i(TAG, "format array = \n" + JSON.toJSONString(array)); if (array == null || array.isEmpty()) { Log.i(TAG, "format array == null || array.isEmpty() >> return array;"); return array; } L formattedArray = JSON.createJSONArray(); Object value; for (int i = 0; i < array.size(); i++) { value = array.get(i); if (value instanceof List<?>) {//JSONList,遍历来format内部项 formattedArray.add(format((L) value)); } else if (value instanceof Map<?, ?>) {//JSONRequest,往下一级提取 formattedArray.add(format((M) value)); } else {//其它Object,直接填充 formattedArray.add(value); } } //太长查看不方便,不如debug Log.i(TAG, "format return formattedArray = " + JSON.toJSONString(formattedArray)); return formattedArray; } /**获取表名称 * @param fullName name 或 name:alias * @return name => name; name:alias => alias */ public static String getTableName(String fullName) { //key:alias -> alias; key:alias[] -> alias[] int index = fullName == null ? -1 : fullName.indexOf(":"); return index < 0 ? fullName : fullName.substring(0, index); } /**获取变量名 * @param fullName * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, Boolean)} formatColon = true, formatAt = true, formatHyphen = true, firstCase = true */ public static String getVariableName(String fullName) { if (JSONMap.isArrayKey(fullName)) { fullName = StringUtil.addSuffix(fullName.substring(0, fullName.length() - 2), "list"); } return formatKey(fullName, true, true, true, true, false, true); } /**格式化数组的名称 key[] => keyList; key:alias[] => aliasList; Table-column[] => tableColumnList * @param key empty ? "list" : key + "List" 且首字母小写 * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, Boolean)} formatColon = false, formatAt = true, formatHyphen = true, firstCase = true */ public static String formatArrayKey(String key) { if (JSONMap.isArrayKey(key)) { key = StringUtil.addSuffix(key.substring(0, key.length() - 2), "list"); } int index = key == null ? -1 : key.indexOf(":"); if (index >= 0) { return key.substring(index + 1); //不处理自定义的 } return formatKey(key, false, true, true, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR, false); //节约性能,除了数组对象 Table-column:alias[] ,一般都符合变量命名规范 } /**格式化对象的名称 name => name; name:alias => alias * @param key name 或 name:alias * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, Boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = true */ public static String formatObjectKey(String key) { int index = key == null ? -1 : key.indexOf(":"); if (index >= 0) { return key.substring(index + 1); // 不处理自定义的 } return formatKey(key, false, true, IS_FORMAT_HYPHEN, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR, false); //节约性能,除了表对象 Table:alias ,一般都符合变量命名规范 } /**格式化普通值的名称 name => name; name:alias => alias * @param fullName name 或 name:alias * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, Boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = false */ public static String formatOtherKey(String fullName) { return formatKey(fullName, false, true, IS_FORMAT_HYPHEN, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR , IS_FORMAT_HYPHEN || IS_FORMAT_UNDERLINE || IS_FORMAT_DOLLAR ? false : null); } /**格式化名称 * @param fullName name 或 name:alias * @param formatAt 去除前缀 @ , @a => a * @param formatColon 去除分隔符 : , A:b => b * @param formatHyphen 去除分隔符 - , A-b-cd-Efg => aBCdEfg * @param formatUnderline 去除分隔符 _ , A_b_cd_Efg => aBCdEfg * @param formatDollar 去除分隔符 $ , A$b$cd$Efg => aBCdEfg * @param firstCase 第一个单词首字母小写,后面的首字母大写, Ab => ab ; A-b-Cd => aBCd * @return name => name; name:alias => alias */ public static String formatKey(String fullName, boolean formatColon, boolean formatAt, boolean formatHyphen , boolean formatUnderline, boolean formatDollar, Boolean firstCase) { if (fullName == null) { Log.w(TAG, "formatKey fullName == null >> return null;"); return null; } if (formatColon) { fullName = formatColon(fullName); } if (formatAt) { //关键词只去掉前缀,不格式化单词,例如 @a-b 返回 a-b ,最后不会调用 setter fullName = formatAt(fullName); } if (formatHyphen && fullName.contains("-")) { fullName = formatHyphen(fullName, true); } if (formatUnderline && fullName.contains("_")) { fullName = formatUnderline(fullName, true); } if (formatDollar && fullName.contains("$")) { fullName = formatDollar(fullName, true); } // 默认不格式化普通 key:value (value 不为 [], {}) 的 key return firstCase == null ? fullName : StringUtil.firstCase(fullName, firstCase); } /**"@key" => "key" * @param key * @return */ public static String formatAt(@NotNull String key) { return key.startsWith("@") ? key.substring(1) : key; } /**key:alias => alias * @param key * @return */ public static String formatColon(@NotNull String key) { int index = key.indexOf(":"); return index < 0 ? key : key.substring(index + 1); } /**A-b-cd-Efg => ABCdEfg * @param key * @return */ public static String formatHyphen(@NotNull String key) { return StringUtil.firstCase(formatHyphen(key, true), false); } /**A-b-cd-Efg => ABCdEfg * @param key * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatHyphen(@NotNull String key, Boolean firstCase) { return formatHyphen(key, firstCase, false); } /**A-b-cd-Efg => ABCdEfg * @param key * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatHyphen(@NotNull String key, Boolean firstCase, Boolean otherCase) { return formatDivider(key, "-", firstCase, otherCase); } /**A_b_cd_Efg => ABCdEfg * @param key * @return */ public static String formatUnderline(@NotNull String key) { return StringUtil.firstCase(formatUnderline(key, true), false); } /**A_b_cd_Efg => ABCdEfg * @param key * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatUnderline(@NotNull String key, Boolean firstCase) { return formatUnderline(key, firstCase, false); } /**A_b_cd_Efg => ABCdEfg * @param key * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatUnderline(@NotNull String key, Boolean firstCase, Boolean otherCase) { return formatDivider(key, "_", firstCase, otherCase); } /**A$b$cd$Efg => ABCdEfg * @param key * @return */ public static String formatDollar(@NotNull String key) { return StringUtil.firstCase(formatDollar(key, true), false); } /**A$b$cd$Efg => ABCdEfg * @param key * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatDollar(@NotNull String key, Boolean firstCase) { return formatDollar(key, firstCase, false); } /**A$b$cd$Efg => ABCdEfg * @param key * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatDollar(@NotNull String key, Boolean firstCase, Boolean otherCase) { return formatDivider(key, "$", firstCase, otherCase); } /**A.b.cd.Efg => ABCdEfg * @param key * @return */ public static String formatDot(@NotNull String key) { return StringUtil.firstCase(formatDot(key, true), false); } /**A.b.cd.Efg => ABCdEfg * @param key * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatDot(@NotNull String key, Boolean firstCase) { return formatDot(key, firstCase, false); } /**A.b.cd.Efg => ABCdEfg * @param key * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatDot(@NotNull String key, Boolean firstCase, Boolean otherCase) { return formatDivider(key, ".", firstCase, otherCase); } /**A/b/cd/Efg => ABCdEfg * @param key * @return */ public static String formatDivider(@NotNull String key, Boolean firstCase) { return formatDivider(key, "/", firstCase); } /**去除分割符,返回驼峰格式 * @param key * @param divider * @return */ public static String formatDivider(@NotNull String key, @NotNull String divider) { return StringUtil.firstCase(formatDivider(key, divider, true), false); } /**去除分割符,返回驼峰格式 * @param key * @param divider * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatDivider(@NotNull String key, @NotNull String divider, Boolean firstCase) { return formatDivider(key, divider, firstCase, false); } /**去除分割符,返回驼峰格式 * @param key * @param divider * @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理 * @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理 * @return */ public static String formatDivider(@NotNull String key, @NotNull String divider, Boolean firstCase, Boolean otherCase) { String[] parts = StringUtil.split(key, divider); StringBuilder name = new StringBuilder(); for (String part : parts) { if (otherCase != null) { part = otherCase ? part.toUpperCase() : part.toLowerCase(); } if (firstCase != null) { part = StringUtil.firstCase(part, firstCase); } name.append(part); } return name.toString(); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |