1717
1818import java .util .Map ;
1919
20+ import org .json .JSONObject ;
21+
2022import com .yanglb .utilitys .codegen .core .model .TableModel ;
2123import com .yanglb .utilitys .codegen .core .translator .BaseMsgTranslator ;
2224import com .yanglb .utilitys .codegen .exceptions .CodeGenException ;
@@ -34,75 +36,37 @@ protected void onBeforeTranslate() throws CodeGenException {
3436 @ Override
3537 protected void onTranslate () throws CodeGenException {
3638 super .onTranslate ();
39+ JSONObject json = new JSONObject ();
3740 StringBuilder sb = this .writableModel .getData ();
38- sb .append ("{\r \n " );
39-
40- boolean hasDot = false ;
4141
4242 // 分组输出
4343 if (this .paramaModel .getOptions ().get ("group" ) != null ) {
4444 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 ;
7050
71- sb . append ( " }, \r \n " );
51+ sub . put ( id , value );
7252 }
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 );
7855 }
7956 } else {
8057 // 合并输出
8158 for (TableModel tblModel : this .model ) {
8259 for (Map <String , String > itm : tblModel .toList ()) {
8360 String id = itm .get ("id" );
61+ String value = itm .get (this .msgLang );
8462 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 );
8865 }
8966 }
90-
91- int idx = sb .lastIndexOf ("," );
92- if (idx != -1 ) {
93- sb .deleteCharAt (idx );
94- }
9567 }
96- sb .append ("}" );
97- }
98-
99- private String convert2JsCode (String value ) {
100- if (value == null ) return null ;
10168
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 ));
10771 }
10872}
0 commit comments