Pārlūkot izejas kodu

Avoid converting the json to string

Fabio Battaglia 5 gadi atpakaļ
vecāks
revīzija
8b9793ea6e

+ 10 - 11
src/main/java/info/hkzlab/dupal/analyzer/board/boardio/DuPALAnalyzer.java

@@ -1,9 +1,9 @@
 package info.hkzlab.dupal.analyzer.board.boardio;
 
-import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
-import java.nio.charset.StandardCharsets;
 
+import org.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -75,7 +75,7 @@ public class DuPALAnalyzer {
     public void startAnalisys() throws Exception {
         int board_revision = dpci.getBoardVersion();
         DuPALCmdInterface.DuPAL_LED led;
-        String formatterOutput = null;
+        JSONObject formatterOutput = null;
        
         switch(dpci.palSpecs.slotNumber()) {
             default:
@@ -116,18 +116,17 @@ public class DuPALAnalyzer {
         }
     }
 
-    private void saveOutputToFile(String destination, String out) throws IOException {
-        FileOutputStream fout = null;
-        
+    private void saveOutputToFile(String destination, JSONObject output) throws IOException {
         logger.info("saveOutputToFile() -> Saving to " + destination);
 
         try {
-            fout = new FileOutputStream(outFile);
+            FileWriter filew = new FileWriter(destination);
+
+            output.write(filew);
+
+            filew.flush();
+            filew.close();
 
-            fout.write(out.getBytes(StandardCharsets.US_ASCII));
-            
-            fout.flush();
-            fout.close();
         } catch(IOException e) {
             logger.error("Error printing out the registered outputs table (not including outputs).");
             throw e;

+ 4 - 14
src/main/java/info/hkzlab/dupal/analyzer/palanalisys/formatter/JSONFormatter.java

@@ -12,11 +12,7 @@ public class JSONFormatter {
 
     private JSONFormatter() {};
    
-    public static String formatJSON(PALSpecs pSpecs, SimpleState[] states) {
-        return formatJSON(pSpecs, states, false);
-    }
-
-    public static String formatJSON(PALSpecs pSpecs, SimpleState[] states, boolean indent) {
+    public static JSONObject formatJSON(PALSpecs pSpecs, SimpleState[] states) {
         JSONObject rootObject = new JSONObject();
         
         // Fill in the header and insert it into the root object
@@ -29,15 +25,10 @@ public class JSONFormatter {
 
         rootObject.put("states", ssArray);
 
-        if(indent) return rootObject.toString(4);
-        else return rootObject.toString();
-    }
-
-    public static String formatJSON(PALSpecs pSpecs, int ioAsOutMask, OutState[] states) {
-        return formatJSON(pSpecs, ioAsOutMask, states, false);
+        return rootObject;
     }
 
-    public static String formatJSON(PALSpecs pSpecs, int ioAsOutMask, OutState[] states, boolean indent) {
+    public static JSONObject formatJSON(PALSpecs pSpecs, int ioAsOutMask, OutState[] states) {
         JSONObject rootObject = new JSONObject();
         JSONArray osLinks = new JSONArray();
         JSONArray regLinks = new JSONArray();
@@ -56,8 +47,7 @@ public class JSONFormatter {
         rootObject.put("oLinks", osLinks);
         rootObject.put("rLinks", regLinks);
 
-        if(indent) return rootObject.toString(4);
-        else return rootObject.toString();
+        return rootObject;
     }
 
     private static JSONObject buildHeader(PALSpecs pSpecs) {

+ 6 - 2
src/test/java/info/hkzlab/dupal/analyzer/JSONFormatterTest.java

@@ -2,7 +2,9 @@ package info.hkzlab.dupal.analyzer;
 
 import static org.junit.Assert.*;
 
+import java.io.StringWriter;
 
+import org.json.JSONObject;
 import org.junit.Test;
 
 import info.hkzlab.dupal.analyzer.devices.PAL16L8Specs;
@@ -42,8 +44,10 @@ public class JSONFormatterTest {
         OutState[] oStates = new OutState[] {os_a, os_b, os_c, os_d, os_e};
 
         String expectedJson = "{\"oLinks\":[{\"inputs\":16,\"destination\":{\"outputs\":0,\"hiz\":0},\"source\":{\"outputs\":0,\"hiz\":0}},{\"inputs\":32,\"destination\":{\"outputs\":1,\"hiz\":0},\"source\":{\"outputs\":0,\"hiz\":0}},{\"inputs\":48,\"destination\":{\"outputs\":2,\"hiz\":0},\"source\":{\"outputs\":0,\"hiz\":0}},{\"inputs\":16,\"destination\":{\"outputs\":0,\"hiz\":0},\"source\":{\"outputs\":1,\"hiz\":0}},{\"inputs\":32,\"destination\":{\"outputs\":4,\"hiz\":0},\"source\":{\"outputs\":1,\"hiz\":0}},{\"inputs\":48,\"destination\":{\"outputs\":3,\"hiz\":0},\"source\":{\"outputs\":1,\"hiz\":0}},{\"inputs\":16,\"destination\":{\"outputs\":0,\"hiz\":0},\"source\":{\"outputs\":2,\"hiz\":0}},{\"inputs\":32,\"destination\":{\"outputs\":1,\"hiz\":0},\"source\":{\"outputs\":2,\"hiz\":0}},{\"inputs\":48,\"destination\":{\"outputs\":3,\"hiz\":0},\"source\":{\"outputs\":2,\"hiz\":0}},{\"inputs\":16,\"destination\":{\"outputs\":2,\"hiz\":0},\"source\":{\"outputs\":3,\"hiz\":0}},{\"inputs\":32,\"destination\":{\"outputs\":1,\"hiz\":0},\"source\":{\"outputs\":3,\"hiz\":0}},{\"inputs\":48,\"destination\":{\"outputs\":4,\"hiz\":0},\"source\":{\"outputs\":3,\"hiz\":0}},{\"inputs\":16,\"destination\":{\"outputs\":0,\"hiz\":0},\"source\":{\"outputs\":4,\"hiz\":0}},{\"inputs\":32,\"destination\":{\"outputs\":3,\"hiz\":0},\"source\":{\"outputs\":4,\"hiz\":0}},{\"inputs\":32,\"destination\":{\"outputs\":4,\"hiz\":0},\"source\":{\"outputs\":4,\"hiz\":0}}],\"header\":{\"analyzerProgram\":\"DuPAL Analyzer\",\"PAL\":{\"IOsAsOUT\":63,\"type\":\"PAL16L8\"},\"revision\":1},\"rLinks\":[]}";
-        String json = JSONFormatter.formatJSON(new PAL16L8Specs(), 0x3F, oStates, false);
+        JSONObject jobj = JSONFormatter.formatJSON(new PAL16L8Specs(), 0x3F, oStates);
+        StringWriter strw = new StringWriter();
+        jobj.write(strw);
 
-        assertEquals("JSONFormatter should create correct JSON", expectedJson, json);
+        assertEquals("JSONFormatter should create correct JSON", expectedJson, strw.toString());
     }
 }