@@ -14,6 +14,7 @@ import com.freeraspreactnative.models.RNPackageInfo
1414import com.freeraspreactnative.models.RNSuspiciousAppInfo
1515import kotlinx.serialization.encodeToString
1616import kotlinx.serialization.json.Json
17+ import org.json.JSONException
1718
1819
1920internal fun ReadableMap.getMapThrowing (key : String ): ReadableMap {
@@ -31,22 +32,26 @@ internal fun ReadableMap.getBooleanSafe(key: String, defaultValue: Boolean = tru
3132 return defaultValue
3233}
3334
34- internal fun ReadableArray.toArray (): Array <String > {
35- val output = mutableListOf<String >()
35+ private inline fun <reified T > ReadableArray.toPrimitiveArray (): Array <T > {
36+ val output = mutableListOf<T >()
37+
3638 for (i in 0 until this .size()) {
37- // in RN versions < 0.63, getString is nullable
38- @Suppress(" UNNECESSARY_SAFE_CALL" )
39- this .getString(i)?.let {
40- output.add(it)
39+ val element: T ? = when (T ::class ) {
40+ String ::class -> this .getString(i) as T ?
41+ Int ::class -> this .getInt(i) as T ?
42+ Double ::class -> this .getDouble(i) as T ?
43+ Boolean ::class -> this .getBoolean(i) as T ?
44+ else -> throw JSONException (" Cannot parse JSON array - unsupported type" )
4145 }
46+ element?.let (output::add)
4247 }
4348 return output.toTypedArray()
4449}
4550
4651internal fun ReadableMap.getArraySafe (key : String ): Array <String > {
4752 if (this .hasKey(key)) {
4853 val inputArray = this .getArray(key)!!
49- return inputArray.toArray ()
54+ return inputArray.toPrimitiveArray ()
5055 }
5156 return arrayOf()
5257}
@@ -56,7 +61,7 @@ internal fun ReadableMap.getNestedArraySafe(key: String): Array<Array<String>> {
5661 if (this .hasKey(key)) {
5762 val inputArray = this .getArray(key)!!
5863 for (i in 0 until inputArray.size()) {
59- outArray.add(inputArray.getArray(i).toArray ())
64+ outArray.add(inputArray.getArray(i).toPrimitiveArray ())
6065 }
6166 }
6267 return outArray.toTypedArray()
0 commit comments