1515 */
1616package com .yanglb .codegen .core .translator .impl ;
1717
18+ import com .google .gson .FormattingStyle ;
19+ import com .google .gson .Gson ;
20+ import com .google .gson .GsonBuilder ;
21+ import com .google .gson .JsonObject ;
1822import com .yanglb .codegen .core .translator .BaseMsgTranslator ;
1923import com .yanglb .codegen .exceptions .CodeGenException ;
2024import com .yanglb .codegen .model .TableModel ;
2327
2428import java .util .Map ;
2529
26- import org .json .JSONObject ;
27-
2830
2931public class MsgJsonTranslatorImpl extends BaseMsgTranslator {
3032 @ Override
@@ -33,20 +35,20 @@ protected void onBeforeTranslate() throws CodeGenException {
3335 this .writableModel .get (0 ).setExtension ("json" );
3436 }
3537
36- private void tblModel2Json (JSONObject json , TableModel tblModel ) {
38+ private void tblModel2Json (JsonObject json , TableModel tblModel ) {
3739 for (Map <String , String > itm : tblModel .toList ()) {
3840 String id = itm .get ("id" );
3941 String value = itm .get (this .msgLang );
4042 if (StringUtil .isNullOrEmpty (id )) continue ;
4143
42- json .put (id , value );
44+ json .addProperty (id , value );
4345 }
4446 }
4547
4648 @ Override
4749 protected void onTranslate (WritableModel writableModel ) throws CodeGenException {
4850 super .onTranslate (writableModel );
49- JSONObject json = new JSONObject ();
51+ JsonObject json = new JsonObject ();
5052 StringBuilder sb = writableModel .getData ();
5153
5254 if (this .parameterModel .getOptions ().hasOption ("combine" )) {
@@ -57,17 +59,18 @@ protected void onTranslate(WritableModel writableModel) throws CodeGenException
5759 } else {
5860 // 分组输出
5961 for (TableModel tblModel : this .model ) {
60- JSONObject sub = new JSONObject ();
62+ JsonObject sub = new JsonObject ();
6163 tblModel2Json (sub , tblModel );
6264
6365 String sheetName = tblModel .getSheetName ();
64- json .put (sheetName , sub );
66+ json .add (sheetName , sub );
6567 }
6668 }
6769
6870 // to JSON string
69- int indentFactor = 4 ;
70- if (parameterModel .getOptions ().hasOption ("minify" )) indentFactor = 0 ;
71- sb .append (json .toString (indentFactor ));
71+ FormattingStyle formattingStyle = FormattingStyle .PRETTY ;
72+ if (parameterModel .getOptions ().hasOption ("minify" )) formattingStyle = FormattingStyle .COMPACT ;
73+ Gson gson = new GsonBuilder ().setFormattingStyle (formattingStyle ).create ();
74+ sb .append (gson .toJson (json ));
7275 }
7376}
0 commit comments