Skip to content

Instantly share code, notes, and snippets.

@Swedz
Created August 11, 2020 10:06
Show Gist options
  • Save Swedz/5357accc133684950ab07c6efe01b3ca to your computer and use it in GitHub Desktop.
Save Swedz/5357accc133684950ab07c6efe01b3ca to your computer and use it in GitHub Desktop.
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
public abstract class AbstractJSONConfig {
private final String path, name;
private final File file;
private JSONObject json;
/**
* Instantiate a new json configuration file.
*
* @param path the path to the file
* @param name the name of the file
*/
public AbstractJSONConfig(String path, String name) {
this.path = path;
this.name = name;
this.file = new File(path + "/" + name + ".json");
}
/**
* Load in the json configuration.
*
* @param override true to force default, false to use existing or load from default if there is none existing
* @return the {@link AbstractJSONConfig}
*/
public AbstractJSONConfig load(boolean override) {
try {
String fullFile = path + "/" + name + ".json";
// Attempt to create the file if we need to
if((file.getParentFile() != null && file.getParentFile().mkdirs()) || file.createNewFile() || override) {
// Create the new file
file.createNewFile();
// Write the default json into the file
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(this.getDefaultJSON().toString(4));
bw.close();
}
// Load in the data from the file
json = this.readJSONFile(new FileInputStream(file));
} catch (Exception ex) {
ex.printStackTrace();
}
return this;
}
/**
* Read in the json file from the input stream.
*
* @param inputStream the {@link FileInputStream}
* @return the {@link JSONObject}
*/
private JSONObject readJSONFile(FileInputStream inputStream) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null)
sb.append(line);
br.close();
if(sb.toString().equals(""))
sb.append("{}");
return new JSONObject(sb.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* Get the path for the config.
*
* @return the path
*/
public String getPath() {
return path;
}
/**
* Get the name for the config.
*
* @return the file name
*/
public String getName() {
return name;
}
/**
* Get the file for the config.
*
* @return the {@link File}
*/
public File getFile() {
return file;
}
/**
* Get the json for the config.
*
* @return the {@link JSONObject}
*/
public JSONObject getJSON() {
return json;
}
/**
* Get the section of the configuration file given the parent section.
*
* @param parent the parent {@link JSONObject} (mainly the first json)
* @param key the key
* @return the {@link JSONObject}; creates empty {@link JSONObject} if it doesn't exist
*/
public JSONObject getSection(JSONObject parent, String key) {
JSONObject section = parent.getJSONObject(key);
if(section == null) section = new JSONObject();
return section;
}
/**
* Get the section of the configuration file from the first json.
*
* @param key the key
* @return the {@link JSONObject}; creates empty {@link JSONObject} if it doesn't exist
*/
public JSONObject getSection(String key) {
return this.getSection(json, key);
}
/**
* Save the json into the file.
*/
public void save() {
// Do not save if the file is null, as it is abstractly defined
if(file == null) return;
try {
// Convert the JSONObject to a 4 spaced-indented string format
String json = this.json.toString(4);
// Save the current content of the JSONObject into the File
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
bw.write(json);
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Generate the default json object for the configuration.
*
* @return the default {@link JSONObject}
*/
protected abstract JSONObject getDefaultJSON();
}
import org.json.JSONObject;
public class JSONConfig extends AbstractJSONConfig {
public JSONConfig(String path, String name) {
super(path, name);
}
@Override
public JSONConfig load(boolean override) {
return (JSONConfig) super.load(override);
}
@Override
public JSONObject getDefaultJSON() {
return new JSONObject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment