fix: encode URI and ArrayList in JSON data (#14)

* Encode URI in JSON data

* Support ArrayList encoding
This commit is contained in:
Tanay Neotia 2022-10-21 02:07:56 -04:00 committed by GitHub
parent a7c38a7004
commit c5a7a93e5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ package com.bhikadia.receive_intent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
@ -11,6 +12,7 @@ import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import java.security.MessageDigest import java.security.MessageDigest
import java.util.ArrayList
fun jsonToBundle(json: JSONObject): Bundle { fun jsonToBundle(json: JSONObject): Bundle {
@ -71,6 +73,9 @@ fun wrap(o: Any?): Any? {
try { try {
if (o is Collection<*>) { if (o is Collection<*>) {
//Log.e("ReceiveIntentPlugin", "$o is Collection<*>") //Log.e("ReceiveIntentPlugin", "$o is Collection<*>")
if (o is ArrayList<*>) {
return toJSONArray(o)
}
return JSONArray(o as Collection<*>?) return JSONArray(o as Collection<*>?)
} else if (o.javaClass.isArray) { } else if (o.javaClass.isArray) {
//Log.e("ReceiveIntentPlugin", "$o is isArray") //Log.e("ReceiveIntentPlugin", "$o is isArray")
@ -91,7 +96,7 @@ fun wrap(o: Any?): Any? {
o is String) { o is String) {
return o return o
} }
if (o.javaClass.getPackage().name.startsWith("java.")) { if (o is Uri || o.javaClass.getPackage().name.startsWith("java.")) {
return o.toString() return o.toString()
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -103,7 +108,7 @@ fun wrap(o: Any?): Any? {
@Throws(JSONException::class) @Throws(JSONException::class)
fun toJSONArray(array: Any): JSONArray? { fun toJSONArray(array: Any): JSONArray? {
val result = JSONArray() val result = JSONArray()
if (!array.javaClass.isArray) { if (!array.javaClass.isArray && array !is ArrayList<*>) {
throw JSONException("Not a primitive array: " + array.javaClass) throw JSONException("Not a primitive array: " + array.javaClass)
} }
@ -114,6 +119,9 @@ fun toJSONArray(array: Any): JSONArray? {
is Array<*> -> { is Array<*> -> {
array.forEach { result.put(wrap(it)) } array.forEach { result.put(wrap(it)) }
} }
is ArrayList<*> -> {
array.forEach { result.put(wrap(it)) }
}
} }
return result return result