summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/com/mavlushechka/a1qa/utils/JSONParser.java95
1 files changed, 68 insertions, 27 deletions
diff --git a/src/main/java/com/mavlushechka/a1qa/utils/JSONParser.java b/src/main/java/com/mavlushechka/a1qa/utils/JSONParser.java
index b98fcec..67595f9 100644
--- a/src/main/java/com/mavlushechka/a1qa/utils/JSONParser.java
+++ b/src/main/java/com/mavlushechka/a1qa/utils/JSONParser.java
@@ -1,53 +1,94 @@
package com.mavlushechka.a1qa.utils;
-import org.json.simple.JSONObject;
-import org.json.simple.parser.ParseException;
+import com.google.gson.Gson;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
-import java.io.FileReader;
-import java.io.IOException;
+import java.util.ArrayList;
import java.util.Map;
-import java.util.Optional;
import java.util.TreeMap;
public class JSONParser {
- private final static org.json.simple.parser.JSONParser jsonParser = new org.json.simple.parser.JSONParser();
- private final static TreeMap<String, String> parsedData = new TreeMap<>();
+ private final static TreeMap<String, JSONObject> parsedJSON = new TreeMap<>();
- public static String parse(String filename, String key) {
- Map.Entry<String, String> ceilingEntry = parsedData.ceilingEntry(filename + ".");
- String requiredData;
+ public static String parseData(String filename, String key) {
+ return parse(filename, key, false);
+ }
- if (ceilingEntry == null || !ceilingEntry.getKey().startsWith(filename + ".")) {
- parseFile(filename);
+ public static String parseObject(String filename, String key) {
+ return parse(filename, key, true);
+ }
+
+ public static boolean isJson(String json) {
+ try {
+ new JSONObject(json);
+ } catch (JSONException jsonObjectException) {
+ try {
+ new JSONArray(json);
+ } catch (JSONException jsonArrayException) {
+ return false;
+ }
}
- requiredData = parsedData.get(filename + "." + key);
+ return true;
+ }
- return Optional.ofNullable(requiredData).orElseThrow(() -> new IllegalArgumentException("Cannot find required data."));
+ public static boolean isBodyEmpty(String json) {
+ return json.equals("{}");
}
- private static void parseFile(String filename) {
- JSONObject jsonObject;
+ public static <T> T convertToObject(String json, Class<T> classToConvert) {
+ return new Gson().fromJson(json, classToConvert);
+ }
+
+ public static <T> String convertToJSON(T object) {
+ return new Gson().toJson(object);
+ }
- try (FileReader fileReader = new FileReader("src/main/resources/" + filename + ".json")) {
- jsonObject = (JSONObject) jsonParser.parse(fileReader);
- } catch (ParseException | IOException e) {
- throw new IllegalArgumentException("Cannot find required file.");
+ public static <T> T[] convertArray(String json, Class<T> classToConvert) {
+ Gson gson = new Gson();
+ JSONArray jsonArray = new JSONArray(json);
+ ArrayList<T> objects = new ArrayList<>();
+
+ for (int i = 0; i < jsonArray.length(); i++) {
+ objects.add(gson.fromJson(jsonArray.getJSONObject(i).toString(), classToConvert));
+ }
+ return (T[]) objects.toArray();
+ }
+
+ private static String parse(String filename, String key, boolean isObject) {
+ Map.Entry<String, JSONObject> ceilingEntry = parsedJSON.ceilingEntry(filename);
+
+ if (ceilingEntry == null || !ceilingEntry.getKey().startsWith(filename)) {
+ parseFile(filename);
}
- putToParsedData(jsonObject, filename);
+ return parseData(filename, key, isObject);
}
- private static void putToParsedData(JSONObject jsonObject, String filename) {
- for (int i = 0; i < jsonObject.keySet().size(); i++) {
- String[] keyValues = jsonObject.values().toArray()[i].toString().replaceAll("[{}\"\\\\]", "").split(",");
+ private static void parseFile(String filename) {
+ parsedJSON.put(filename, new JSONObject(FileParser.parse("src/main/resources/" + filename + ".json")));
+ }
- for (String keyValue : keyValues) {
- String[] splitKeyValue = keyValue.split(":", 2);
+ private static String parseData(String JSONName, String key, boolean isObject) {
+ JSONObject jsonObject = new JSONObject(parsedJSON.get(JSONName).toString());
+ String[] keys = key.split("\\.");
- parsedData.put(filename + "." + jsonObject.keySet().toArray()[i] + "." + splitKeyValue[0], splitKeyValue[1]);
+ for (int i = 0; i < keys.length-1; i++) {
+ if (keys[i].contains("[") && keys[i].contains("]")) {
+ String splitKey = keys[i].split("\\[")[0];
+ int index = Integer.parseInt(keys[i].split("\\[")[1].replaceAll("[\\[\\]]", ""));
+
+ jsonObject = jsonObject.getJSONArray(splitKey).getJSONObject(index);
+ } else {
+ jsonObject = jsonObject.getJSONObject(keys[i]);
}
}
+ if (isObject) {
+ return jsonObject.getJSONObject(keys[keys.length-1]).toString();
+ }
+ return jsonObject.getString(keys[keys.length-1]);
}
}