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
Show file tree
Hide file tree
Changes from 3 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 @@ -179,6 +179,9 @@ public sealed class Json(
return decodeFromString(JsonElementSerializer, string)
}

public fun parseToJsonObject(@FormatLanguage("json", "", "") string: String): JsonObject = parseToJsonElement(string).jsonObject
public fun parseToJsonArray(@FormatLanguage("json", "", "") string: String): JsonArray = parseToJsonElement(string).jsonArray

/**
* Following functions are copied from extensions on StringFormat
* to streamline experience for newcomers, since IDE does not star-import kotlinx.serialization.* automatically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ public val JsonElement.jsonArray: JsonArray
public val JsonElement.jsonNull: JsonNull
get() = this as? JsonNull ?: error("JsonNull")

/**
* Returns true if current element is [JsonNull]
*/
public val JsonElement?.isJsonNull: Boolean
get() = (this as? JsonNull) != null

/**
* 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 +353,25 @@ 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()

/**
* Convenience methods to get typed elements from [JsonObject]
*/
public inline fun <reified T> JsonElement.bean(): T = Json.decodeFromString<T>(this.toString())
public inline fun <reified T> JsonObject.getBeanOrNull(key: String): T? = this[key]?.bean<T>()
public fun JsonObject.getJsonObject(key: String): JsonObject? = this[key]?.jsonObject
public fun JsonObject.getJsonArray(key: String): JsonArray? = this[key]?.jsonArray
public fun JsonObject.getJsonPrimitive(key: String): JsonPrimitive? = this[key]?.jsonPrimitive
public fun JsonObject.getJsonNull(key: String): JsonNull? = this[key]?.jsonNull
public fun JsonObject.getIntOrNull(key: String): Int? = this[key]?.jsonPrimitive?.intOrNull
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and similar) functions should include a check that it is not a string isString should return false

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and similar) functions should include a check that it is not a string isString should return false

If I don't check isString , it seems to have no consequences, I guess it should be the converted type?
image

public fun JsonObject.getLongOrNull(key: String): Long? = this[key]?.jsonPrimitive?.longOrNull
public fun JsonObject.getBooleanOrNull(key: String): Boolean? = this[key]?.jsonPrimitive?.booleanOrNull
public fun JsonObject.getDoubleOrNull(key: String): Double? = this[key]?.jsonPrimitive?.doubleOrNull
public fun JsonObject.getFloatOrNull(key: String): Float? = this[key]?.jsonPrimitive?.floatOrNull
public fun JsonObject.getStringOrNull(key: String): String? = this[key]?.jsonPrimitive?.contentOrNull
public fun JsonObject.getIntOrElse(key: String, default: Int): Int = getIntOrNull(key) ?: default
public fun JsonObject.getLongOrElse(key: String, default: Long): Long = getLongOrNull(key) ?: default
public fun JsonObject.getBooleanOrElse(key: String, default: Boolean): Boolean = getBooleanOrNull(key) ?: default
public fun JsonObject.getDoubleOrElse(key: String, default: Double): Double = getDoubleOrNull(key) ?: default
public fun JsonObject.getFloatOrElse(key: String, default: Float): Float = getFloatOrNull(key) ?: default
public fun JsonObject.getStringOrElse(key: String, default: String): String = getStringOrNull(key) ?: default