From d6590d6962c94e4134e4a51477554b2ca6c548c3 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sun, 5 Nov 2023 01:15:50 +0100 Subject: [PATCH] update exceptions --- .../com/sjapps/jsonlist/MainActivity.java | 70 ++++++++++++------- .../sjapps/jsonlist/java/JsonFunctions.java | 33 ++++----- .../jsonlist/java/GetJsonArrayRootTest.java | 6 +- .../jsonlist/java/GetJsonObjectTest.java | 12 ++-- .../jsonlist/java/JsonFunctionsTest.java | 6 +- 5 files changed, 72 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index c00f397..592af62 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -29,7 +29,9 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; import com.sjapps.about.AboutActivity; import com.sjapps.adapters.ListAdapter; import com.sjapps.jsonlist.java.ExceptionCallback; @@ -37,6 +39,8 @@ import com.sjapps.jsonlist.java.ListItem; import com.sjapps.library.customdialog.BasicDialog; +import org.json.JSONException; + import java.util.ArrayList; @@ -192,35 +196,42 @@ private void LoadData(String Data) { ArrayList temp = data.getRootList(); JsonElement element; try { + + + element = JsonParser.parseString(Data); - } catch (OutOfMemoryError | Exception e) { + } catch (OutOfMemoryError e) { e.printStackTrace(); - handler.post(() -> { - Toast.makeText(MainActivity.this, "File is too large", Toast.LENGTH_SHORT).show(); - progressBar.setVisibility(View.GONE); - }); + fileTooLargeException(); + return; + } catch (Exception e){ + e.printStackTrace(); + fileNotLoadedException(); return; } if (element == null) { - handler.post(() -> { - Toast.makeText(MainActivity.this, "Filed to load file!", Toast.LENGTH_SHORT).show(); - progressBar.setVisibility(View.GONE); - }); + fileNotLoadedException(); return; } - if (element instanceof JsonObject) { - Log.d(TAG, "run: Json object"); - JsonObject object = FileSystem.loadDataToJsonObj(element); - Log.d(TAG, "LoadData: " + object); - data.setRootList(getJsonObject(object,onReadJsonException)); - } - if (element instanceof JsonArray) { - Log.d(TAG, "run: Json array"); - JsonArray array = FileSystem.loadDataToJsonArray(element); - Log.d(TAG, "LoadData: " + array); - data.setRootList(getJsonArrayRoot(array,onReadJsonException)); + try { + if (element instanceof JsonObject) { + Log.d(TAG, "run: Json object"); + JsonObject object = FileSystem.loadDataToJsonObj(element); + Log.d(TAG, "LoadData: " + object); + data.setRootList(getJsonObject(object)); + } + if (element instanceof JsonArray) { + Log.d(TAG, "run: Json array"); + JsonArray array = FileSystem.loadDataToJsonArray(element); + Log.d(TAG, "LoadData: " + array); + data.setRootList(getJsonArrayRoot(array)); + } + } catch (Exception e){ + e.printStackTrace(); + creatingListException(); + data.setRootList(null); } if (!data.isRootListNull()) { @@ -312,8 +323,19 @@ public void onActivityResult(ActivityResult result) { } }); - ExceptionCallback onReadJsonException = e -> { - Log.e(TAG, "getJsonObject: Failed to load data"); - handler.post(() -> Toast.makeText(MainActivity.this, "Failed to load data!", Toast.LENGTH_SHORT).show()); - }; + void fileTooLargeException(){ + postMessageException("File is too large"); + } + void fileNotLoadedException(){ + postMessageException("Fail to load file!"); + } + void creatingListException(){ + postMessageException("Fail to create list!"); + } + void postMessageException(String msg){ + handler.post(() -> { + Toast.makeText(MainActivity.this,msg, Toast.LENGTH_SHORT).show(); + progressBar.setVisibility(View.GONE); + }); + } } \ No newline at end of file diff --git a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java index 74df873..ffce2a2 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -8,27 +8,27 @@ public class JsonFunctions { - public static ArrayList getJsonArrayRoot(JsonArray array, ExceptionCallback callback) { + public static ArrayList getJsonArrayRoot(JsonArray array) { ArrayList mainList = new ArrayList<>(); ListItem item = new ListItem(); setArrayName(array,item); item.setIsArray(true); - item.setListObjects(getJsonArray(array, callback)); + item.setListObjects(getJsonArray(array)); mainList.add(item); return mainList; } - public static ArrayList> getJsonArray(JsonArray array, ExceptionCallback callback) { + public static ArrayList> getJsonArray(JsonArray array) { ArrayList> ArrList = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { if (array.get(i) instanceof JsonObject) { - ArrayList ListOfItems = getJsonObject((JsonObject) array.get(i),callback); + ArrayList ListOfItems = getJsonObject((JsonObject) array.get(i)); ArrList.add(ListOfItems); continue; } if (array.get(i) instanceof JsonArray){ - ArrayList> ListOfItems = getJsonArray((JsonArray) array.get(i),callback); + ArrayList> ListOfItems = getJsonArray((JsonArray) array.get(i)); ArrayList itemsInList = new ArrayList<>(); ListItem arrItem = new ListItem(); @@ -68,21 +68,16 @@ static boolean isArrayOfArray(JsonArray array) { return true; } - public static ArrayList getJsonObject(JsonObject obj, ExceptionCallback callback) { + public static ArrayList getJsonObject(JsonObject obj) { ArrayList mainList = new ArrayList<>(); Set keys = obj.keySet(); Object[] keysArray = keys.toArray(); - try { - for (Object o : keysArray) { - ListItem item = new ListItem(); - item.setName(o.toString()); - setItem(obj,o,item,callback); - mainList.add(item); - } - } catch (Exception e) { - callback.onException(e); - return null; + for (Object o : keysArray) { + ListItem item = new ListItem(); + item.setName(o.toString()); + setItem(obj,o,item); + mainList.add(item); } return mainList; } @@ -102,10 +97,10 @@ private static String getStringFromJson(String value){ return value.startsWith("\"") && value.endsWith("\"") ? value.substring(1,value.length()-1) : value; } - private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCallback callback){ + private static void setItem(JsonObject obj, Object o, ListItem item){ if (obj.get(o.toString()) instanceof JsonObject) { item.setIsObject(true); - ArrayList objList = getJsonObject((JsonObject) obj.get(o.toString()), callback); + ArrayList objList = getJsonObject((JsonObject) obj.get(o.toString())); item.setObjects(objList); return; } @@ -113,7 +108,7 @@ private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCa JsonArray array = (JsonArray) obj.get(o.toString()); item.setIsArray(true); - item.setListObjects(getJsonArray(array,callback)); + item.setListObjects(getJsonArray(array)); return; } item.setValue(getStringFromJson(obj.get(o.toString()).toString())); diff --git a/app/src/test/java/com/sjapps/jsonlist/java/GetJsonArrayRootTest.java b/app/src/test/java/com/sjapps/jsonlist/java/GetJsonArrayRootTest.java index da14d09..77b348b 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/GetJsonArrayRootTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/GetJsonArrayRootTest.java @@ -22,7 +22,7 @@ public void getJsonArrayRootTest(){ JsonArray array = FileSystem.loadDataToJsonArray(element); - ArrayList itemsArr = JsonFunctions.getJsonArrayRoot(array,e -> {}); + ArrayList itemsArr = JsonFunctions.getJsonArrayRoot(array); ArrayList expectedArr = new ArrayList<>(); ListItem root = new ListItem(); @@ -51,7 +51,7 @@ public void getJsonArrayRootTest2(){ JsonArray array = FileSystem.loadDataToJsonArray(element); - ArrayList itemsArr = JsonFunctions.getJsonArrayRoot(array,e -> {}); + ArrayList itemsArr = JsonFunctions.getJsonArrayRoot(array); ArrayList expectedArr = new ArrayList<>(); @@ -98,7 +98,7 @@ public void getJsonArrayRootTest2(){ @Test public void testGetJsonArrayRootWithEmptyArray() { JsonArray jsonArray = new JsonArray(); - ArrayList result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {}); + ArrayList result = JsonFunctions.getJsonArrayRoot(jsonArray); assertNotNull(result); assertEquals(1, result.size()); assertEquals("Objects Array", result.get(0).getName()); diff --git a/app/src/test/java/com/sjapps/jsonlist/java/GetJsonObjectTest.java b/app/src/test/java/com/sjapps/jsonlist/java/GetJsonObjectTest.java index 1acdc62..326acc9 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/GetJsonObjectTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/GetJsonObjectTest.java @@ -20,7 +20,7 @@ public void testGetJsonObjectWithNestedObject() { nestedObject.addProperty("nestedKey", "nestedValue"); jsonObject.add("key1", nestedObject); - ArrayList result = JsonFunctions.getJsonObject(jsonObject, e -> {}); + ArrayList result = JsonFunctions.getJsonObject(jsonObject); assertNotNull(result); assertEquals(1, result.size()); @@ -39,7 +39,7 @@ public void testGetJsonObjectWithNestedArray() { jsonArray.add("value2"); jsonObject.add("key1", jsonArray); - ArrayList result = JsonFunctions.getJsonObject(jsonObject, e -> {}); + ArrayList result = JsonFunctions.getJsonObject(jsonObject); assertNotNull(result); assertEquals(1, result.size()); @@ -57,7 +57,7 @@ public void testGetJsonObjectFromString() { JsonObject jsonObject = new Gson().fromJson(jsonString, JsonObject.class); - ArrayList result = JsonFunctions.getJsonObject(jsonObject, e -> {}); + ArrayList result = JsonFunctions.getJsonObject(jsonObject); assertNotNull(result); assertEquals(2, result.size()); @@ -74,7 +74,7 @@ public void getJsonObjectFromString() { JsonObject object = new Gson().fromJson(data, JsonObject.class); - ArrayList items = JsonFunctions.getJsonObject(object,e -> {}); + ArrayList items = JsonFunctions.getJsonObject(object); ArrayList expected = new ArrayList<>(); @@ -123,7 +123,7 @@ public void getJsonObjectFromStringObject() { JsonObject object = new Gson().fromJson(data, JsonObject.class); - ArrayList items = JsonFunctions.getJsonObject(object,e -> {}); + ArrayList items = JsonFunctions.getJsonObject(object); ArrayList expected = new ArrayList<>(); @@ -169,7 +169,7 @@ public void getJsonObjectFromStringArray() { JsonObject object = new Gson().fromJson(data, JsonObject.class); - ArrayList items = JsonFunctions.getJsonObject(object,e -> {}); + ArrayList items = JsonFunctions.getJsonObject(object); ArrayList expected = new ArrayList<>(); diff --git a/app/src/test/java/com/sjapps/jsonlist/java/JsonFunctionsTest.java b/app/src/test/java/com/sjapps/jsonlist/java/JsonFunctionsTest.java index 277c926..f8694c8 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/JsonFunctionsTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/JsonFunctionsTest.java @@ -19,7 +19,7 @@ public void testGetJsonArrayRoot() { JsonArray jsonArray = new JsonArray(); jsonArray.add(new JsonObject()); - ArrayList result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {}); + ArrayList result = JsonFunctions.getJsonArrayRoot(jsonArray); assertNotNull(result); assertEquals(1, result.size()); assertEquals("Objects Array", result.get(0).getName()); @@ -34,7 +34,7 @@ public void testGetJsonArray() { jsonArray.add(new JsonObject()); jsonArray.add(new JsonObject()); - ArrayList> result = JsonFunctions.getJsonArray(jsonArray, e -> {}); + ArrayList> result = JsonFunctions.getJsonArray(jsonArray); assertNotNull(result); assertEquals(2, result.size()); } @@ -57,7 +57,7 @@ public void testGetJsonObject() { jsonObject.addProperty("key1", "value1"); jsonObject.addProperty("key2", "value2"); - ArrayList result = JsonFunctions.getJsonObject(jsonObject, e -> {}); + ArrayList result = JsonFunctions.getJsonObject(jsonObject); assertNotNull(result); assertEquals(2, result.size()); }