diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt index 202567b77..d15841079 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt @@ -229,6 +229,12 @@ public class JsonArray(private val content: List) : JsonElement(), public val JsonElement.jsonPrimitive: JsonPrimitive get() = this as? JsonPrimitive ?: error("JsonPrimitive") +/** + * Returns content of the current element as JsonPrimitive or `null` if the current element is not a valid representation of JsonPrimitive + */ +public val JsonElement.jsonPrimitiveOrNull: JsonPrimitive? + get() = this as? JsonPrimitive + /** * Convenience method to get current element as [JsonObject] * @throws IllegalArgumentException if current element is not a [JsonObject] @@ -236,6 +242,12 @@ public val JsonElement.jsonPrimitive: JsonPrimitive public val JsonElement.jsonObject: JsonObject get() = this as? JsonObject ?: error("JsonObject") +/** + * Returns content of the current element as JsonObject or `null` if the current element is not a valid representation of JsonObject + */ +public val JsonElement.jsonObjectOrNull: JsonObject? + get() = this as? JsonObject + /** * Convenience method to get current element as [JsonArray] * @throws IllegalArgumentException if current element is not a [JsonArray] @@ -243,6 +255,12 @@ public val JsonElement.jsonObject: JsonObject public val JsonElement.jsonArray: JsonArray get() = this as? JsonArray ?: error("JsonArray") +/** + * Returns content of the current element as JsonArray or `null` if the current element is not a valid representation of JsonArray + */ +public val JsonElement.JsonArrayOrNull: JsonArray? + get() = this as? JsonArray + /** * Convenience method to get current element as [JsonNull] * @throws IllegalArgumentException if current element is not a [JsonNull] @@ -250,6 +268,12 @@ public val JsonElement.jsonArray: JsonArray public val JsonElement.jsonNull: JsonNull get() = this as? JsonNull ?: error("JsonNull") +/** + * Returns content of the current element as JsonNull or `null` if the current element is not a valid representation of JsonNull + */ +public val JsonElement.jsonNullOrNull: JsonNull? + get() = this as? JsonNull + /** * Returns content of the current element as int * @throws NumberFormatException if current element is not a valid representation of number @@ -347,3 +371,66 @@ internal fun unexpectedJson(key: String, expected: String): Nothing = // Use this function to avoid re-wrapping exception into NumberFormatException internal fun JsonPrimitive.parseLongImpl(): Long = StringJsonLexer(content).consumeNumericLiteralFully() + +/** + * Returns the JsonObject value associated with the given [key] in this [JsonObject], + * or null if the key is missing, the value is a string, or cannot be parsed as a JsonObject. + */ +public fun JsonObject.getJsonObjectOrNull(key: String): JsonObject? = + this[key]?.jsonObjectOrNull + +/** + * Returns the JsonArray value associated with the given [key] in this [JsonObject], + * or null if the key is missing, the value is a string, or cannot be parsed as a JsonArray. + */ +public fun JsonObject.getJsonArrayOrNull(key: String): JsonArray? = + this[key]?.JsonArrayOrNull + +/** + * Returns the JsonPrimitive value associated with the given [key] in this [JsonObject], + * or null if the key is missing, the value is a string, or cannot be parsed as a JsonPrimitive. + */ +public fun JsonObject.getJsonPrimitiveOrNull(key: String): JsonPrimitive? = + this[key]?.jsonPrimitiveOrNull + +/** + * Returns the int value associated with the given [key] in this [JsonObject], + * or null if the key is missing, the value is a string, or cannot be parsed as a int. + */ +public fun JsonObject.getIntOrNull(key: String): Int? = + this[key]?.jsonPrimitive?.takeIf { !it.isString }?.intOrNull + +/** + * Returns the long value associated with the given [key] in this [JsonObject], + * or null if the key is missing, the value is a string, or cannot be parsed as a long. + */ +public fun JsonObject.getLongOrNull(key: String): Long? = + this[key]?.jsonPrimitive?.takeIf { !it.isString }?.longOrNull + +/** + * Returns the boolean value associated with the given [key] in this [JsonObject], + * or null if the key is missing, the value is a string, or cannot be parsed as a boolean. + */ +public fun JsonObject.getBooleanOrNull(key: String): Boolean? = + this[key]?.jsonPrimitive?.takeIf { !it.isString }?.booleanOrNull + +/** + * Returns the double value associated with the given [key] in this [JsonObject], + * or null if the key is missing, the value is a string, or cannot be parsed as a double. + */ +public fun JsonObject.getDoubleOrNull(key: String): Double? = + this[key]?.jsonPrimitive?.takeIf { !it.isString }?.doubleOrNull + +/** + * Returns the float value associated with the given [key] in this [JsonObject], + * or null if the key is missing, the value is a string, or cannot be parsed as a float. + */ +public fun JsonObject.getFloatOrNull(key: String): Float? = + this[key]?.jsonPrimitive?.takeIf { !it.isString }?.floatOrNull + +/** + * Returns the string content associated with the given [key] in this [JsonObject], + * or null if the key is missing or the value is not a JSON string. + */ +public fun JsonObject.getStringOrNull(key: String): String? = + this[key]?.jsonPrimitive?.takeIf { it.isString }?.contentOrNull