Repository navigation
Json cleanup #55
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
base: master
Are you sure you want to change the base?
Json cleanup #55
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package org.mtransit.commons | ||
|
|
||
| import kotlinx.serialization.json.JsonArray | ||
| import kotlinx.serialization.json.JsonElement | ||
| import kotlinx.serialization.json.JsonNull | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlinx.serialization.json.JsonObjectBuilder | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
| import kotlinx.serialization.json.boolean | ||
| import kotlinx.serialization.json.booleanOrNull | ||
| import kotlinx.serialization.json.buildJsonObject | ||
| import kotlinx.serialization.json.double | ||
| import kotlinx.serialization.json.doubleOrNull | ||
| import kotlinx.serialization.json.long | ||
| import kotlinx.serialization.json.longOrNull | ||
|
|
||
| fun Any?.toJsonElement(): JsonElement = when (this) { | ||
| null -> JsonNull | ||
| is JsonElement -> this | ||
| is Number -> JsonPrimitive(this) | ||
| is Boolean -> JsonPrimitive(this) | ||
| is String -> JsonPrimitive(this) | ||
| is Enum<*> -> JsonPrimitive(this.name) | ||
| is Map<*, *> -> JsonObject( | ||
| this.entries.associate { (k, v) -> k.toString() to v.toJsonElement() } | ||
| ) | ||
|
|
||
| is Iterable<*> -> JsonArray(this.map { it.toJsonElement() }) | ||
| is Array<*> -> JsonArray(this.map { it.toJsonElement() }) | ||
| else -> throw IllegalArgumentException("Unsupported type for JsonElement: ${this::class}") | ||
| } | ||
|
|
||
| fun Any?.toJsonPrimitive(): JsonPrimitive = when (this) { | ||
| null -> JsonNull | ||
| is JsonPrimitive -> this | ||
| is Number -> JsonPrimitive(this) | ||
| is Boolean -> JsonPrimitive(this) | ||
| is String -> JsonPrimitive(this) | ||
| is Enum<*> -> JsonPrimitive(this.name) | ||
| else -> throw IllegalArgumentException("Cannot convert ${this::class} to JsonPrimitive") | ||
| } | ||
|
|
||
| val JsonElement.jsonPrimitiveOrNull get() = this as? JsonPrimitive | ||
| val JsonElement.jsonObjectOrNull get() = this as? JsonObject | ||
| val JsonElement.jsonArrayOrNull get() = this as? JsonArray | ||
| val JsonElement.jsonNullOrNull get() = this as? JsonNull | ||
|
|
||
| fun JsonElement.toAny(): Any? = when (this) { | ||
| is JsonNull -> null | ||
| is JsonPrimitive -> when { | ||
| isString -> content | ||
| booleanOrNull != null -> boolean | ||
| longOrNull != null -> long | ||
| doubleOrNull != null -> double | ||
| else -> content | ||
| } | ||
|
Comment on lines
+50
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the current implementation, standard library functions like We can optimize this by using the Elvis operator ( is JsonPrimitive -> if (isString) {
content
} else {
booleanOrNull ?: longOrNull ?: doubleOrNull ?: content
} |
||
| is JsonObject -> mapValues { it.value.toAny() } | ||
| is JsonArray -> map { it.toAny() } | ||
| } | ||
|
|
||
| inline fun JsonObject.edit(builderAction: JsonObjectBuilder.() -> Unit): JsonObject { | ||
| return buildJsonObject { | ||
| forEach { (key, value) -> put(key, value) } | ||
| builderAction() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Kotlin, primitive arrays (such as
IntArray,LongArray,DoubleArray, etc.) do not inherit fromArray<*>. As a result, passing any primitive array totoJsonElement()will fall through to theelsebranch and throw anIllegalArgumentExceptionat runtime.To make this function robust and support all array types, we should explicitly handle primitive arrays in the
whenexpression.