![JAR search and dependency download from the Maven repository](/logo.png)
me.shib.java.lib.jsonconfig.JsonConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsonutils Show documentation
Show all versions of jsonutils Show documentation
A simple set of JSON wrappers
package me.shib.java.lib.jsonconfig;
import me.shib.java.lib.utils.JsonUtil;
import java.io.*;
import java.util.HashMap;
public final class JsonConfig {
private static HashMap configMap = new HashMap<>();
private static JsonUtil jsonUtil = new JsonUtil();
private File file;
private JsonConfig(File file) {
this.file = file;
}
public static synchronized JsonConfig getJsonConfig(File file) {
JsonConfig jsonConfig = configMap.get(file);
if (jsonConfig == null) {
jsonConfig = new JsonConfig(file);
configMap.put(file, jsonConfig);
}
return jsonConfig;
}
private String readFromFile() throws IOException {
StringBuilder contentBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
contentBuilder.append(line).append("\n");
}
br.close();
return contentBuilder.toString();
}
public T get(Class classOfT) throws IOException {
return jsonUtil.fromJson(readFromFile(), classOfT);
}
private void writeToFile(String json) throws FileNotFoundException {
PrintWriter pw = new PrintWriter(file);
pw.append(json);
pw.close();
}
public void put(Object object) throws FileNotFoundException {
writeToFile(jsonUtil.toPrettyJson(object));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy