Skip to content

Commit 2a5175a

Browse files
authored
feat(android): enhance JSObject return types (#6779)
1 parent 31444ac commit 2a5175a

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Enhance Android's `JSObject` return types.

core/tauri/mobile/android/src/main/java/app/tauri/plugin/JSObject.kt

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,50 @@ class JSObject : JSONObject {
2828
}
2929

3030
fun getInteger(key: String): Int? {
31-
return getInteger(key, null)
31+
return getIntegerInternal(key, null)
3232
}
3333

34-
fun getInteger(key: String, defaultValue: Int?): Int? {
34+
fun getInteger(key: String, defaultValue: Int): Int {
35+
return getIntegerInternal(key, defaultValue)!!
36+
}
37+
38+
private fun getIntegerInternal(key: String, defaultValue: Int?): Int? {
3539
try {
3640
return super.getInt(key)
3741
} catch (_: JSONException) {
3842
}
3943
return defaultValue
4044
}
4145

42-
fun getBoolean(key: String, defaultValue: Boolean?): Boolean? {
46+
override fun getBoolean(key: String): Boolean {
47+
return getBooleanInternal(key, false)!!
48+
}
49+
50+
fun getBoolean(key: String, defaultValue: Boolean?): Boolean {
51+
return getBooleanInternal(key, defaultValue)!!
52+
}
53+
54+
private fun getBooleanInternal(key: String, defaultValue: Boolean?): Boolean? {
4355
try {
4456
return super.getBoolean(key)
4557
} catch (_: JSONException) {
4658
}
4759
return defaultValue
4860
}
4961

50-
/**
51-
* Fetch boolean from jsonObject
52-
*/
53-
fun getBool(key: String): Boolean? {
54-
return getBoolean(key, null)
55-
}
56-
5762
fun getJSObject(name: String): JSObject? {
5863
try {
59-
return getJSObject(name, null)
60-
} catch (e: JSONException) {
64+
return getJSObjectInternal(name, null)
65+
} catch (_: JSONException) {
6166
}
6267
return null
6368
}
6469

65-
@Throws(JSONException::class)
66-
fun getJSObject(name: String, defaultValue: JSObject?): JSObject? {
70+
fun getJSObject(name: String, defaultValue: JSObject): JSObject {
71+
return getJSObjectInternal(name, defaultValue)!!
72+
}
73+
74+
private fun getJSObjectInternal(name: String, defaultValue: JSObject?): JSObject? {
6775
try {
6876
val obj = get(name)
6977
if (obj is JSONObject) {
@@ -127,11 +135,6 @@ class JSObject : JSONObject {
127135
return this
128136
}
129137

130-
@Throws(JSONException::class)
131-
fun putSafe(key: String, value: Any?): JSObject {
132-
return super.put(key, value) as JSObject
133-
}
134-
135138
companion object {
136139
/**
137140
* Convert a pathetic JSONObject into a JSObject

0 commit comments

Comments
 (0)