From 4551f2ff91e7ec6329085f64fbdd927a70e7ee84 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:47:32 +0100 Subject: [PATCH 1/6] add Json Array support for array of arraya --- .../sjapps/jsonlist/java/JsonFunctions.java | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) 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..516c447 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,8 @@ 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)); + setItemOnArray(array,item,callback); mainList.add(item); - return mainList; } @@ -26,6 +23,19 @@ public static ArrayList> getJsonArray(JsonArray array, Excep ArrayList ListOfItems = getJsonObject((JsonObject) array.get(i),callback); ArrList.add(ListOfItems); } + if (array.get(i) instanceof JsonArray){ + + ArrayList> ListOfItems = getJsonArray((JsonArray) array.get(i),callback); + + ArrayList itemsInList = new ArrayList<>(); + ListItem arrItem = new ListItem(); + arrItem.setName("Array"); + arrItem.setIsArrayOfObjects(true); + arrItem.setListObjects(ListOfItems); + + itemsInList.add(arrItem); + ArrList.add(itemsInList); + } } return ArrList; } @@ -39,6 +49,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 +77,25 @@ public static ArrayList getJsonObject(JsonObject obj, ExceptionCallbac return mainList; } + private static void setItemOnArray(JsonArray array,ListItem item, ExceptionCallback callback){ + if(isArrayOfObjects(array)) { + item.setName("Json Array"); + item.setIsArrayOfObjects(true); + item.setListObjects(getJsonArray(array, callback)); + return; + } + if (isArrayOfArray(array)){ + item.setName("Array"); + item.setIsArrayOfObjects(true); + item.setListObjects(getJsonArray(array,callback)); + return; + } + + item.setName("Array items"); + item.setIsArray(true); + item.setValue(array.toString()); + } + private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCallback callback){ if (obj.get(o.toString()) instanceof JsonObject) { item.setIsObject(true); From cdd4a0dcbf92a927554453c32b4113e3b8d5d008 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:47:45 +0100 Subject: [PATCH 2/6] add tests --- .../jsonlist/java/GetJsonArrayRootTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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..fdea92c 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/GetJsonArrayRootTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/GetJsonArrayRootTest.java @@ -43,6 +43,27 @@ 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); + root.setValue("[{\"data2\":123},1242,true,null]"); + + expectedArr.add(root); + assertEquals(expectedArr,itemsArr); + } @Test From bfb19e052e9448169fdc73b51fa4fe2ba8a7904d Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:25:54 +0100 Subject: [PATCH 3/6] remove isArrayOfObjects, use isArray instead --- .../java/com/sjapps/adapters/ListAdapter.java | 2 +- .../com/sjapps/jsonlist/java/JsonFunctions.java | 7 ++----- .../java/com/sjapps/jsonlist/java/ListItem.java | 15 ++------------- .../jsonlist/java/GetJsonArrayRootTest.java | 4 ++-- .../sjapps/jsonlist/java/GetJsonObjectTest.java | 7 +++---- .../sjapps/jsonlist/java/GetListFromPathTest.java | 4 ++-- .../sjapps/jsonlist/java/JsonFunctionsTest.java | 2 +- 7 files changed, 13 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/sjapps/adapters/ListAdapter.java b/app/src/main/java/com/sjapps/adapters/ListAdapter.java index 4203e32..16931dd 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); 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 516c447..4a346dc 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -30,7 +30,7 @@ public static ArrayList> getJsonArray(JsonArray array, Excep ArrayList itemsInList = new ArrayList<>(); ListItem arrItem = new ListItem(); arrItem.setName("Array"); - arrItem.setIsArrayOfObjects(true); + arrItem.setIsArray(true); arrItem.setListObjects(ListOfItems); itemsInList.add(arrItem); @@ -80,13 +80,11 @@ public static ArrayList getJsonObject(JsonObject obj, ExceptionCallbac private static void setItemOnArray(JsonArray array,ListItem item, ExceptionCallback callback){ if(isArrayOfObjects(array)) { item.setName("Json Array"); - item.setIsArrayOfObjects(true); item.setListObjects(getJsonArray(array, callback)); return; } if (isArrayOfArray(array)){ item.setName("Array"); - item.setIsArrayOfObjects(true); item.setListObjects(getJsonArray(array,callback)); return; } @@ -106,7 +104,6 @@ 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; @@ -158,7 +155,7 @@ public static ArrayList getListFromPath(String path, ArrayList listItems = new ArrayList<>(); @@ -73,7 +73,7 @@ public void testGetJsonArrayRootWithEmptyArray() { assertNotNull(result); assertEquals(1, result.size()); assertEquals("Json Array", result.get(0).getName()); - assertTrue(result.get(0).isArrayOfObjects()); + 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..3cd9d61 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,8 @@ 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()); } @@ -179,7 +178,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 +204,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..31a4a2f 100644 --- a/app/src/test/java/com/sjapps/jsonlist/java/JsonFunctionsTest.java +++ b/app/src/test/java/com/sjapps/jsonlist/java/JsonFunctionsTest.java @@ -23,7 +23,7 @@ public void testGetJsonArrayRoot() { assertNotNull(result); assertEquals(1, result.size()); assertEquals("Json Array", result.get(0).getName()); - assertTrue(result.get(0).isArrayOfObjects()); + assertTrue(result.get(0).isArray()); assertEquals(1, result.get(0).getListObjects().size()); } From 233e45ae57c76bd6623c6fa61582ebf38bb559d2 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:38:24 +0100 Subject: [PATCH 4/6] update Json Array now every element in Json Array is displaying separate List Item --- .../java/com/sjapps/adapters/ListAdapter.java | 8 ++++- .../sjapps/jsonlist/java/JsonFunctions.java | 32 ++++++++++--------- app/src/main/res/layout/list_layout2.xml | 4 +-- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/sjapps/adapters/ListAdapter.java b/app/src/main/java/com/sjapps/adapters/ListAdapter.java index 16931dd..e6e15e0 100644 --- a/app/src/main/java/com/sjapps/adapters/ListAdapter.java +++ b/app/src/main/java/com/sjapps/adapters/ListAdapter.java @@ -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 4a346dc..0ad392f 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -11,7 +11,9 @@ public class JsonFunctions { public static ArrayList getJsonArrayRoot(JsonArray array, ExceptionCallback callback) { ArrayList mainList = new ArrayList<>(); ListItem item = new ListItem(); - setItemOnArray(array,item,callback); + setArrayName(array,item); + item.setIsArray(true); + item.setListObjects(getJsonArray(array, callback)); mainList.add(item); return mainList; } @@ -22,6 +24,7 @@ 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){ @@ -29,13 +32,21 @@ public static ArrayList> getJsonArray(JsonArray array, Excep ArrayList itemsInList = new ArrayList<>(); ListItem arrItem = new ListItem(); - arrItem.setName("Array"); + + setArrayName((JsonArray) array.get(i),arrItem); arrItem.setIsArray(true); arrItem.setListObjects(ListOfItems); itemsInList.add(arrItem); ArrList.add(itemsInList); + continue; } + ListItem item = new ListItem(); + String value = array.get(i).toString(); + item.setValue(value.startsWith("\"") && value.endsWith("\"") ? value.substring(1,value.length()-1) : value); + ArrayList items = new ArrayList<>(); + items.add(item); + ArrList.add(items); } return ArrList; } @@ -77,21 +88,16 @@ public static ArrayList getJsonObject(JsonObject obj, ExceptionCallbac return mainList; } - private static void setItemOnArray(JsonArray array,ListItem item, ExceptionCallback callback){ + private static void setArrayName(JsonArray array, ListItem item){ if(isArrayOfObjects(array)) { - item.setName("Json Array"); - item.setListObjects(getJsonArray(array, callback)); + item.setName("Objects Array"); return; } if (isArrayOfArray(array)){ item.setName("Array"); - item.setListObjects(getJsonArray(array,callback)); return; } - item.setName("Array items"); - item.setIsArray(true); - item.setValue(array.toString()); } private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCallback callback){ @@ -103,13 +109,9 @@ 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)) { - 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(); diff --git a/app/src/main/res/layout/list_layout2.xml b/app/src/main/res/layout/list_layout2.xml index a50f756..b2414e7 100644 --- a/app/src/main/res/layout/list_layout2.xml +++ b/app/src/main/res/layout/list_layout2.xml @@ -21,12 +21,12 @@ android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" - android:layout_marginBottom="10dp" android:textStyle="bold" android:textIsSelectable="true" android:longClickable="true" android:textColor="?colorOnSurfaceVariant" android:autoLink="all" + android:visibility="visible" android:text="Item" android:textSize="25dp" /> @@ -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" From b461c17042e75aa8cc0d00329e77ba686e39c9d9 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:38:56 +0100 Subject: [PATCH 5/6] update tests --- .../jsonlist/java/GetJsonArrayRootTest.java | 35 +++++++++++++++++-- .../jsonlist/java/GetJsonObjectTest.java | 4 ++- .../jsonlist/java/JsonFunctionsTest.java | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) 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 61ca86f..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,7 +26,7 @@ public void getJsonArrayRootTest(){ ArrayList expectedArr = new ArrayList<>(); ListItem root = new ListItem(); - root.setName("Json Array"); + root.setName("Objects Array"); root.setIsArray(true); ArrayList listItems = new ArrayList<>(); @@ -59,7 +59,36 @@ public void getJsonArrayRootTest2(){ ListItem root = new ListItem(); root.setName("Array items"); root.setIsArray(true); - root.setValue("[{\"data2\":123},1242,true,null]"); + + 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); @@ -72,7 +101,7 @@ public void testGetJsonArrayRootWithEmptyArray() { ArrayList result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {}); assertNotNull(result); assertEquals(1, result.size()); - assertEquals("Json Array", result.get(0).getName()); + 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 3cd9d61..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,8 +44,10 @@ public void testGetJsonObjectWithNestedArray() { assertNotNull(result); assertEquals(1, result.size()); assertEquals("key1", result.get(0).getName()); - 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()); + } 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 31a4a2f..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,7 +22,7 @@ public void testGetJsonArrayRoot() { ArrayList result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {}); assertNotNull(result); assertEquals(1, result.size()); - assertEquals("Json Array", result.get(0).getName()); + assertEquals("Objects Array", result.get(0).getName()); assertTrue(result.get(0).isArray()); assertEquals(1, result.get(0).getListObjects().size()); } From 05cffd66594c62afa69687cc86b9868e6411406a Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:51:43 +0100 Subject: [PATCH 6/6] refactor --- .../java/com/sjapps/jsonlist/java/JsonFunctions.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 0ad392f..74df873 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -42,8 +42,7 @@ public static ArrayList> getJsonArray(JsonArray array, Excep continue; } ListItem item = new ListItem(); - String value = array.get(i).toString(); - item.setValue(value.startsWith("\"") && value.endsWith("\"") ? value.substring(1,value.length()-1) : value); + item.setValue(getStringFromJson(array.get(i).toString())); ArrayList items = new ArrayList<>(); items.add(item); ArrList.add(items); @@ -99,6 +98,9 @@ private static void setArrayName(JsonArray array, ListItem item){ } 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) { @@ -114,8 +116,7 @@ private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCa 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) {