2525import java .util .List ;
2626import java .util .Map ;
2727import java .util .Map .Entry ;
28+ import java .util .Objects ;
2829import java .util .Set ;
2930import java .util .regex .Matcher ;
3031import java .util .regex .Pattern ;
3738 *
3839 * 功能:读取Json文件并在outputPath目录生成相应的JavaBean文件,直接Copy使用,注释可选(直过CheckStyle)。
3940 *
41+ * 支持属性大小写、支持属性大小写同时兼容(Gson2.4级以上)
42+ *
4043 * 持续更新地址:https://github.com/zxiaofan/JavaUtils
4144 *
42- * Note:暂不支持多线程并发.
45+ * Note:暂不支持多线程并发;将待转换json存入D:\\json.txt",main函数直接运行即可 .
4346 *
4447 * @author zxiaofan
4548 */
@@ -62,9 +65,9 @@ public class JsonToJavaBean {
6265 private static String beanRootName = "Root" ;
6366
6467 /**
65- * 是否为字段添加@SerializedName("Upper ")注解,字段大写(默认false) .
68+ * 是否为字段添加@SerializedName("")注解:1、属性小写(默认);2、属性大写(value = "AddName");3、大小写兼容(value = "addName", alternate = {"AddName"}) .
6669 */
67- private static boolean serializedNameUpper = false ;
70+ private static Integer serializedName = 1 ;
6871
6972 /**
7073 * 是否替换get、Set方法的字段中的特殊字符(默认开启).
@@ -211,9 +214,14 @@ private static void createJavaModel() {
211214 builder .append (vo .getFieldDesc ());
212215 builder .append (desc2 );
213216 }
214- if (serializedNameUpper ) {
217+ if (Objects . equals ( 2 , serializedName ) ) {
215218 builder .append ("@SerializedName(\" " ).append (vo .getFieldNameUpper ()).append ("\" )" ).append (rn );
216219 builder .append (tab );
220+ } else if (Objects .equals (3 , serializedName )) {
221+ // @SerializedName(value = "addName", alternate = {"AddName"})
222+ builder .append ("@SerializedName(value = \" " ).append (vo .getFieldNameLower ()).append ("\" , " );
223+ builder .append ("alternate = {\" " ).append (vo .getFieldNameUpper ()).append ("\" })" ).append (rn );
224+ builder .append (tab );
217225 }
218226 builder .append ("private " ).append (vo .getFieldType ()).append (" " ).append (vo .getFieldNameLower ()).append (";" ).append (rn );
219227 if (vo .getFieldType ().startsWith (ObjType .BigDecimal )) {
@@ -233,7 +241,7 @@ private static void createJavaModel() {
233241 hasImport = true ;
234242 }
235243 }
236- if (serializedNameUpper ) {
244+ if (! Objects . equals ( 1 , serializedName ) ) {
237245 if (hasImport ) {
238246 listJar [5 ] = rn ;
239247 }
@@ -305,8 +313,6 @@ private static void createJavaModel() {
305313 /**
306314 * 格式化Bean中字段值.
307315 *
308- * @param fieldVos
309- * fieldVos
310316 */
311317 private static void formatBean () {
312318 for (List <Bean > fieldVos : fields .values ()) {
@@ -401,6 +407,8 @@ private static void buildOrignBean(String json, List<Bean> beans, String classNa
401407 *
402408 * @param json
403409 * josn
410+ * @param type
411+ * type
404412 * @return Object
405413 */
406414 private static Object fromJson (String json , Class type ) {
@@ -416,8 +424,15 @@ private static Object fromJson(String json, Class type) {
416424 }
417425
418426 /**
419- * 构建特殊匹配规则 .
427+ * 构建并特殊匹配规则 .
420428 *
429+ * @param fieldName
430+ * 属性名
431+ * @param fieldType
432+ * 属性类型
433+ * @param listRule
434+ * 规则列表
435+ * @return 特殊属性
421436 */
422437 private static String matchRule (String fieldName , String fieldType , List <String []> listRule ) {
423438 if (!ismachRule ) {
@@ -505,6 +520,7 @@ private static String matchRule(String fieldName, String fieldType, List<String[
505520 * @param matchRule
506521 * 自定义规则
507522 * @param listRule
523+ * 规则列表
508524 */
509525 public static void buildRule (String matchRule , List <String []> listRule ) {
510526 if (matchRule .length () != 0 && listRule .isEmpty ()) {
@@ -515,11 +531,19 @@ public static void buildRule(String matchRule, List<String[]> listRule) {
515531 }
516532 }
517533
518- private static String readTextFile (String sFileName , String sEncode ) {
519- StringBuffer sbStr = new StringBuffer ();
520- try {
521- File ff = new File (sFileName );
522- InputStreamReader read = new InputStreamReader (new FileInputStream (ff ), sEncode );
534+ /**
535+ * 文件读取.
536+ *
537+ * @param fileName
538+ * 文件名
539+ * @param sencode
540+ * 编码格式
541+ * @return 文本
542+ */
543+ private static String readTextFile (String fileName , String sencode ) {
544+ StringBuilder sbStr = new StringBuilder ();
545+ File ff = new File (fileName );
546+ try (FileInputStream fileInputStream = new FileInputStream (ff ); InputStreamReader read = new InputStreamReader (fileInputStream , sencode );) {
523547 BufferedReader ins = new BufferedReader (read );
524548 String dataLine = "" ;
525549 while (null != (dataLine = ins .readLine ())) {
@@ -536,21 +560,21 @@ private static String readTextFile(String sFileName, String sEncode) {
536560 /**
537561 * 新建文件(夹).
538562 *
539- * @param path
563+ * @param pathT
540564 * 路径
541565 */
542- private static void createFile (String path ) {
543- File file = new File (path );
566+ private static void createFile (String pathT ) {
567+ File file = new File (pathT );
544568 if (!file .exists ()) {
545- if (path .contains ("." )) {
569+ if (pathT .contains ("." )) {
546570 try {
547571 file .createNewFile ();
548572 } catch (IOException e ) {
549573 e .printStackTrace ();
550574 }
551575 } else {
552576 // 递归创建文件夹,保证该路径所有文件夹都被创建
553- createFile (path .substring (0 , path .lastIndexOf ("\\ " )));
577+ createFile (pathT .substring (0 , pathT .lastIndexOf ("\\ " )));
554578 file .mkdir ();
555579 }
556580 }
@@ -605,7 +629,7 @@ private static String caseConversion(String fieldName, boolean toLower) {
605629 return result ;
606630 }
607631
608- private static SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH-mm-ss " );
632+ private static SimpleDateFormat format = new SimpleDateFormat ("yyyyMMddHHmmss " );
609633
610634 private static String typeString = "typeString" ;
611635
@@ -644,16 +668,36 @@ private static String caseConversion(String fieldName, boolean toLower) {
644668 private static String importSerializedNameUpper = "import com.google.gson.annotations.SerializedName;\r \n " ;
645669}
646670
671+ /**
672+ * @author yunhai
673+ *
674+ * Bean实体类
675+ */
647676class Bean {
677+ /**
678+ * 原始属性名.
679+ */
648680 private String fieldName ;
649681
650- private String fieldNameLower ; // 小写开头
682+ /**
683+ * 属性名小写开头.
684+ */
685+ private String fieldNameLower ;
651686
652- private String fieldNameUpper ; // 大写开头
687+ /**
688+ * 属性名大写开头.
689+ */
690+ private String fieldNameUpper ;
653691
654- private String fieldType ; // 类型
692+ /**
693+ * 字段类型.
694+ */
695+ private String fieldType ;
655696
656- private String fieldDesc ; // 描述
697+ /**
698+ * 字段描述.
699+ */
700+ private String fieldDesc ;
657701
658702 /**
659703 * 设置fieldName.
0 commit comments