Skip to content

Commit

Permalink
feat(android): enhance JSObject return types (#6779)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 23, 2023
1 parent 31444ac commit 2a5175a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changes/enhance-jsobject-return-types.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Enhance Android's `JSObject` return types.
Expand Up @@ -28,42 +28,50 @@ class JSObject : JSONObject {
}

fun getInteger(key: String): Int? {
return getInteger(key, null)
return getIntegerInternal(key, null)
}

fun getInteger(key: String, defaultValue: Int?): Int? {
fun getInteger(key: String, defaultValue: Int): Int {
return getIntegerInternal(key, defaultValue)!!
}

private fun getIntegerInternal(key: String, defaultValue: Int?): Int? {
try {
return super.getInt(key)
} catch (_: JSONException) {
}
return defaultValue
}

fun getBoolean(key: String, defaultValue: Boolean?): Boolean? {
override fun getBoolean(key: String): Boolean {
return getBooleanInternal(key, false)!!
}

fun getBoolean(key: String, defaultValue: Boolean?): Boolean {
return getBooleanInternal(key, defaultValue)!!
}

private fun getBooleanInternal(key: String, defaultValue: Boolean?): Boolean? {
try {
return super.getBoolean(key)
} catch (_: JSONException) {
}
return defaultValue
}

/**
* Fetch boolean from jsonObject
*/
fun getBool(key: String): Boolean? {
return getBoolean(key, null)
}

fun getJSObject(name: String): JSObject? {
try {
return getJSObject(name, null)
} catch (e: JSONException) {
return getJSObjectInternal(name, null)
} catch (_: JSONException) {
}
return null
}

@Throws(JSONException::class)
fun getJSObject(name: String, defaultValue: JSObject?): JSObject? {
fun getJSObject(name: String, defaultValue: JSObject): JSObject {
return getJSObjectInternal(name, defaultValue)!!
}

private fun getJSObjectInternal(name: String, defaultValue: JSObject?): JSObject? {
try {
val obj = get(name)
if (obj is JSONObject) {
Expand Down Expand Up @@ -127,11 +135,6 @@ class JSObject : JSONObject {
return this
}

@Throws(JSONException::class)
fun putSafe(key: String, value: Any?): JSObject {
return super.put(key, value) as JSObject
}

companion object {
/**
* Convert a pathetic JSONObject into a JSObject
Expand Down

0 comments on commit 2a5175a

Please sign in to comment.