Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8dbaa2d

Browse files
committed
解决 format: true 对应 key 返回不是小驼峰,而是强制小写,例如 User[] 返回不是 userList 而是 userlist
1 parent 3501258 commit 8dbaa2d

File tree

1 file changed

+101
-13
lines changed

1 file changed

+101
-13
lines changed

APIJSONORM/src/main/java/apijson/JSONResponse.java

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,14 @@ public static String formatKey(String fullName, boolean formatColon, boolean for
485485
if (formatAt) { //关键词只去掉前缀,不格式化单词,例如 @a-b 返回 a-b ,最后不会调用 setter
486486
fullName = formatAt(fullName);
487487
}
488-
if (formatHyphen) {
489-
fullName = formatHyphen(fullName, firstCase != null);
488+
if (formatHyphen && fullName.contains("-")) {
489+
fullName = formatHyphen(fullName, true);
490490
}
491-
if (formatUnderline) {
492-
fullName = formatUnderline(fullName, firstCase != null);
491+
if (formatUnderline && fullName.contains("_")) {
492+
fullName = formatUnderline(fullName, true);
493493
}
494-
if (formatDollar) {
495-
fullName = formatDollar(fullName, firstCase != null);
494+
if (formatDollar && fullName.contains("$")) {
495+
fullName = formatDollar(fullName, true);
496496
}
497497

498498
// 默认不格式化普通 key:value (value 不为 [], {}) 的 key
@@ -520,32 +520,100 @@ public static String formatColon(@NotNull String key) {
520520
* @param key
521521
* @return
522522
*/
523+
public static String formatHyphen(@NotNull String key) {
524+
return StringUtil.firstCase(formatHyphen(key, true), false);
525+
}
526+
/**A-b-cd-Efg => ABCdEfg
527+
* @param key
528+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
529+
* @return
530+
*/
523531
public static String formatHyphen(@NotNull String key, Boolean firstCase) {
524-
return formatDivider(key, "-", firstCase);
532+
return formatHyphen(key, firstCase, false);
533+
}
534+
/**A-b-cd-Efg => ABCdEfg
535+
* @param key
536+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
537+
* @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理
538+
* @return
539+
*/
540+
public static String formatHyphen(@NotNull String key, Boolean firstCase, Boolean otherCase) {
541+
return formatDivider(key, "-", firstCase, otherCase);
525542
}
526543

527544
/**A_b_cd_Efg => ABCdEfg
528545
* @param key
529546
* @return
530547
*/
548+
public static String formatUnderline(@NotNull String key) {
549+
return StringUtil.firstCase(formatUnderline(key, true), false);
550+
}
551+
/**A_b_cd_Efg => ABCdEfg
552+
* @param key
553+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
554+
* @return
555+
*/
531556
public static String formatUnderline(@NotNull String key, Boolean firstCase) {
532-
return formatDivider(key, "_", firstCase);
557+
return formatUnderline(key, firstCase, false);
558+
}
559+
/**A_b_cd_Efg => ABCdEfg
560+
* @param key
561+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
562+
* @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理
563+
* @return
564+
*/
565+
public static String formatUnderline(@NotNull String key, Boolean firstCase, Boolean otherCase) {
566+
return formatDivider(key, "_", firstCase, otherCase);
533567
}
534568

535569
/**A$b$cd$Efg => ABCdEfg
536570
* @param key
537571
* @return
538572
*/
573+
public static String formatDollar(@NotNull String key) {
574+
return StringUtil.firstCase(formatDollar(key, true), false);
575+
}
576+
/**A$b$cd$Efg => ABCdEfg
577+
* @param key
578+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
579+
* @return
580+
*/
539581
public static String formatDollar(@NotNull String key, Boolean firstCase) {
540-
return formatDivider(key, "$", firstCase);
582+
return formatDollar(key, firstCase, false);
583+
}
584+
/**A$b$cd$Efg => ABCdEfg
585+
* @param key
586+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
587+
* @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理
588+
* @return
589+
*/
590+
public static String formatDollar(@NotNull String key, Boolean firstCase, Boolean otherCase) {
591+
return formatDivider(key, "$", firstCase, otherCase);
541592
}
542593

543594
/**A.b.cd.Efg => ABCdEfg
544595
* @param key
545596
* @return
546597
*/
598+
public static String formatDot(@NotNull String key) {
599+
return StringUtil.firstCase(formatDot(key, true), false);
600+
}
601+
/**A.b.cd.Efg => ABCdEfg
602+
* @param key
603+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
604+
* @return
605+
*/
547606
public static String formatDot(@NotNull String key, Boolean firstCase) {
548-
return formatDivider(key, ".", firstCase);
607+
return formatDot(key, firstCase, false);
608+
}
609+
/**A.b.cd.Efg => ABCdEfg
610+
* @param key
611+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
612+
* @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理
613+
* @return
614+
*/
615+
public static String formatDot(@NotNull String key, Boolean firstCase, Boolean otherCase) {
616+
return formatDivider(key, ".", firstCase, otherCase);
549617
}
550618

551619
/**A/b/cd/Efg => ABCdEfg
@@ -559,16 +627,36 @@ public static String formatDivider(@NotNull String key, Boolean firstCase) {
559627
/**去除分割符,返回驼峰格式
560628
* @param key
561629
* @param divider
562-
* @param firstCase
630+
* @return
631+
*/
632+
public static String formatDivider(@NotNull String key, @NotNull String divider) {
633+
return StringUtil.firstCase(formatDivider(key, divider, true), false);
634+
}
635+
/**去除分割符,返回驼峰格式
636+
* @param key
637+
* @param divider
638+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
563639
* @return
564640
*/
565641
public static String formatDivider(@NotNull String key, @NotNull String divider, Boolean firstCase) {
642+
return formatDivider(key, divider, firstCase, false);
643+
}
644+
645+
/**去除分割符,返回驼峰格式
646+
* @param key
647+
* @param divider
648+
* @param firstCase 首字符的大小写,true-大写,false-小写,null-不处理
649+
* @param otherCase 非首字符的大小写,true-大写,false-小写,null-不处理
650+
* @return
651+
*/
652+
public static String formatDivider(@NotNull String key, @NotNull String divider, Boolean firstCase, Boolean otherCase) {
566653
String[] parts = StringUtil.split(key, divider);
567654
StringBuilder name = new StringBuilder();
568655
for (String part : parts) {
569-
part = part.toLowerCase(); // 始终小写,也方便反过来 ABCdEfg -> A_b_cd_Efg
656+
if (otherCase != null) {
657+
part = otherCase ? part.toUpperCase() : part.toLowerCase();
658+
}
570659
if (firstCase != null) {
571-
// 始终小写, A_b_cd_Efg -> ABCdEfg, firstCase ? part.toLowerCase() : part.toUpperCase();
572660
part = StringUtil.firstCase(part, firstCase);
573661
}
574662
name.append(part);

0 commit comments

Comments
 (0)