Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array #17

Merged
merged 6 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/src/main/java/com/sjapps/adapters/ListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
66 changes: 52 additions & 14 deletions app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ public class JsonFunctions {
public static ArrayList<ListItem> getJsonArrayRoot(JsonArray array, ExceptionCallback callback) {
ArrayList<ListItem> 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;
}

Expand All @@ -25,7 +24,28 @@ public static ArrayList<ArrayList<ListItem>> getJsonArray(JsonArray array, Excep
if (array.get(i) instanceof JsonObject) {
ArrayList<ListItem> ListOfItems = getJsonObject((JsonObject) array.get(i),callback);
ArrList.add(ListOfItems);
continue;
}
if (array.get(i) instanceof JsonArray){

ArrayList<ArrayList<ListItem>> ListOfItems = getJsonArray((JsonArray) array.get(i),callback);

ArrayList<ListItem> 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<ListItem> items = new ArrayList<>();
items.add(item);
ArrList.add(items);
}
return ArrList;
}
Expand All @@ -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<ListItem> getJsonObject(JsonObject obj, ExceptionCallback callback) {
ArrayList<ListItem> mainList = new ArrayList<>();
Set<String> keys = obj.keySet();
Expand All @@ -58,6 +87,21 @@ public static ArrayList<ListItem> 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);
Expand All @@ -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<ArrayList<ListItem>> 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<ListItem> getArrayList(ArrayList<ArrayList<ListItem>> list) {
Expand Down Expand Up @@ -120,7 +158,7 @@ public static ArrayList<ListItem> getListFromPath(String path, ArrayList<ListIte
if (id != -1 && item.getId() != id)
continue;

if (item.isArrayOfObjects()) {
if (item.isArray()) {
list = getArrayList(item.getListObjects());
break;
}
Expand Down
15 changes: 2 additions & 13 deletions app/src/main/java/com/sjapps/jsonlist/java/ListItem.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.sjapps.jsonlist.java;

import java.util.ArrayList;
import java.util.Objects;

public class ListItem {

private String Name;
private String Value;
private boolean isArrayOfObjects;
private boolean isArray;
private boolean isObject;
private boolean isSpace;
Expand Down Expand Up @@ -35,14 +33,6 @@ public void setValue(String value) {
Value = value;
}

public boolean isArrayOfObjects() {
return isArrayOfObjects;
}

public void setIsArrayOfObjects(boolean array) {
isArrayOfObjects = array;
}

public boolean isArray() {
return isArray;
}
Expand Down Expand Up @@ -97,7 +87,6 @@ public String toString() {
"\"ID\":" + id +
",\"Name\":" +(Name!=null && !Name.startsWith("\"")?"\"":"") + Name + (Name!=null && !Name.startsWith("\"")?"\"":"") +
", \"Value\":" + (Value!=null && !Value.startsWith("\"")?"\"":"") + Value + (Value!=null && !Value.startsWith("\"")?"\"":"") +
", \"isArrayOfObjects\":" + isArrayOfObjects +
", \"isArray\":" + isArray +
", \"isObject\":" + isObject +
", \"isSpace\":" + isSpace +
Expand All @@ -116,11 +105,11 @@ public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ListItem)) return false;
ListItem item = (ListItem) o;
return isArrayOfObjects() == item.isArrayOfObjects() && isArray() == item.isArray() && isObject() == item.isObject() && isSpace() == item.isSpace() && java.util.Objects.equals(getName(), item.getName()) && java.util.Objects.equals(getValue(), item.getValue()) && java.util.Objects.equals(getObjects(), item.getObjects()) && java.util.Objects.equals(getListObjects(), item.getListObjects());
return isArray() == item.isArray() && isObject() == item.isObject() && isSpace() == item.isSpace() && java.util.Objects.equals(getName(), item.getName()) && java.util.Objects.equals(getValue(), item.getValue()) && java.util.Objects.equals(getObjects(), item.getObjects()) && java.util.Objects.equals(getListObjects(), item.getListObjects());
}

@Override
public int hashCode() {
return java.util.Objects.hash(getName(), getValue(), isArrayOfObjects(), isArray(), isObject(), isSpace(), getObjects(), getListObjects());
return java.util.Objects.hash(getName(), getValue(), isArray(), isObject(), isSpace(), getObjects(), getListObjects());
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/list_layout2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void getJsonArrayRootTest(){
ArrayList<ListItem> expectedArr = new ArrayList<>();

ListItem root = new ListItem();
root.setName("Json Array");
root.setIsArrayOfObjects(true);
root.setName("Objects Array");
root.setIsArray(true);

ArrayList<ListItem> listItems = new ArrayList<>();

Expand All @@ -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<ListItem> itemsArr = JsonFunctions.getJsonArrayRoot(array,e -> {});


ArrayList<ListItem> expectedArr = new ArrayList<>();

ListItem root = new ListItem();
root.setName("Array items");
root.setIsArray(true);

ArrayList<ArrayList<ListItem>> arrayListArrayList = new ArrayList<>();

ArrayList<ListItem> items1 = new ArrayList<>();
ListItem item1 = new ListItem();
item1.setName("data2");
item1.setValue("123");
items1.add(item1);
arrayListArrayList.add(items1);

ArrayList<ListItem> items2 = new ArrayList<>();
ListItem item2 = new ListItem();
item2.setValue("1242");
items2.add(item2);
arrayListArrayList.add(items2);

ArrayList<ListItem> items3 = new ArrayList<>();
ListItem item3 = new ListItem();
item3.setValue("true");
items3.add(item3);
arrayListArrayList.add(items3);

ArrayList<ListItem> 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
Expand All @@ -51,8 +101,8 @@ public void testGetJsonArrayRootWithEmptyArray() {
ArrayList<ListItem> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}


Expand Down Expand Up @@ -179,7 +180,7 @@ public void getJsonObjectFromStringArray() {
item1.setValue("test");

item2.setName("item2");
item2.setIsArrayOfObjects(true);
item2.setIsArray(true);

ArrayList<ArrayList<ListItem>> objs = new ArrayList<>();
ArrayList<ListItem> items1 = new ArrayList<>();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testGetListFromPathWithNestedStructureArrayOfObjects() {
ListItem item1 = new ListItem();
item1.setName("Item1");
rootList.add(item1);
item1.setIsArrayOfObjects(true);
item1.setIsArray(true);

ArrayList<ArrayList<ListItem>> nestedListList = new ArrayList<>();
ArrayList<ListItem> nestedList1 = new ArrayList<>();
Expand Down Expand Up @@ -123,7 +123,7 @@ public void testGetListFromPathGoBack() {
ListItem item1 = new ListItem();
item1.setName("Item1");
rootList.add(item1);
item1.setIsArrayOfObjects(true);
item1.setIsArray(true);

ArrayList<ArrayList<ListItem>> nestedListList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public void testGetJsonArrayRoot() {
ArrayList<ListItem> 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());
}

Expand Down