From 2843ff9fdf4e5af2d72f71ae4e616453bb141a61 Mon Sep 17 00:00:00 2001 From: Pepich Date: Wed, 1 Feb 2017 16:03:26 +0100 Subject: [PATCH] Initial commit --- src/com/redstoner/misc/JsonManager.java | 109 ++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/com/redstoner/misc/JsonManager.java diff --git a/src/com/redstoner/misc/JsonManager.java b/src/com/redstoner/misc/JsonManager.java new file mode 100644 index 0000000..be7da92 --- /dev/null +++ b/src/com/redstoner/misc/JsonManager.java @@ -0,0 +1,109 @@ +package com.redstoner.misc; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.redstoner.annotations.Version; + +/** This class provides simple JSON handling, like storing and loading from and to files. + * + * @author Pepich */ +@Version(major = 1, minor = 0, revision = 0, compatible = -1) +public class JsonManager +{ + private JsonManager() + {} + + /** Loads a JSONObject from a file. + * + * @param source the file to load from. + * @return the JSONObject or null if the source does not contain a valid JSONObject. */ + public static JSONObject getObject(File source) + { + if (!source.exists()) + return null; + JSONParser parser = new JSONParser(); + try + { + FileReader reader = new FileReader(source); + Object rawObject = parser.parse(reader); + reader.close(); + JSONObject jsonObject = (JSONObject) rawObject; + return jsonObject; + } + catch (IOException | ParseException e) + {} + return null; + } + + /** Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself. + * + * @param object the JSONObject to save. + * @param destination the file to write to. */ + public static void save(JSONObject object, File destination) + { + if (destination.exists()) + destination.delete(); + else if (!destination.getParentFile().exists()) + destination.getParentFile().mkdirs(); + try + { + destination.createNewFile(); + FileWriter writer = new FileWriter(destination); + object.writeJSONString(writer); + writer.flush(); + writer.close(); + } + catch (IOException e) + {} + } + + /** Loads a JSONArray from a file. + * + * @param source the file to load from. + * @return the JSONArray or null if the source does not contain a valid JSONArray. */ + public static JSONArray getArray(File source) + { + if (!source.exists()) + return null; + JSONParser parser = new JSONParser(); + try + { + Object rawObject = parser.parse(new FileReader(source)); + JSONArray jsonArray = (JSONArray) rawObject; + return jsonArray; + } + catch (IOException | ParseException e) + {} + return null; + } + + /** Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself. + * + * @param object the JSONArray to save. + * @param destination the file to write to. */ + public static void save(JSONArray array, File destination) + { + if (destination.exists()) + destination.delete(); + else if (!destination.getParentFile().exists()) + destination.getParentFile().mkdirs(); + try + { + destination.createNewFile(); + FileWriter writer = new FileWriter(destination); + array.writeJSONString(writer); + writer.flush(); + writer.close(); + } + catch (IOException e) + {} + } +}