1 package expo.modules.jsonutils
2 
3 import org.json.JSONArray
4 import org.json.JSONException
5 import org.json.JSONObject
6 
7 @Throws(JSONException::class)
requirenull8 inline fun <reified T : Any> JSONObject.require(key: String): T {
9   return when (T::class) {
10     String::class -> this.getString(key) as T
11     Double::class -> this.getDouble(key) as T
12     Int::class -> this.getInt(key) as T
13     Long::class -> this.getLong(key) as T
14     Boolean::class -> this.getBoolean(key) as T
15     JSONArray::class -> this.getJSONArray(key) as T
16     JSONObject::class -> this.getJSONObject(key) as T
17     else -> this.get(key) as T
18   }
19 }
20 
getNullablenull21 inline fun <reified T : Any> JSONObject.getNullable(key: String): T? {
22   return if (!this.has(key)) {
23     null
24   } else this.require(key)
25 }
26