Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): enhance JSObject return types #6779

Merged
merged 1 commit into from Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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