|
@@ -12,43 +12,76 @@ import org.bukkit.plugin.Plugin;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
-
|
|
|
|
|
|
+/**
|
|
|
|
+ * A simple class for serializing/deserializing objects using Gson and managing
|
|
|
|
+ * its file locations. The object class must be serializable
|
|
|
|
+ *
|
|
|
|
+ * @author MasterCake
|
|
|
|
+ *
|
|
|
|
+ */
|
|
public class JSONConfig {
|
|
public class JSONConfig {
|
|
- File data;
|
|
|
|
- Type type;
|
|
|
|
- Object obj;
|
|
|
|
-
|
|
|
|
- public JSONConfig(Object obj, Type type, Plugin pl){
|
|
|
|
- this.data = new File(pl.getDataFolder().getPath() + "/" + "data.json");
|
|
|
|
- this.type = type;
|
|
|
|
|
|
+ private File dataFile;
|
|
|
|
+ private Type typeToken;
|
|
|
|
+ private Object objectToSerialize;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Creates a new JSONConfig
|
|
|
|
+ *
|
|
|
|
+ * @param type type of the object to store
|
|
|
|
+ * @param objectToSerialize the actual object to store
|
|
|
|
+ * @param parentPlugin the plugin instance calling this method
|
|
|
|
+ */
|
|
|
|
+ public JSONConfig(Type type, Object objectToSerialize, Plugin parentPlugin) {
|
|
|
|
+ this.dataFile = new File(parentPlugin.getDataFolder().getPath() + "/" + "data.json");
|
|
|
|
+ this.typeToken = type;
|
|
|
|
+ this.objectToSerialize = objectToSerialize;
|
|
}
|
|
}
|
|
- public String getJson(Object obj){
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns the JSON string representing the object
|
|
|
|
+ *
|
|
|
|
+ * @param obj
|
|
|
|
+ * @return a JSON string
|
|
|
|
+ */
|
|
|
|
+ public String getJson() {
|
|
Gson gson = new Gson();
|
|
Gson gson = new Gson();
|
|
- return gson.toJson(obj, type);
|
|
|
|
|
|
+ return gson.toJson(objectToSerialize, typeToken);
|
|
}
|
|
}
|
|
- public long saveToDisk(Object obj){
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Saves the object to disk, using the given file {@link #dataFile}
|
|
|
|
+ *
|
|
|
|
+ * @return the final file size in bytes
|
|
|
|
+ */
|
|
|
|
+ public long saveToDisk() {
|
|
Gson gson = new Gson();
|
|
Gson gson = new Gson();
|
|
try {
|
|
try {
|
|
- FileOutputStream fout= new FileOutputStream (data);
|
|
|
|
|
|
+ FileOutputStream fout = new FileOutputStream(dataFile);
|
|
ObjectOutputStream oos = new ObjectOutputStream(fout);
|
|
ObjectOutputStream oos = new ObjectOutputStream(fout);
|
|
-
|
|
|
|
- oos.writeObject(gson.toJson(obj, type));
|
|
|
|
|
|
+
|
|
|
|
+ oos.writeObject(gson.toJson(objectToSerialize, typeToken));
|
|
fout.close();
|
|
fout.close();
|
|
oos.close();
|
|
oos.close();
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
- return data.length();
|
|
|
|
|
|
+ return dataFile.length();
|
|
}
|
|
}
|
|
- public Object get(){
|
|
|
|
- Object c = null;
|
|
|
|
- if(data.exists()){
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Reads the JSON string from the file and serializes it to an object
|
|
|
|
+ *
|
|
|
|
+ * @return the object
|
|
|
|
+ */
|
|
|
|
+ public Object getObject() {
|
|
|
|
+ Object c = null;
|
|
|
|
+ if (dataFile.exists()) {
|
|
try {
|
|
try {
|
|
- FileInputStream fin = new FileInputStream(data);
|
|
|
|
|
|
+ FileInputStream fin = new FileInputStream(dataFile);
|
|
ObjectInputStream ois = new ObjectInputStream(fin);
|
|
ObjectInputStream ois = new ObjectInputStream(fin);
|
|
Gson gson = new Gson();
|
|
Gson gson = new Gson();
|
|
- c = gson.fromJson((String) ois.readObject(), type);
|
|
|
|
|
|
+ c = gson.fromJson((String) ois.readObject(), typeToken);
|
|
ois.close();
|
|
ois.close();
|
|
fin.close();
|
|
fin.close();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -56,7 +89,7 @@ public class JSONConfig {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return c;
|
|
return c;
|
|
- }else{
|
|
|
|
|
|
+ } else {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|