MissingIdentifier/src/main/java/IO/JSON.java

47 lines
1.5 KiB
Java

package IO;
import com.cedarsoftware.util.io.JsonWriter;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.slf4j.LoggerFactory;
import java.io.*;
/*******************************************************************************************************************
* Author: Ansgar [Hiajen]
* Usage: Load and Write given JSON data to static location
* Credits: t.me/Wursteintopf
* state: Done
*******************************************************************************************************************/
public class JSON {
private static final String PATH = "";
public static JSONArray loadJson(String filename){
LoggerFactory.getLogger(JSON.class).info("Open JSON: " + filename);
try {
JSONParser parser = new JSONParser();
return (JSONArray) parser.parse(new BufferedReader(new FileReader(PATH + filename)));
} catch (Exception e) {
LoggerFactory.getLogger(JSON.class).error(e.getMessage());
return new JSONArray();
}
}
public static void saveJson(JSONArray json, String filename){
LoggerFactory.getLogger(JSON.class).info("Save JSON: " + filename);
try (BufferedWriter file = new BufferedWriter(new FileWriter(PATH + filename))) {
file.write(JsonWriter.formatJson(json.toJSONString()));
file.flush();
} catch (IOException e) {
LoggerFactory.getLogger(JSON.class).error(e.getMessage());
}
}
}