diff --git a/src/main/java/ext/Json.kt b/src/main/java/ext/Json.kt index 964b3b38..61cd6545 100644 --- a/src/main/java/ext/Json.kt +++ b/src/main/java/ext/Json.kt @@ -43,16 +43,18 @@ else JACKSON.readValue(this, cls) fun T.toJson() = JACKSON.writeValueAsString(this) inline fun String.json() = try { - JACKSON.readValue(this, T::class.java) + if (isEmpty() || this == "null") null + else JACKSON.readValue(this, T::class.java) } catch (e: Exception) { println("Failed to parse JSON: $this") throw e } -fun String.jsonMap(): Map = json() -fun String.jsonArray(): List> = json() - +fun String.jsonMap(): Map = json() ?: emptyMap() +fun String.jsonArray(): List> = json() ?: emptyList() +fun String.jsonMaybeMap(): Map? = json() +fun String.jsonMaybeArray(): List>? = json() // KotlinX Serialization @OptIn(ExperimentalSerializationApi::class)