diff options
Diffstat (limited to 'src/main/java/com/redstoner/misc/JsonManager.java')
-rw-r--r-- | src/main/java/com/redstoner/misc/JsonManager.java | 165 |
1 files changed, 80 insertions, 85 deletions
diff --git a/src/main/java/com/redstoner/misc/JsonManager.java b/src/main/java/com/redstoner/misc/JsonManager.java index 13b51b6..498933f 100644 --- a/src/main/java/com/redstoner/misc/JsonManager.java +++ b/src/main/java/com/redstoner/misc/JsonManager.java @@ -1,149 +1,144 @@ package com.redstoner.misc; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; - +import com.redstoner.annotations.Version; 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; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; -/** This class provides simple JSON handling, like storing and loading from and to files. - * - * @author Pepich */ -@Version(major = 1, minor = 0, revision = 2, compatible = -1) -public class JsonManager -{ - private JsonManager() - {} - - /** Loads a JSONObject from a file. - * +/** + * This class provides simple JSON handling, like storing and loading from and to files. + * + * @author Pepich + */ +@Version (major = 1, minor = 0, revision = 2, 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) - { + * + * @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); + try { + FileReader reader = new FileReader(source); + Object rawObject = parser.parse(reader); reader.close(); JSONObject jsonObject = (JSONObject) rawObject; return jsonObject; + } catch (IOException | ParseException e) { } - catch (IOException | ParseException e) - {} return null; } - - /** Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.</br> + + /** + * Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.</br> * Note that this operation will be run on a different thread and you do not need to take care of that yourself. - * - * @param object the JSONObject to save. - * @param destination the file to write to. */ - public static void save(JSONObject object, File destination) - { - Thread t = new Thread(new Runnable() - { + * + * @param object the JSONObject to save. + * @param destination the file to write to. + */ + public static void save(JSONObject object, File destination) { + Thread t = new Thread(new Runnable() { @Override - public void run() - { + public void run() { saveSync(object, destination); } }); t.start(); } - - /** Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.</br> + + /** + * Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.</br> * Note that this operation will be run on the same thread that you are calling it from! - * - * @param object the JSONObject to save. - * @param destination the file to write to. */ - public static void saveSync(JSONObject object, File destination) - { + * + * @param object the JSONObject to save. + * @param destination the file to write to. + */ + public static void saveSync(JSONObject object, File destination) { if (destination.exists()) destination.delete(); else if (!destination.getParentFile().exists()) destination.getParentFile().mkdirs(); - try - { + try { destination.createNewFile(); - FileWriter writer = new FileWriter(destination); - String json_string = object.toJSONString(); + FileWriter writer = new FileWriter(destination); + String json_string = object.toJSONString(); writer.write(json_string); writer.flush(); writer.close(); + } catch (IOException e) { } - catch (IOException e) - {} } - - /** Loads a JSONArray from a file. - * + + /** + * 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) - { + * + * @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)); + try { + Object rawObject = parser.parse(new FileReader(source)); JSONArray jsonArray = (JSONArray) rawObject; return jsonArray; + } catch (IOException | ParseException e) { } - catch (IOException | ParseException e) - {} return null; } - - /** Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.</br> + + /** + * Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.</br> * Note that this operation will be run on a different thread and you do not need to take care of that yourself. - * - * @param object the JSONArray to save. - * @param destination the file to write to. */ - public static void save(JSONArray array, File destination) - { - Thread t = new Thread(new Runnable() - { + * + * @param object the JSONArray to save. + * @param destination the file to write to. + */ + public static void save(JSONArray array, File destination) { + Thread t = new Thread(new Runnable() { @Override - public void run() - { + public void run() { saveSync(array, destination); } }); t.start(); } - - /** Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.</br> + + /** + * Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.</br> * Note that this operation will be run on the same thread that you are calling it from! - * - * @param object the JSONArray to save. - * @param destination the file to write to. */ - public static void saveSync(JSONArray array, File destination) - { + * + * @param object the JSONArray to save. + * @param destination the file to write to. + */ + public static void saveSync(JSONArray array, File destination) { if (destination.exists()) destination.delete(); else if (!destination.getParentFile().exists()) destination.getParentFile().mkdirs(); - try - { + try { destination.createNewFile(); - FileWriter writer = new FileWriter(destination); - String json_string = array.toJSONString(); + FileWriter writer = new FileWriter(destination); + String json_string = array.toJSONString(); writer.write(json_string); writer.flush(); writer.close(); + } catch (IOException e) { } - catch (IOException e) - {} } } |