Updated version; added string-based methods in JsonHandler
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 3 Jul 2019 21:02:36 +0000 (23:02 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 3 Jul 2019 21:02:36 +0000 (23:02 +0200)
net.mograsim.logic.ui.am2900/src/net/mograsim/logic/ui/examples/JsonExample.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/CodeSnippetSupplier.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/standardComponentIDMapping.json
net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/standardSnippetIDMapping.json [new file with mode: 0644]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java

index 128ec05..c0ebad6 100644 (file)
@@ -2,8 +2,6 @@ package net.mograsim.logic.ui.examples;
 
 import java.io.IOException;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.google.gson.JsonNull;
 
 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
@@ -19,6 +17,7 @@ import net.mograsim.logic.ui.model.wires.GUIWire;
 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 import net.mograsim.logic.ui.serializing.SubmodelComponentDeserializer;
 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
+import net.mograsim.logic.ui.util.JsonHandler;
 
 public class JsonExample
 {
@@ -49,11 +48,11 @@ public class JsonExample
                GUI_rsLatch comp = new GUI_rsLatch(viewModel);
                comp.moveTo(30, 0);
                SubmodelComponentParams params = comp.calculateParams();
-               String jsonString = new GsonBuilder().setPrettyPrinting().create().toJson(params);
+               String jsonString = JsonHandler.toJson(params);
                System.out.println(jsonString);
-               SubmodelComponent deserialized = SubmodelComponentDeserializer.create(viewModel,
-                               new Gson().fromJson(jsonString, SubmodelComponentParams.class));
-               deserialized.moveTo(30, 50);
+               SubmodelComponentParams paramsD = JsonHandler.fromJson(jsonString, SubmodelComponentParams.class);
+               SubmodelComponent componentD = SubmodelComponentDeserializer.create(viewModel, paramsD);
+               componentD.moveTo(30, 50);
                double h = 0;
                for (String s : comp.getInputPinNames())
                {
@@ -62,7 +61,7 @@ public class JsonExample
                        new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
                        sw = new GUIManualSwitch(viewModel);
                        sw.moveTo(0, h + 50);
-                       new GUIWire(viewModel, sw.getOutputPin(), deserialized.getPin(s));
+                       new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
                        h += 20;
                }
                h = 0;
@@ -73,7 +72,7 @@ public class JsonExample
                        new GUIWire(viewModel, bd.getInputPin(), comp.getPin(s));
                        bd = new GUIBitDisplay(viewModel);
                        bd.moveTo(80, h + 50);
-                       new GUIWire(viewModel, bd.getInputPin(), deserialized.getPin(s));
+                       new GUIWire(viewModel, bd.getInputPin(), componentD.getPin(s));
                        h += 20;
                }
        }
index abd0e73..743d843 100644 (file)
@@ -49,7 +49,7 @@ public class CodeSnippetSupplier
 
        static
        {
-               try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./mapping.json"))
+               try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardSnippetIDMapping.json"))
                {
                        if (s == null)
                                throw new IOException("Resource not found");
index 6c7a835..a594273 100644 (file)
@@ -1,4 +1,4 @@
-mograsim version: 0.1.2
+mograsim version: 0.1.3
 {
   "GUIAm2901": "file:components/am2901/GUIAm2901.json",
   "GUIAm2901ALUFuncDecode": "file:components/am2901/GUIAm2901ALUFuncDecode.json",
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/standardSnippetIDMapping.json b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/standardSnippetIDMapping.json
new file mode 100644 (file)
index 0000000..8224951
--- /dev/null
@@ -0,0 +1,3 @@
+mograsim version: 0.1.3
+{
+}
\ No newline at end of file
index 99ff8d1..df48266 100644 (file)
@@ -6,6 +6,7 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.stream.Collectors;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
@@ -29,18 +30,27 @@ public class JsonHandler
        {
                try (InputStreamReader reader = new InputStreamReader(input); BufferedReader bf = new BufferedReader(reader))
                {
-                       String json = bf.lines().dropWhile(s -> s.length() == 0 || s.charAt(0) != '{').reduce("", (x, y) -> x.concat(y));
-                       T params = parser.fromJson(json, type);
-                       return params;
+                       return fromJson(bf.lines().collect(Collectors.joining("\n")), type);
                }
        }
 
+       public static <T> T fromJson(String src, Class<T> type)
+       {
+               // TODO actually parse and compare version
+               String rawJson = src.lines().dropWhile(s -> s.length() == 0 || s.charAt(0) != '{').collect(Collectors.joining());
+               return parser.fromJson(rawJson, type);
+       }
+
        public static void writeJson(Object o, String path) throws IOException
        {
                try (FileWriter writer = new FileWriter(path))
                {
-                       writer.write(String.format("mograsim version: %s\n", Version.jsonCompVersion.toString()));
-                       writer.write(parser.toJson(o));
+                       writer.write(toJson(o));
                }
        }
+
+       public static String toJson(Object o)
+       {
+               return String.format("mograsim version: %s\n%s", Version.jsonCompVersion.toString(), parser.toJson(o));
+       }
 }
index 8e80249..aa84568 100644 (file)
@@ -2,7 +2,7 @@ package net.mograsim.logic.ui.util;
 
 public final class Version
 {
-       public final static Version jsonCompVersion = new Version(0, 1, 2);
+       public final static Version jsonCompVersion = new Version(0, 1, 3);
        public final int major, minor, patch;
 
        public Version(int major, int minor, int patch)