Skip to content

Commit 73935dd

Browse files
committed
Convert from JsonPrimitives to Scalars
1 parent 24ac8ba commit 73935dd

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,27 @@ object OpacityCore {
159159
// return withContext(Dispatchers.IO) { getGustoPayrollAdminIdNative() }
160160
// }
161161

162+
fun parseJsonElementToAny(jsonElement: JsonElement): Any {
163+
return when (jsonElement) {
164+
is JsonPrimitive -> {
165+
when {
166+
jsonElement.isString -> jsonElement.toString()
167+
jsonElement.intOrNull != null -> jsonElement.int
168+
jsonElement.booleanOrNull != null -> jsonElement.boolean
169+
jsonElement.doubleOrNull != null -> jsonElement.double
170+
else -> throw Exception("Could not convert JSON primitive $jsonElement")
171+
}
172+
}
173+
is JsonObject -> {
174+
jsonElement.toMap().mapValues { parseJsonElementToAny(it.value) }
175+
}
176+
is JsonArray -> {
177+
jsonElement.map { parseJsonElementToAny(it) }
178+
}
179+
else -> throw Exception("Could not convert JSON primitive $jsonElement")
180+
}
181+
}
182+
162183
suspend fun get(name: String, params: Map<String, Any>?): Map<String, Any> {
163184
return withContext(Dispatchers.IO) {
164185
val paramsString = params?.let { Json.encodeToString(it) }
@@ -167,7 +188,8 @@ object OpacityCore {
167188
throw Exception(res.err)
168189
}
169190

170-
val map: Map<String, JsonElement> = Json.parseToJsonElement(res.data!!).jsonObject.toMap()
191+
val map: Map<String, Any> = Json.parseToJsonElement(res.data!!).jsonObject
192+
.mapValues { parseJsonElementToAny(it.value) }
171193
map
172194
}
173195
}

0 commit comments

Comments
 (0)