Skip to content

Add Enhanced JsonObject deserialization and 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 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -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 @@ -347,3 +347,24 @@ 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> JsonObject.getBeanOrNull(key: String): T? = this[key]?.takeIf { it is JsonObject || it is JsonArray }?.let { Json.decodeFromString<T>(it.toString()) }
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.isJsonNull(key: String): Boolean = this[key] is JsonNull
public fun JsonObject.getIntOrNull(key: String): Int? = this[key]?.jsonPrimitive?.takeIf { !it.isString }?.intOrNull
public fun JsonObject.getLongOrNull(key: String): Long? = this[key]?.jsonPrimitive?.takeIf { !it.isString }?.longOrNull
public fun JsonObject.getBooleanOrNull(key: String): Boolean? = this[key]?.jsonPrimitive?.takeIf { !it.isString }?.booleanOrNull
public fun JsonObject.getDoubleOrNull(key: String): Double? = this[key]?.jsonPrimitive?.takeIf { !it.isString }?.doubleOrNull
public fun JsonObject.getFloatOrNull(key: String): Float? = this[key]?.jsonPrimitive?.takeIf { !it.isString }?.floatOrNull
public fun JsonObject.getStringOrNull(key: String): String? = this[key]?.jsonPrimitive?.takeIf { it.isString }?.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