Skip to content

Add JsonObject field reading syntax sugar #3019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,27 +229,51 @@ public class JsonArray(private val content: List<JsonElement>) : 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]
*/
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]
*/
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]
*/
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
Expand Down Expand Up @@ -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