Skip to content

Commit 8997a01

Browse files
author
杨利兵
committed
优化JSON生成功能。
1 parent 3a8afd4 commit 8997a01

File tree

4 files changed

+17
-52
lines changed

4 files changed

+17
-52
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
<classpathentry kind="lib" path="lib/poi-ooxml-schemas-3.11-20141221.jar"/>
1111
<classpathentry kind="lib" path="lib/poi-scratchpad-3.11-20141221.jar"/>
1212
<classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
13+
<classpathentry kind="lib" path="lib/json-20140107.jar" sourcepath="lib/json-20140107-sources.jar"/>
1314
<classpathentry kind="output" path="bin"/>
1415
</classpath>

lib/json-20140107-sources.jar

78.1 KB
Binary file not shown.

lib/json-20140107.jar

63.4 KB
Binary file not shown.

src/com/yanglb/utilitys/codegen/core/translator/impl/MsgJsonTranslatorImpl.java

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.Map;
1919

20+
import org.json.JSONObject;
21+
2022
import com.yanglb.utilitys.codegen.core.model.TableModel;
2123
import com.yanglb.utilitys.codegen.core.translator.BaseMsgTranslator;
2224
import 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

Comments
 (0)