55
66package apijson ;
77
8- import java .util .List ;
9- import java .util .Set ;
10- import java .util .StringTokenizer ;
11-
128import com .alibaba .fastjson .JSONArray ;
139import com .alibaba .fastjson .JSONObject ;
1410
11+ import java .util .List ;
12+ import java .util .Set ;
13+
1514/**parser for response
1615 * @author Lemon
1716 * @see #getObject
2322public class JSONResponse extends apijson .JSONObject {
2423 private static final long serialVersionUID = 1L ;
2524
25+ // 节约性能和减少 bug,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
26+ /**格式化带 - 中横线的单词
27+ */
28+ public static boolean IS_FORMAT_HYPHEN = false ;
29+ /**格式化带 _ 下划线的单词
30+ */
31+ public static boolean IS_FORMAT_UNDERLINE = false ;
32+ /**格式化带 $ 美元符的单词
33+ */
34+ public static boolean IS_FORMAT_DOLLAR = false ;
35+
2636 private static final String TAG = "JSONResponse" ;
2737
2838 public JSONResponse () {
@@ -206,10 +216,6 @@ public JSONResponse getJSONResponse(String key) {
206216 //状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
207217
208218
209-
210-
211-
212-
213219 /**
214220 * key = clazz.getSimpleName()
215221 * @param clazz
@@ -408,18 +414,18 @@ public static String getTableName(String fullName) {
408414
409415 /**获取变量名
410416 * @param fullName
411- * @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = true, formatAt = true, formatHyphen = true, firstCase = true
417+ * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, boolean )} formatColon = true, formatAt = true, formatHyphen = true, firstCase = true
412418 */
413419 public static String getVariableName (String fullName ) {
414420 if (isArrayKey (fullName )) {
415421 fullName = StringUtil .addSuffix (fullName .substring (0 , fullName .length () - 2 ), "list" );
416422 }
417- return formatKey (fullName , true , true , true , true );
423+ return formatKey (fullName , true , true , true , true , false , true );
418424 }
419425
420426 /**格式化数组的名称 key[] => keyList; key:alias[] => aliasList; Table-column[] => tableColumnList
421427 * @param key empty ? "list" : key + "List" 且首字母小写
422- * @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = true, firstCase = true
428+ * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, boolean )} formatColon = false, formatAt = true, formatHyphen = true, firstCase = true
423429 */
424430 public static String formatArrayKey (String key ) {
425431 if (isArrayKey (key )) {
@@ -430,28 +436,29 @@ public static String formatArrayKey(String key) {
430436 return key .substring (index + 1 ); //不处理自定义的
431437 }
432438
433- return formatKey (key , false , true , true , true ); //节约性能,除了数组对象 Table-column:alias[] ,一般都符合变量命名规范
439+ return formatKey (key , false , true , true , IS_FORMAT_UNDERLINE , IS_FORMAT_DOLLAR , false ); //节约性能,除了数组对象 Table-column:alias[] ,一般都符合变量命名规范
434440 }
435441
436442 /**格式化对象的名称 name => name; name:alias => alias
437443 * @param key name 或 name:alias
438- * @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = true
444+ * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, boolean )} formatColon = false, formatAt = true, formatHyphen = false, firstCase = true
439445 */
440446 public static String formatObjectKey (String key ) {
441447 int index = key == null ? -1 : key .indexOf (":" );
442448 if (index >= 0 ) {
443- return key .substring (index + 1 ); //不处理自定义的
449+ return key .substring (index + 1 ); // 不处理自定义的
444450 }
445451
446- return formatKey (key , false , true , false , true ); //节约性能,除了表对象 Table:alias ,一般都符合变量命名规范
452+ return formatKey (key , false , true , IS_FORMAT_HYPHEN , IS_FORMAT_UNDERLINE , IS_FORMAT_DOLLAR , false ); //节约性能,除了表对象 Table:alias ,一般都符合变量命名规范
447453 }
448454
449455 /**格式化普通值的名称 name => name; name:alias => alias
450456 * @param fullName name 或 name:alias
451- * @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = false
457+ * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean )} formatColon = false, formatAt = true, formatHyphen = false, firstCase = false
452458 */
453459 public static String formatOtherKey (String fullName ) {
454- return formatKey (fullName , false , true , false , false ); //节约性能,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
460+ return formatKey (fullName , false , true , IS_FORMAT_HYPHEN , IS_FORMAT_UNDERLINE , IS_FORMAT_DOLLAR
461+ , IS_FORMAT_HYPHEN || IS_FORMAT_UNDERLINE || IS_FORMAT_DOLLAR ? false : null );
455462 }
456463
457464
@@ -460,10 +467,13 @@ public static String formatOtherKey(String fullName) {
460467 * @param formatAt 去除前缀 @ , @a => a
461468 * @param formatColon 去除分隔符 : , A:b => b
462469 * @param formatHyphen 去除分隔符 - , A-b-cd-Efg => aBCdEfg
470+ * @param formatUnderline 去除分隔符 _ , A_b_cd_Efg => aBCdEfg
471+ * @param formatDollar 去除分隔符 $ , A$b$cd$Efg => aBCdEfg
463472 * @param firstCase 第一个单词首字母小写,后面的首字母大写, Ab => ab ; A-b-Cd => aBCd
464473 * @return name => name; name:alias => alias
465474 */
466- public static String formatKey (String fullName , boolean formatColon , boolean formatAt , boolean formatHyphen , boolean firstCase ) {
475+ public static String formatKey (String fullName , boolean formatColon , boolean formatAt , boolean formatHyphen
476+ , boolean formatUnderline , boolean formatDollar , Boolean firstCase ) {
467477 if (fullName == null ) {
468478 Log .w (TAG , "formatKey fullName == null >> return null;" );
469479 return null ;
@@ -476,10 +486,17 @@ public static String formatKey(String fullName, boolean formatColon, boolean for
476486 fullName = formatAt (fullName );
477487 }
478488 if (formatHyphen ) {
479- fullName = formatHyphen (fullName , firstCase );
489+ fullName = formatHyphen (fullName , firstCase != null );
490+ }
491+ if (formatUnderline ) {
492+ fullName = formatUnderline (fullName , firstCase != null );
493+ }
494+ if (formatDollar ) {
495+ fullName = formatDollar (fullName , firstCase != null );
480496 }
481497
482- return firstCase ? StringUtil .firstCase (fullName ) : fullName ; //不格式化普通 key:value (value 不为 [], {}) 的 key
498+ // 默认不格式化普通 key:value (value 不为 [], {}) 的 key
499+ return firstCase == null ? fullName : StringUtil .firstCase (fullName , firstCase );
483500 }
484501
485502 /**"@key" => "key"
@@ -489,6 +506,7 @@ public static String formatKey(String fullName, boolean formatColon, boolean for
489506 public static String formatAt (@ NotNull String key ) {
490507 return key .startsWith ("@" ) ? key .substring (1 ) : key ;
491508 }
509+
492510 /**key:alias => alias
493511 * @param key
494512 * @return
@@ -502,15 +520,60 @@ public static String formatColon(@NotNull String key) {
502520 * @param key
503521 * @return
504522 */
505- public static String formatHyphen (@ NotNull String key , boolean firstCase ) {
506- String name = "" ;
523+ public static String formatHyphen (@ NotNull String key , Boolean firstCase ) {
524+ return formatDivider (key , "-" , firstCase );
525+ }
526+
527+ /**A_b_cd_Efg => ABCdEfg
528+ * @param key
529+ * @return
530+ */
531+ public static String formatUnderline (@ NotNull String key , Boolean firstCase ) {
532+ return formatDivider (key , "_" , firstCase );
533+ }
507534
508- StringTokenizer parts = new StringTokenizer (key , "-" );
509- name += parts .nextToken ();
510- while (parts .hasMoreTokens ()) {
511- String part = parts .nextToken ();
512- name += firstCase ? StringUtil .firstCase (part , true ) : part ;
535+ /**A$b$cd$Efg => ABCdEfg
536+ * @param key
537+ * @return
538+ */
539+ public static String formatDollar (@ NotNull String key , Boolean firstCase ) {
540+ return formatDivider (key , "$" , firstCase );
541+ }
542+
543+ /**A.b.cd.Efg => ABCdEfg
544+ * @param key
545+ * @return
546+ */
547+ public static String formatDot (@ NotNull String key , Boolean firstCase ) {
548+ return formatDivider (key , "." , firstCase );
549+ }
550+
551+ /**A/b/cd/Efg => ABCdEfg
552+ * @param key
553+ * @return
554+ */
555+ public static String formatDivider (@ NotNull String key , Boolean firstCase ) {
556+ return formatDivider (key , "/" , firstCase );
557+ }
558+
559+ /**去除分割符,返回驼峰格式
560+ * @param key
561+ * @param divider
562+ * @param firstCase
563+ * @return
564+ */
565+ public static String formatDivider (@ NotNull String key , @ NotNull String divider , Boolean firstCase ) {
566+ String [] parts = StringUtil .split (key , divider );
567+ StringBuilder name = new StringBuilder ();
568+ for (String part : parts ) {
569+ part = part .toLowerCase (); // 始终小写,也方便反过来 ABCdEfg -> A_b_cd_Efg
570+ if (firstCase != null ) {
571+ // 始终小写, A_b_cd_Efg -> ABCdEfg, firstCase ? part.toLowerCase() : part.toUpperCase();
572+ part = StringUtil .firstCase (part , firstCase );
573+ }
574+ name .append (part );
513575 }
514- return name ;
576+ return name . toString () ;
515577 }
578+
516579}
0 commit comments