Skip to content

Commit

Permalink
Merge pull request #18 from SlaVcE14/fix-exception
Browse files Browse the repository at this point in the history
update exceptions
  • Loading branch information
SlaVcE14 authored Nov 5, 2023
2 parents 0599bc2 + d6590d6 commit b8e1c48
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 55 deletions.
70 changes: 46 additions & 24 deletions app/src/main/java/com/sjapps/jsonlist/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
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;
import com.sjapps.jsonlist.java.JsonData;
import com.sjapps.jsonlist.java.ListItem;
import com.sjapps.library.customdialog.BasicDialog;

import org.json.JSONException;

import java.util.ArrayList;


Expand Down Expand Up @@ -192,35 +196,42 @@ private void LoadData(String Data) {
ArrayList<ListItem> 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()) {
Expand Down Expand Up @@ -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);
});
}
}
33 changes: 14 additions & 19 deletions app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@

public class JsonFunctions {

public static ArrayList<ListItem> getJsonArrayRoot(JsonArray array, ExceptionCallback callback) {
public static ArrayList<ListItem> getJsonArrayRoot(JsonArray array) {
ArrayList<ListItem> 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<ArrayList<ListItem>> getJsonArray(JsonArray array, ExceptionCallback callback) {
public static ArrayList<ArrayList<ListItem>> getJsonArray(JsonArray array) {
ArrayList<ArrayList<ListItem>> ArrList = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
if (array.get(i) instanceof JsonObject) {
ArrayList<ListItem> ListOfItems = getJsonObject((JsonObject) array.get(i),callback);
ArrayList<ListItem> ListOfItems = getJsonObject((JsonObject) array.get(i));
ArrList.add(ListOfItems);
continue;
}
if (array.get(i) instanceof JsonArray){

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

ArrayList<ListItem> itemsInList = new ArrayList<>();
ListItem arrItem = new ListItem();
Expand Down Expand Up @@ -68,21 +68,16 @@ static boolean isArrayOfArray(JsonArray array) {
return true;
}

public static ArrayList<ListItem> getJsonObject(JsonObject obj, ExceptionCallback callback) {
public static ArrayList<ListItem> getJsonObject(JsonObject obj) {
ArrayList<ListItem> mainList = new ArrayList<>();
Set<String> 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;
}
Expand All @@ -102,18 +97,18 @@ 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<ListItem> objList = getJsonObject((JsonObject) obj.get(o.toString()), callback);
ArrayList<ListItem> objList = getJsonObject((JsonObject) obj.get(o.toString()));
item.setObjects(objList);
return;
}
if (obj.get(o.toString()) instanceof JsonArray) {
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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void getJsonArrayRootTest(){

JsonArray array = FileSystem.loadDataToJsonArray(element);

ArrayList<ListItem> itemsArr = JsonFunctions.getJsonArrayRoot(array,e -> {});
ArrayList<ListItem> itemsArr = JsonFunctions.getJsonArrayRoot(array);
ArrayList<ListItem> expectedArr = new ArrayList<>();

ListItem root = new ListItem();
Expand Down Expand Up @@ -51,7 +51,7 @@ public void getJsonArrayRootTest2(){

JsonArray array = FileSystem.loadDataToJsonArray(element);

ArrayList<ListItem> itemsArr = JsonFunctions.getJsonArrayRoot(array,e -> {});
ArrayList<ListItem> itemsArr = JsonFunctions.getJsonArrayRoot(array);


ArrayList<ListItem> expectedArr = new ArrayList<>();
Expand Down Expand Up @@ -98,7 +98,7 @@ public void getJsonArrayRootTest2(){
@Test
public void testGetJsonArrayRootWithEmptyArray() {
JsonArray jsonArray = new JsonArray();
ArrayList<ListItem> result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {});
ArrayList<ListItem> result = JsonFunctions.getJsonArrayRoot(jsonArray);
assertNotNull(result);
assertEquals(1, result.size());
assertEquals("Objects Array", result.get(0).getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void testGetJsonObjectWithNestedObject() {
nestedObject.addProperty("nestedKey", "nestedValue");
jsonObject.add("key1", nestedObject);

ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject, e -> {});
ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject);

assertNotNull(result);
assertEquals(1, result.size());
Expand All @@ -39,7 +39,7 @@ public void testGetJsonObjectWithNestedArray() {
jsonArray.add("value2");
jsonObject.add("key1", jsonArray);

ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject, e -> {});
ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject);

assertNotNull(result);
assertEquals(1, result.size());
Expand All @@ -57,7 +57,7 @@ public void testGetJsonObjectFromString() {

JsonObject jsonObject = new Gson().fromJson(jsonString, JsonObject.class);

ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject, e -> {});
ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject);

assertNotNull(result);
assertEquals(2, result.size());
Expand All @@ -74,7 +74,7 @@ public void getJsonObjectFromString() {

JsonObject object = new Gson().fromJson(data, JsonObject.class);

ArrayList<ListItem> items = JsonFunctions.getJsonObject(object,e -> {});
ArrayList<ListItem> items = JsonFunctions.getJsonObject(object);

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

Expand Down Expand Up @@ -123,7 +123,7 @@ public void getJsonObjectFromStringObject() {

JsonObject object = new Gson().fromJson(data, JsonObject.class);

ArrayList<ListItem> items = JsonFunctions.getJsonObject(object,e -> {});
ArrayList<ListItem> items = JsonFunctions.getJsonObject(object);

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

Expand Down Expand Up @@ -169,7 +169,7 @@ public void getJsonObjectFromStringArray() {

JsonObject object = new Gson().fromJson(data, JsonObject.class);

ArrayList<ListItem> items = JsonFunctions.getJsonObject(object,e -> {});
ArrayList<ListItem> items = JsonFunctions.getJsonObject(object);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void testGetJsonArrayRoot() {
JsonArray jsonArray = new JsonArray();
jsonArray.add(new JsonObject());

ArrayList<ListItem> result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {});
ArrayList<ListItem> result = JsonFunctions.getJsonArrayRoot(jsonArray);
assertNotNull(result);
assertEquals(1, result.size());
assertEquals("Objects Array", result.get(0).getName());
Expand All @@ -34,7 +34,7 @@ public void testGetJsonArray() {
jsonArray.add(new JsonObject());
jsonArray.add(new JsonObject());

ArrayList<ArrayList<ListItem>> result = JsonFunctions.getJsonArray(jsonArray, e -> {});
ArrayList<ArrayList<ListItem>> result = JsonFunctions.getJsonArray(jsonArray);
assertNotNull(result);
assertEquals(2, result.size());
}
Expand All @@ -57,7 +57,7 @@ public void testGetJsonObject() {
jsonObject.addProperty("key1", "value1");
jsonObject.addProperty("key2", "value2");

ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject, e -> {});
ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject);
assertNotNull(result);
assertEquals(2, result.size());
}
Expand Down

0 comments on commit b8e1c48

Please sign in to comment.