diff --git a/src/main/java/com/vectron/fcl/FclStack.java b/src/main/java/com/vectron/fcl/FclStack.java index 49af113..628822b 100644 --- a/src/main/java/com/vectron/fcl/FclStack.java +++ b/src/main/java/com/vectron/fcl/FclStack.java @@ -1,36 +1,18 @@ package com.vectron.fcl; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.stream.JsonReader; import com.vectron.fcl.types.Obj; -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import static java.util.Arrays.asList; public class FclStack { - private static final Gson gson; private final LStack stack = new LStack<>(); - static { - FclTypeAdapter typeAdapter = new FclTypeAdapter(); - gson = new GsonBuilder() - .registerTypeAdapter(Obj.class, typeAdapter) - .setLenient() - .serializeSpecialFloatingPointValues() - .create(); - typeAdapter.setGSon(gson); - } - public void push(Obj obj) { stack.push(obj); } @@ -80,36 +62,22 @@ public class FclStack { } public void load(FileStore fileStore, String id) { - FileInputStream stream = null; - try { - stream = fileStore.open(fileName(id)); - Obj[] loaded = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class); - stack.clear(); - stack.addAll(asList(loaded)); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - if (stream != null) { - try { - stream.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } + Obj[] loaded = JsonSerializer.load(fileStore, fileName(id)); + stack.clear(); + stack.addAll(asList(loaded)); } public void load(JsonReader jsonReader) { stack.clear(); - stack.addAll(asList(gson.fromJson(jsonReader, Obj[].class))); + stack.addAll(asList(JsonSerializer.fromJson(jsonReader))); } public void save(FileStore fileStore, String id) { - fileStore.save(gson.toJson(stack.toArray(new Obj[0])).getBytes(), fileName(id)); + fileStore.save(JsonSerializer.toJson(stack).getBytes(), fileName(id)); } public JsonElement toJsonTree() { - return gson.toJsonTree(stack.toArray(new Obj[0])); + return JsonSerializer.toJsonTree(stack); } private String fileName(String id) { diff --git a/src/main/java/com/vectron/fcl/JsonSerializer.java b/src/main/java/com/vectron/fcl/JsonSerializer.java new file mode 100644 index 0000000..4825de0 --- /dev/null +++ b/src/main/java/com/vectron/fcl/JsonSerializer.java @@ -0,0 +1,71 @@ +package com.vectron.fcl; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.stream.JsonReader; +import com.vectron.fcl.types.Obj; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.Type; +import java.util.Map; + +public class JsonSerializer { + private static final Gson gson; + + static { + FclTypeAdapter typeAdapter = new FclTypeAdapter(); + gson = new GsonBuilder() + .registerTypeAdapter(Obj.class, typeAdapter) + .setLenient() + .serializeSpecialFloatingPointValues() + .create(); + typeAdapter.setGSon(gson); + } + + public static Obj[] load(FileStore fileStore, String fileName) { + FileInputStream stream = null; + try { + stream = fileStore.open(fileName); + return gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + if (stream != null) { + try { + stream.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + } + + public static JsonElement toJsonTree(LStack stack) { + return gson.toJsonTree(stack.toArray(new Obj[0])); + } + + public static JsonElement toJsonTree(Obj obj) { + return gson.toJsonTree(obj, Obj.class); + } + + public static String toJson(LStack stack) { + return gson.toJson(stack.toArray(new Obj[0])); + } + + public static String toJson(JsonObject json) { + return gson.toJson(json); + } + + public static Map fromJson(String str, Type type) { + return gson.fromJson(str, type); + } + + public static Obj[] fromJson(JsonReader jsonReader) { + return gson.fromJson(jsonReader, Obj[].class); + } +}