From 083e9ae00e2908d86160df5a222a9d47ab26ed77 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Wed, 1 Feb 2023 23:52:35 +0100 Subject: [PATCH] refactoring: Updated JsonParser Edited method name Now working with relative paths --- .../java/org/bitbiome/classes/JsonParser.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java index 56a9535..66dc79c 100644 --- a/src/main/java/org/bitbiome/classes/JsonParser.java +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -2,13 +2,11 @@ package org.bitbiome.classes; import org.json.JSONObject; import org.json.JSONTokener; -import org.json.JSONWriter; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.StringWriter; - public class JsonParser { @@ -28,20 +26,30 @@ public class JsonParser { 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) { - String resourceName = System.getProperty("user.dir") + "/src/main/resources/" + fileName; + String resourceName = fileName; + try { FileWriter fw = new FileWriter(resourceName, false); fw.write(object.toString(1)); @@ -49,7 +57,5 @@ public class JsonParser { } catch (IOException e) { throw new RuntimeException(e); } - - } }