X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Futil%2FJsonHandler.java;h=20491ce58f27ab3c278a06d1d7f1b196da9da1f7;hb=f8985e813a8984430378448b6ba5a3c36d1e0356;hp=9cd9e8cca5d8b41a2e9dc28c9bf8e1a5c0ba078e;hpb=b5d8c2d71e27350ea7c9314e40df5bb0584271cd;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/util/JsonHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/util/JsonHandler.java index 9cd9e8cc..20491ce5 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/util/JsonHandler.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/util/JsonHandler.java @@ -6,14 +6,16 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; public class JsonHandler { - private static Gson parser = new GsonBuilder().setPrettyPrinting().create(); + public final static Gson parser = new GsonBuilder().setPrettyPrinting().create(); public static T readJson(String path, Class type) throws IOException { @@ -28,7 +30,8 @@ public class JsonHandler */ public static T readJson(InputStream input, Class type) throws IOException { - try (InputStreamReader reader = new InputStreamReader(input); BufferedReader bf = new BufferedReader(reader)) + try (InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8); + BufferedReader bf = new BufferedReader(reader)) { return fromJson(bf.lines().collect(Collectors.joining("\n")), type); } @@ -36,11 +39,16 @@ public class JsonHandler public static T fromJson(String src, Class type) { - // TODO actually parse and compare version + // throw away legacy version line String rawJson = src.lines().dropWhile(s -> s.length() == 0 || s.charAt(0) != '{').collect(Collectors.joining()); return parser.fromJson(rawJson, type); } + public static T fromJsonTree(JsonElement src, Class type) + { + return parser.fromJson(src, type); + } + public static void writeJson(Object o, String path) throws IOException { try (FileWriter writer = new FileWriter(path)) @@ -51,6 +59,11 @@ public class JsonHandler public static String toJson(Object o) { - return String.format("mograsim version: %s\n%s", Version.jsonCompVersion.toString(), parser.toJson(o)); + return parser.toJson(o); + } + + public static JsonElement toJsonTree(Object o) + { + return parser.toJsonTree(o); } -} +} \ No newline at end of file