17
17
18
18
import java .util .Map ;
19
19
20
+ import org .json .JSONObject ;
21
+
20
22
import com .yanglb .utilitys .codegen .core .model .TableModel ;
21
23
import com .yanglb .utilitys .codegen .core .translator .BaseMsgTranslator ;
22
24
import com .yanglb .utilitys .codegen .exceptions .CodeGenException ;
@@ -34,75 +36,37 @@ protected void onBeforeTranslate() throws CodeGenException {
34
36
@ Override
35
37
protected void onTranslate () throws CodeGenException {
36
38
super .onTranslate ();
39
+ JSONObject json = new JSONObject ();
37
40
StringBuilder sb = this .writableModel .getData ();
38
- sb .append ("{\r \n " );
39
-
40
- boolean hasDot = false ;
41
41
42
42
// 分组输出
43
43
if (this .paramaModel .getOptions ().get ("group" ) != null ) {
44
44
for (TableModel tblModel : this .model ) {
45
- // 如果没有Sheet名或Sheet名被#注释则添加到Root中
46
- String sheetName = tblModel .getSheetName ();
47
- if (sheetName ==null || sheetName .equals ("" ) || sheetName .startsWith ("@" )) {
48
- for (Map <String , String > itm : tblModel .toList ()) {
49
- String id = itm .get ("id" );
50
- if (StringUtility .isNullOrEmpty (id )) continue ;
51
- // 对字符串进行转换
52
- String value = this .convert2JsCode (itm .get (this .msgLang ));
53
- sb .append (String .format (" \" %\" s: \" %s\" ,\r \n " , id , value ));
54
- }
55
- } else {
56
- hasDot = false ;
57
- sb .append (String .format (" \" %s\" : {\r \n " , tblModel .getSheetName ()));
58
- for (Map <String , String > itm : tblModel .toList ()) {
59
- String id = itm .get ("id" );
60
- if (StringUtility .isNullOrEmpty (id )) continue ;
61
- // 对字符串进行转换
62
- String value = this .convert2JsCode (itm .get (this .msgLang ));
63
- sb .append (String .format (" \" %s\" : \" %s\" ,\r \n " , id , value ));
64
- hasDot = true ;
65
- }
66
- int idx = sb .lastIndexOf ("," );
67
- if (idx != -1 && hasDot ) {
68
- sb .deleteCharAt (idx );
69
- }
45
+ JSONObject sub = new JSONObject ();
46
+ for (Map <String , String > itm : tblModel .toList ()) {
47
+ String id = itm .get ("id" );
48
+ String value = itm .get (this .msgLang );
49
+ if (StringUtility .isNullOrEmpty (id )) continue ;
70
50
71
- sb . append ( " }, \r \n " );
51
+ sub . put ( id , value );
72
52
}
73
- }
74
-
75
- int idx = sb .lastIndexOf ("," );
76
- if (idx != -1 ) {
77
- sb .deleteCharAt (idx );
53
+ String sheetName = tblModel .getSheetName ();
54
+ json .put (sheetName , sub );
78
55
}
79
56
} else {
80
57
// 合并输出
81
58
for (TableModel tblModel : this .model ) {
82
59
for (Map <String , String > itm : tblModel .toList ()) {
83
60
String id = itm .get ("id" );
61
+ String value = itm .get (this .msgLang );
84
62
if (StringUtility .isNullOrEmpty (id )) continue ;
85
- // 对字符串进行转换
86
- String value = this .convert2JsCode (itm .get (this .msgLang ));
87
- sb .append (String .format (" \" %s\" : \" %s\" ,\r \n " , id , value ));
63
+
64
+ json .put (id , value );
88
65
}
89
66
}
90
-
91
- int idx = sb .lastIndexOf ("," );
92
- if (idx != -1 ) {
93
- sb .deleteCharAt (idx );
94
- }
95
67
}
96
- sb .append ("}" );
97
- }
98
-
99
- private String convert2JsCode (String value ) {
100
- if (value == null ) return null ;
101
68
102
- // 先替换\r\n,防止有文档只有\r或\n 后面再替换一次
103
- value = value .replaceAll ("\r \n " , "\\ \\ r\\ \\ n" );
104
- value = value .replaceAll ("\r " , "\\ \\ r\\ \\ n" );
105
- value = value .replaceAll ("\n " , "\\ \\ r\\ \\ n" );
106
- return value ;
69
+ // to JSON string
70
+ sb .append (json .toString (4 ));
107
71
}
108
72
}
0 commit comments