Browse Source

refactoring: Updated JsonParser

Edited method name
Now working with relative paths
remotes/origin/quiz
Friederike von Gruben 2 years ago
parent
commit
083e9ae00e
  1. 32
      src/main/java/org/bitbiome/classes/JsonParser.java

32
src/main/java/org/bitbiome/classes/JsonParser.java

@ -2,13 +2,11 @@ package org.bitbiome.classes;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener; import org.json.JSONTokener;
import org.json.JSONWriter;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter;
public class JsonParser { public class JsonParser {
@ -28,20 +26,30 @@ public class JsonParser {
https://github.com/stleary/JSON-java https://github.com/stleary/JSON-java
*/ */
public static JSONObject getJSONObject(String fileName) {
String resourceName = "./../../" + fileName;
InputStream is = JsonParser.class.getResourceAsStream(resourceName);
if (is == null) {
throw new NullPointerException("Cannot find resource file " + resourceName);
public static JSONObject readJSONFile(String filePath) {
StringBuilder sb = null;
try {
FileReader reader = new FileReader(filePath);
char[] buffer = new char[1024];
int length;
sb = new StringBuilder();
while ((length = reader.read(buffer)) != -1) {
sb.append(buffer, 0, length);
}
reader.close();
} catch (IOException e) {
System.out.println(e);
} }
JSONTokener tokener = new JSONTokener(is);
return new JSONObject(tokener);
return new JSONObject(sb.toString());
} }
public static void writeObject(String fileName, JSONObject object) { public static void writeObject(String fileName, JSONObject object) {
String resourceName = System.getProperty("user.dir") + "/src/main/resources/" + fileName;
String resourceName = fileName;
try { try {
FileWriter fw = new FileWriter(resourceName, false); FileWriter fw = new FileWriter(resourceName, false);
fw.write(object.toString(1)); fw.write(object.toString(1));
@ -49,7 +57,5 @@ public class JsonParser {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }
Loading…
Cancel
Save