From 56a1f1e84d342ff6d0895a16b734d174fb6210fa Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Tue, 27 Mar 2018 10:57:53 +0100 Subject: [PATCH] [notifications] Better bundle/json conversion of list types --- .../messaging/BundleJSONConverter.java | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/android/src/main/java/io/invertase/firebase/messaging/BundleJSONConverter.java b/android/src/main/java/io/invertase/firebase/messaging/BundleJSONConverter.java index e90ed8c3..72b3fa13 100644 --- a/android/src/main/java/io/invertase/firebase/messaging/BundleJSONConverter.java +++ b/android/src/main/java/io/invertase/firebase/messaging/BundleJSONConverter.java @@ -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 - if (jsonArray.length() == 0) { - bundle.putStringArrayList(key, new ArrayList()); - return; - } - - // Only strings are supported for now - if (jsonArray.get(0) instanceof String) { - ArrayList stringArrayList = new ArrayList(); + // Assume an empty list is an ArrayList + if (jsonArray.length() == 0 || jsonArray.get(0) instanceof String) { + ArrayList 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 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 bundleArrayList = new ArrayList<>(); for (int i =0; i < jsonArray.length(); i++) { @@ -162,12 +180,14 @@ public class BundleJSONConverter { JSONArray jsonArray = new JSONArray(); List listValue = (List) 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()); }