Skip to content

Commit

Permalink
fix null in json can be applied to non-null fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed May 15, 2024
1 parent f454a28 commit 7b2239d
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions onebot/src/main/kotlin/sdk/util/JsonHelper.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cn.evolvefield.onebot.sdk.util

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.*
import java.util.function.Supplier

val gson: Gson = GsonBuilder().setPrettyPrinting().create()
Expand Down Expand Up @@ -111,10 +108,39 @@ fun JsonObject.forceString(key: String): String {
}

fun <T> JsonObject.fromJson(key: String, type: Class<T>): T? {
return gson.fromJson(this[key], type)
return gson.fromJson(this[key].deepCopyIgnoreNulls(), type)
}
inline fun <reified T : Any> JsonObject.fromJson(key: String): T? {
return gson.fromJson(this[key], T::class.java)
return gson.fromJson(this[key].deepCopyIgnoreNulls(), T::class.java)
}
fun JsonElement.deepCopyIgnoreNulls(): JsonElement {
return ignoreNulls(this) ?: deepCopy()
}
fun ignoreNulls(json: JsonElement): JsonElement? {
return when (json) {
is JsonObject -> {
return JsonObject().apply {
val members = asMap()
for ((key, value) in members) {
ignoreNulls(value)?.also { add(key, it) }
}
}
}
is JsonArray -> {
val elements = json.asList()
if (elements.isNotEmpty()) {
JsonArray(elements.size).apply {
for (element in elements) {
ignoreNulls(element)?.also { add(it) }
}
}
}
JsonArray()
}
is JsonPrimitive -> return json.deepCopy()

else -> return null
}
}
val <T> T.nullable: T?
get() = this

0 comments on commit 7b2239d

Please sign in to comment.