Skip to content

Commit da55a8d

Browse files
committed
Fix JSON parsing code
1 parent e1ec315 commit da55a8d

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

OpacityCore/src/main/jni/include/opacity.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ extern void ios_close_webview(void);
7070

7171
extern const char *ios_get_browser_cookies_for_current_url(void);
7272

73+
extern const char *ios_get_browser_cookies_for_domain(const char *domain);
74+
7375
#ifdef __cplusplus
7476
} // extern "C"
7577
#endif // __cplusplus

OpacityCore/src/main/kotlin/com/opacitylabs/opacitycore/OpacityCore.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlinx.serialization.encodeToString
1010
import kotlinx.serialization.json.Json
1111
import kotlinx.serialization.json.JsonArray
1212
import kotlinx.serialization.json.JsonElement
13+
import kotlinx.serialization.json.JsonNull
1314
import kotlinx.serialization.json.JsonObject
1415
import kotlinx.serialization.json.JsonPrimitive
1516
import kotlinx.serialization.json.boolean
@@ -96,8 +97,18 @@ object OpacityCore {
9697
LocalBroadcastManager.getInstance(appContext).sendBroadcast(closeIntent)
9798
}
9899

99-
private fun parseJsonElementToAny(jsonElement: JsonElement): Any {
100+
private fun parseJsonElementToAny(jsonElement: JsonElement): Any? {
100101
return when (jsonElement) {
102+
is JsonObject -> {
103+
jsonElement.toMap().mapValues { parseJsonElementToAny(it.value) }
104+
}
105+
106+
is JsonArray -> {
107+
jsonElement.map { parseJsonElementToAny(it) }
108+
}
109+
110+
is JsonNull -> null
111+
101112
is JsonPrimitive -> {
102113
when {
103114
jsonElement.isString -> jsonElement.content
@@ -108,28 +119,20 @@ object OpacityCore {
108119
}
109120
}
110121

111-
is JsonObject -> {
112-
jsonElement.toMap().mapValues { parseJsonElementToAny(it.value) }
113-
}
114-
115-
is JsonArray -> {
116-
jsonElement.map { parseJsonElementToAny(it) }
117-
}
118-
119122
else -> throw Exception("Could not convert JSON primitive $jsonElement")
120123
}
121124
}
122125

123126
@JvmStatic
124-
suspend fun get(name: String, params: Map<String, Any?>?): Map<String, Any> {
127+
suspend fun get(name: String, params: Map<String, Any?>?): Map<String, Any?> {
125128
return withContext(Dispatchers.IO) {
126129
val paramsString = params?.let { Json.encodeToString(it) }
127130
val res = getNative(name, paramsString)
128131
if (res.status != 0) {
129132
throw Exception(res.err)
130133
}
131134

132-
val map: Map<String, Any> =
135+
val map: Map<String, Any?> =
133136
Json.parseToJsonElement(res.data!!).jsonObject.mapValues {
134137
parseJsonElementToAny(it.value)
135138
}

app/src/main/java/com/opacitylabs/opacitycoreexample/MainActivity.kt

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,7 @@ class MainActivity : ComponentActivity() {
5050
containerColor = androidx.compose.ui.graphics.Color.Black
5151
) { innerPadding ->
5252
Column(modifier = Modifier.padding(innerPadding)) {
53-
val flowInput = remember { mutableStateOf("") }
54-
55-
Button(
56-
onClick = {
57-
lifecycleScope.launch {
58-
try {
59-
val res = OpacityCore.get("uber_rider:profile", null)
60-
Log.d("MainActivity", res["json"].toString())
61-
} catch (e: Exception) {
62-
Log.e("MainActivity", e.toString())
63-
}
64-
}
65-
},
66-
) { Text(text = "Uber Rider Profile") }
53+
val flowInput = remember { mutableStateOf("github:profile") }
6754

6855
TextField(
6956
value = flowInput.value,
@@ -76,7 +63,8 @@ class MainActivity : ComponentActivity() {
7663
lifecycleScope.launch {
7764
try {
7865
val res = OpacityCore.get(flowInput.value, null)
79-
Log.d("MainActivity", res["json"].toString())
66+
Log.d("MainActivity", "🟩🟩🟩")
67+
Log.d("MainActivity", res.toString())
8068
} catch (e: Exception) {
8169
Log.e("MainActivity", e.toString())
8270
}
@@ -127,6 +115,6 @@ class MainActivity : ComponentActivity() {
127115
requireNotNull(opacityApiKey) { "Opacity API key is null" }
128116

129117
OpacityCore.setContext(this)
130-
OpacityCore.initialize(opacityApiKey, false, OpacityCore.Environment.PRODUCTION, true)
118+
OpacityCore.initialize(opacityApiKey, false, OpacityCore.Environment.STAGING, true)
131119
}
132120
}

0 commit comments

Comments
 (0)