This commit is contained in:
zeroflag 2021-08-17 15:56:49 +02:00
parent 88b2b00678
commit e6c8f36760
2 changed files with 77 additions and 38 deletions

View file

@ -1,36 +1,18 @@
package com.vectron.fcl; package com.vectron.fcl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.vectron.fcl.types.Obj; 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.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
public class FclStack { public class FclStack {
private static final Gson gson;
private final LStack<Obj> stack = new LStack<>(); private final LStack<Obj> 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) { public void push(Obj obj) {
stack.push(obj); stack.push(obj);
} }
@ -80,36 +62,22 @@ public class FclStack {
} }
public void load(FileStore fileStore, String id) { public void load(FileStore fileStore, String id) {
FileInputStream stream = null; Obj[] loaded = JsonSerializer.load(fileStore, fileName(id));
try {
stream = fileStore.open(fileName(id));
Obj[] loaded = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class);
stack.clear(); stack.clear();
stack.addAll(asList(loaded)); 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);
}
}
}
} }
public void load(JsonReader jsonReader) { public void load(JsonReader jsonReader) {
stack.clear(); stack.clear();
stack.addAll(asList(gson.fromJson(jsonReader, Obj[].class))); stack.addAll(asList(JsonSerializer.fromJson(jsonReader)));
} }
public void save(FileStore fileStore, String id) { 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() { public JsonElement toJsonTree() {
return gson.toJsonTree(stack.toArray(new Obj[0])); return JsonSerializer.toJsonTree(stack);
} }
private String fileName(String id) { private String fileName(String id) {

View file

@ -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<Obj> 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<Obj> stack) {
return gson.toJson(stack.toArray(new Obj[0]));
}
public static String toJson(JsonObject json) {
return gson.toJson(json);
}
public static Map<String, Obj> fromJson(String str, Type type) {
return gson.fromJson(str, type);
}
public static Obj[] fromJson(JsonReader jsonReader) {
return gson.fromJson(jsonReader, Obj[].class);
}
}