From c5a7a93e5b51266a3b0f9823cb67c51b49b1e81f Mon Sep 17 00:00:00 2001 From: Tanay Neotia <50850142+tneotia@users.noreply.github.com> Date: Fri, 21 Oct 2022 02:07:56 -0400 Subject: [PATCH] fix: encode URI and ArrayList in JSON data (#14) * Encode URI in JSON data * Support ArrayList encoding --- .../main/kotlin/com/bhikadia/receive_intent/Utils.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/android/src/main/kotlin/com/bhikadia/receive_intent/Utils.kt b/android/src/main/kotlin/com/bhikadia/receive_intent/Utils.kt index d637276..cc4a4cd 100644 --- a/android/src/main/kotlin/com/bhikadia/receive_intent/Utils.kt +++ b/android/src/main/kotlin/com/bhikadia/receive_intent/Utils.kt @@ -3,6 +3,7 @@ package com.bhikadia.receive_intent import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import android.net.Uri import android.os.Build import android.os.Bundle import android.os.Parcelable @@ -11,6 +12,7 @@ import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import java.security.MessageDigest +import java.util.ArrayList fun jsonToBundle(json: JSONObject): Bundle { @@ -71,6 +73,9 @@ fun wrap(o: Any?): Any? { try { if (o is Collection<*>) { //Log.e("ReceiveIntentPlugin", "$o is Collection<*>") + if (o is ArrayList<*>) { + return toJSONArray(o) + } return JSONArray(o as Collection<*>?) } else if (o.javaClass.isArray) { //Log.e("ReceiveIntentPlugin", "$o is isArray") @@ -91,7 +96,7 @@ fun wrap(o: Any?): Any? { o is String) { return o } - if (o.javaClass.getPackage().name.startsWith("java.")) { + if (o is Uri || o.javaClass.getPackage().name.startsWith("java.")) { return o.toString() } } catch (e: Exception) { @@ -103,7 +108,7 @@ fun wrap(o: Any?): Any? { @Throws(JSONException::class) fun toJSONArray(array: Any): JSONArray? { val result = JSONArray() - if (!array.javaClass.isArray) { + if (!array.javaClass.isArray && array !is ArrayList<*>) { throw JSONException("Not a primitive array: " + array.javaClass) } @@ -114,6 +119,9 @@ fun toJSONArray(array: Any): JSONArray? { is Array<*> -> { array.forEach { result.put(wrap(it)) } } + is ArrayList<*> -> { + array.forEach { result.put(wrap(it)) } + } } return result