[notifications] Better bundle/json conversion of list types
This commit is contained in:
parent
2979cc4950
commit
56a1f1e84d
|
@ -110,19 +110,37 @@ public class BundleJSONConverter {
|
|||
SETTERS.put(JSONArray.class, new Setter() {
|
||||
public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException {
|
||||
JSONArray jsonArray = (JSONArray) value;
|
||||
// Empty list, can't even figure out the type, assume an ArrayList<String>
|
||||
if (jsonArray.length() == 0) {
|
||||
bundle.putStringArrayList(key, new ArrayList<String>());
|
||||
return;
|
||||
}
|
||||
|
||||
// Only strings are supported for now
|
||||
if (jsonArray.get(0) instanceof String) {
|
||||
ArrayList<String> stringArrayList = new ArrayList<String>();
|
||||
// Assume an empty list is an ArrayList<String>
|
||||
if (jsonArray.length() == 0 || jsonArray.get(0) instanceof String) {
|
||||
ArrayList<String> stringArrayList = new ArrayList<>();
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
stringArrayList.add((String) jsonArray.get(i));
|
||||
}
|
||||
bundle.putStringArrayList(key, stringArrayList);
|
||||
} else if (jsonArray.get(0) instanceof Integer) {
|
||||
ArrayList<Integer> integerArrayList = new ArrayList<>();
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
integerArrayList.add((Integer) jsonArray.get(i));
|
||||
}
|
||||
bundle.putIntegerArrayList(key, integerArrayList);
|
||||
} else if (jsonArray.get(0) instanceof Boolean) {
|
||||
boolean[] booleanArray = new boolean[jsonArray.length()];
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
booleanArray[i] = (Boolean)jsonArray.get(i);
|
||||
}
|
||||
bundle.putBooleanArray(key, booleanArray);
|
||||
} else if (jsonArray.get(0) instanceof Double) {
|
||||
double[] doubleArray = new double[jsonArray.length()];
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
doubleArray[i] = (Double)jsonArray.get(i);
|
||||
}
|
||||
bundle.putDoubleArray(key, doubleArray);
|
||||
} else if (jsonArray.get(0) instanceof Long) {
|
||||
long[] longArray = new long[jsonArray.length()];
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
longArray[i] = (Long) jsonArray.get(i);
|
||||
}
|
||||
bundle.putLongArray(key, longArray);
|
||||
} else if (jsonArray.get(0) instanceof JSONObject) {
|
||||
ArrayList<Bundle> bundleArrayList = new ArrayList<>();
|
||||
for (int i =0; i < jsonArray.length(); i++) {
|
||||
|
@ -162,12 +180,14 @@ public class BundleJSONConverter {
|
|||
JSONArray jsonArray = new JSONArray();
|
||||
List<Object> listValue = (List<Object>) value;
|
||||
for (Object objValue : listValue) {
|
||||
if (objValue instanceof String) {
|
||||
if (objValue instanceof String
|
||||
|| objValue instanceof Integer
|
||||
|| objValue instanceof Double
|
||||
|| objValue instanceof Long
|
||||
|| objValue instanceof Boolean) {
|
||||
jsonArray.put(objValue);
|
||||
} else if (objValue instanceof Bundle) {
|
||||
jsonArray.put(convertToJSON((Bundle) objValue));
|
||||
} else if (objValue instanceof Integer) {
|
||||
jsonArray.put(objValue);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported type: " + objValue.getClass());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue