mirror of
https://github.com/zeroflag/fcl.git
synced 2025-01-11 20:01:10 +01:00
sto/rcl
This commit is contained in:
parent
88b2b00678
commit
e6c8f36760
2 changed files with 77 additions and 38 deletions
|
@ -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<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) {
|
||||
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);
|
||||
Obj[] loaded = JsonSerializer.load(fileStore, fileName(id));
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
71
src/main/java/com/vectron/fcl/JsonSerializer.java
Normal file
71
src/main/java/com/vectron/fcl/JsonSerializer.java
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue