diff --git a/app/src/main/java/com/sjapps/adapters/ListAdapter.java b/app/src/main/java/com/sjapps/adapters/ListAdapter.java index 4203e32..e6e15e0 100644 --- a/app/src/main/java/com/sjapps/adapters/ListAdapter.java +++ b/app/src/main/java/com/sjapps/adapters/ListAdapter.java @@ -66,7 +66,7 @@ public View getView(int position, View convertView, ViewGroup parent) { if(item.isSpace()) { return LayoutInflater.from(context).inflate(R.layout.space_layout, parent, false); } - if (item.isArrayOfObjects() || item.isObject()) { + if (item.isArray() || item.isObject()) { View view = LayoutInflater.from(context).inflate(R.layout.list_layout,parent,false); TextView titleTxt = view.findViewById(R.id.itemName); @@ -95,8 +95,14 @@ public View getView(int position, View convertView, ViewGroup parent) { View view = LayoutInflater.from(context).inflate(R.layout.list_layout2,parent,false); TextView titleTxt = view.findViewById(R.id.itemName); TextView valueTxt = view.findViewById(R.id.itemValue); + if (item.getName() == null) + titleTxt.setVisibility(View.GONE); + else { + titleTxt.setVisibility(View.VISIBLE); + titleTxt.setText(item.getName()); + } view.setClickable(false); - titleTxt.setText(item.getName()); + valueTxt.setText(item.getValue().isEmpty() ? "\"\"" : item.getValue()); return view; 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 f9b3083..74df873 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -11,11 +11,10 @@ public class JsonFunctions { public static ArrayList getJsonArrayRoot(JsonArray array, ExceptionCallback callback) { ArrayList mainList = new ArrayList<>(); ListItem item = new ListItem(); - item.setName("Json Array"); - item.setIsArrayOfObjects(true); - item.setListObjects(getJsonArray(array,callback)); + setArrayName(array,item); + item.setIsArray(true); + item.setListObjects(getJsonArray(array, callback)); mainList.add(item); - return mainList; } @@ -25,7 +24,28 @@ public static ArrayList> getJsonArray(JsonArray array, Excep if (array.get(i) instanceof JsonObject) { ArrayList ListOfItems = getJsonObject((JsonObject) array.get(i),callback); ArrList.add(ListOfItems); + continue; + } + if (array.get(i) instanceof JsonArray){ + + ArrayList> ListOfItems = getJsonArray((JsonArray) array.get(i),callback); + + ArrayList itemsInList = new ArrayList<>(); + ListItem arrItem = new ListItem(); + + setArrayName((JsonArray) array.get(i),arrItem); + arrItem.setIsArray(true); + arrItem.setListObjects(ListOfItems); + + itemsInList.add(arrItem); + ArrList.add(itemsInList); + continue; } + ListItem item = new ListItem(); + item.setValue(getStringFromJson(array.get(i).toString())); + ArrayList items = new ArrayList<>(); + items.add(item); + ArrList.add(items); } return ArrList; } @@ -39,6 +59,15 @@ static boolean isArrayOfObjects(JsonArray array) { return true; } + static boolean isArrayOfArray(JsonArray array) { + for (int i = 0; i < array.size(); i++) { + if (!(array.get(i) instanceof JsonArray)) { + return false; + } + } + return true; + } + public static ArrayList getJsonObject(JsonObject obj, ExceptionCallback callback) { ArrayList mainList = new ArrayList<>(); Set keys = obj.keySet(); @@ -58,6 +87,21 @@ public static ArrayList getJsonObject(JsonObject obj, ExceptionCallbac return mainList; } + private static void setArrayName(JsonArray array, ListItem item){ + if(isArrayOfObjects(array)) { + item.setName("Objects Array"); + return; + } + if (isArrayOfArray(array)){ + item.setName("Array"); + return; + } + item.setName("Array items"); + } + 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){ if (obj.get(o.toString()) instanceof JsonObject) { item.setIsObject(true); @@ -67,18 +111,12 @@ private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCa } if (obj.get(o.toString()) instanceof JsonArray) { JsonArray array = (JsonArray) obj.get(o.toString()); - if (isArrayOfObjects(array)) { - item.setIsArrayOfObjects(true); - ArrayList> ArrList = getJsonArray(array,callback); - item.setListObjects(ArrList); - return; - } + item.setIsArray(true); - item.setValue(array.toString()); + item.setListObjects(getJsonArray(array,callback)); return; } - String value = obj.get(o.toString()).toString(); - item.setValue(value.startsWith("\"") && value.endsWith("\"") ? value.substring(1,value.length()-1) : value); + item.setValue(getStringFromJson(obj.get(o.toString()).toString())); } static ArrayList getArrayList(ArrayList> list) { @@ -120,7 +158,7 @@ public static ArrayList getListFromPath(String path, ArrayList @@ -36,7 +36,7 @@ android:layout_height="wrap_content" android:layout_below="@+id/itemName" android:layout_marginHorizontal="10dp" - android:layout_marginBottom="10dp" + android:layout_marginVertical="10dp" android:textIsSelectable="true" android:longClickable="true" android:textColor="?colorOnSurfaceVariant" 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 5091e54..da14d09 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/GetJsonArrayRootTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/GetJsonArrayRootTest.java @@ -26,8 +26,8 @@ public void getJsonArrayRootTest(){ ArrayList expectedArr = new ArrayList<>(); ListItem root = new ListItem(); - root.setName("Json Array"); - root.setIsArrayOfObjects(true); + root.setName("Objects Array"); + root.setIsArray(true); ArrayList listItems = new ArrayList<>(); @@ -43,6 +43,56 @@ public void getJsonArrayRootTest(){ expectedArr.add(root); assertEquals(expectedArr,itemsArr); + } + @Test + public void getJsonArrayRootTest2(){ + String data = "[{\"data2\":123},1242,true,null]"; + JsonElement element = JsonParser.parseString(data); + + JsonArray array = FileSystem.loadDataToJsonArray(element); + + ArrayList itemsArr = JsonFunctions.getJsonArrayRoot(array,e -> {}); + + + ArrayList expectedArr = new ArrayList<>(); + + ListItem root = new ListItem(); + root.setName("Array items"); + root.setIsArray(true); + + ArrayList> arrayListArrayList = new ArrayList<>(); + + ArrayList items1 = new ArrayList<>(); + ListItem item1 = new ListItem(); + item1.setName("data2"); + item1.setValue("123"); + items1.add(item1); + arrayListArrayList.add(items1); + + ArrayList items2 = new ArrayList<>(); + ListItem item2 = new ListItem(); + item2.setValue("1242"); + items2.add(item2); + arrayListArrayList.add(items2); + + ArrayList items3 = new ArrayList<>(); + ListItem item3 = new ListItem(); + item3.setValue("true"); + items3.add(item3); + arrayListArrayList.add(items3); + + ArrayList items4 = new ArrayList<>(); + ListItem item4 = new ListItem(); + item4.setValue("null"); + items4.add(item4); + arrayListArrayList.add(items4); + + + root.setListObjects(arrayListArrayList); + + expectedArr.add(root); + assertEquals(expectedArr,itemsArr); + } @Test @@ -51,8 +101,8 @@ public void testGetJsonArrayRootWithEmptyArray() { ArrayList result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {}); assertNotNull(result); assertEquals(1, result.size()); - assertEquals("Json Array", result.get(0).getName()); - assertTrue(result.get(0).isArrayOfObjects()); + assertEquals("Objects Array", result.get(0).getName()); + assertTrue(result.get(0).isArray()); assertEquals(0, result.get(0).getListObjects().size()); } 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 e066726..1acdc62 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/GetJsonObjectTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/GetJsonObjectTest.java @@ -44,9 +44,10 @@ public void testGetJsonObjectWithNestedArray() { assertNotNull(result); assertEquals(1, result.size()); assertEquals("key1", result.get(0).getName()); -// assertTrue(result.get(0).isArray()); - assertFalse(result.get(0).isArrayOfObjects()); - assertEquals("[\"value1\",\"value2\"]", result.get(0).getValue()); + assertTrue(result.get(0).isArray()); + assertEquals("value1",result.get(0).getListObjects().get(0).get(0).getValue()); + assertEquals("value2",result.get(0).getListObjects().get(1).get(0).getValue()); + } @@ -179,7 +180,7 @@ public void getJsonObjectFromStringArray() { item1.setValue("test"); item2.setName("item2"); - item2.setIsArrayOfObjects(true); + item2.setIsArray(true); ArrayList> objs = new ArrayList<>(); ArrayList items1 = new ArrayList<>(); @@ -205,7 +206,7 @@ public void getJsonObjectFromStringArray() { assertNotNull(items); assertEquals(item1,items.get(0)); assertEquals(item2,items.get(1)); - assertTrue(items.get(1).isArrayOfObjects()); + assertTrue(items.get(1).isArray()); assertEquals(items1,items.get(1).getListObjects().get(0)); assertEquals(items2,items.get(1).getListObjects().get(1)); assertEquals(expected, items); diff --git a/app/src/test/java/com/sjapps/jsonlist/java/GetListFromPathTest.java b/app/src/test/java/com/sjapps/jsonlist/java/GetListFromPathTest.java index c2cb54e..9637aac 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/GetListFromPathTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/GetListFromPathTest.java @@ -64,7 +64,7 @@ public void testGetListFromPathWithNestedStructureArrayOfObjects() { ListItem item1 = new ListItem(); item1.setName("Item1"); rootList.add(item1); - item1.setIsArrayOfObjects(true); + item1.setIsArray(true); ArrayList> nestedListList = new ArrayList<>(); ArrayList nestedList1 = new ArrayList<>(); @@ -123,7 +123,7 @@ public void testGetListFromPathGoBack() { ListItem item1 = new ListItem(); item1.setName("Item1"); rootList.add(item1); - item1.setIsArrayOfObjects(true); + item1.setIsArray(true); ArrayList> nestedListList = 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 8789273..277c926 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/JsonFunctionsTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/JsonFunctionsTest.java @@ -22,8 +22,8 @@ public void testGetJsonArrayRoot() { ArrayList result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {}); assertNotNull(result); assertEquals(1, result.size()); - assertEquals("Json Array", result.get(0).getName()); - assertTrue(result.get(0).isArrayOfObjects()); + assertEquals("Objects Array", result.get(0).getName()); + assertTrue(result.get(0).isArray()); assertEquals(1, result.get(0).getListObjects().size()); }