Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.dori_mon.json;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.HashMap;
- import java.util.Map;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonArray;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonIOException;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonSyntaxException;
- public class JsonFile {
- private File file;
- private JsonObject json;
- private Map<String, JsonElement> defaults = new HashMap<>();
- Gson gson;
- /**
- * Creates a new JsonFile and sets it up (see {@link JsonFile#setup()}.
- * @param fileName the name of the file.
- */
- public JsonFile(final String fileName) {
- this("", fileName);
- }
- /**
- * Creates a new JsonFile and sets it up (see {@link JsonFile#setup()}.
- * @param path the path of the file.
- * @param fileName the name of the file.
- */
- public JsonFile(final String path, final String fileName) {
- this(path, fileName, new GsonBuilder().disableHtmlEscaping().setPrettyPrinting());
- }
- /**
- * Creates a new JsonFile and sets it up (see {@link JsonFile#setup()}.
- * @param path the path of the file.
- * @param fileName the name of the file.
- * @param gsonBuilder a gsonBuilder to use for formatting.
- */
- public JsonFile(final String path, final String fileName, final GsonBuilder gsonBuilder) {
- this.gson = gsonBuilder.create();
- if (!path.equals("")) {
- File pathFile = new File(path);
- if (!pathFile.exists())
- pathFile.mkdirs();
- }
- String finalFileName = new String(fileName);
- if (!fileName.endsWith(".json"))
- finalFileName = finalFileName + ".json";
- if (!path.equals(""))
- file = new File(path, finalFileName);
- else
- file = new File(finalFileName);
- if (!file.exists())
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- return;
- }
- setup();
- }
- /**
- * Saves the edited JsonObject to the File.
- */
- public void save() {
- try {
- Writer writer = new FileWriter(file);
- String parsedJson = gson.toJson(json);
- writer.write(parsedJson);
- writer.flush();
- writer.close();
- } catch (IOException e) {
- System.err.println("Could not save JsonFile named " + file.getName() + "!");
- e.printStackTrace();
- return;
- }
- }
- /**
- * Sets up the JsonFile, parses and handles defaults.
- */
- public void setup() {
- try {
- JsonElement element = gson.fromJson(new FileReader(file), JsonElement.class);
- if (element == null)
- json = new JsonObject();
- else
- json = element.getAsJsonObject();
- } catch (JsonSyntaxException | JsonIOException | FileNotFoundException e) {
- json = new JsonObject();
- }
- if (json == null)
- json = new JsonObject();
- copyDefaults();
- }
- /**
- * Sets a key to a value in the JsonFile.
- * @param key the key.
- * @param value the value.
- */
- public void set(String key, Object value) {
- if (containsKey(key))
- json.remove(key);
- json.add(key, gson.toJsonTree(value));
- save();
- }
- /**
- * Gets a Value as JsonElement from the JsonFile.
- * @param key the key.
- * @return the value.
- */
- public JsonElement get(String key) {
- return json.get(key);
- }
- /**
- * Gets a Value as String from the JsonFile.
- * @param key the key.
- * @return the value.
- */
- public String getAsString(String key) {
- return get(key).getAsString();
- }
- /**
- * Gets a Value as Boolean from the JsonFile.
- * @param key the key.
- * @return the value.
- */
- public boolean getAsBoolean(String key) {
- return get(key).getAsBoolean();
- }
- /**
- * Gets a Value as Integer from the JsonFile.
- * @param key the key.
- * @return the value.
- */
- public int getAsInt(String key) {
- return get(key).getAsInt();
- }
- /**
- * Gets a Value as Double from the JsonFile.
- * @param key the key.
- * @return the value.
- */
- public double getAsDouble(String key) {
- return get(key).getAsDouble();
- }
- /**
- * Gets a Value as Long from the JsonFile.
- * @param key the key.
- * @return the value.
- */
- public long getAsLong(String key) {
- return get(key).getAsLong();
- }
- /**
- * Gets a Value as JsonObject from the JsonFile.
- * @param key the key.
- * @return the value.
- */
- public JsonObject getAsJsonObject(String key) {
- return get(key).getAsJsonObject();
- }
- /**
- * Gets a Value as JsonArray from the JsonFile.
- * @param key the key.
- * @return the value.
- */
- public JsonArray getAsJsonArray(String key) {
- return get(key).getAsJsonArray();
- }
- /**
- * Checks if the file exists.
- * @return true if it exists, false if not.
- */
- public boolean exists() {
- return file.exists();
- }
- /**
- * Checks if the JsonFile contains a key.
- * @param key the key.
- * @return true if it exists, false if not.
- */
- public boolean containsKey(String key) {
- return json.has(key);
- }
- /**
- * Adds a default to the defaults Map.
- * @param key the key of the default.
- * @param value the value of the default.
- */
- public void addDefault(String key, Object value) {
- defaults.put(key, gson.toJsonTree(value));
- }
- /**
- * Copies all defaults from the defaults Map to the JsonFile.
- */
- public void copyDefaults() {
- for (String key : defaults.keySet())
- if (!json.has(key))
- json.add(key, defaults.get(key));
- save();
- }
- /**
- * deletes the JsonFile.
- */
- public void delete() {
- file.delete();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement