Snowmtl: Fix json serializer (#7168)

* Fix json serializer

* Fix lint
This commit is contained in:
Chopper 2025-01-15 09:48:32 -03:00 committed by Draff
parent 03accf8717
commit 7ae3695832
No known key found for this signature in database
GPG Key ID: E8A89F3211677653
2 changed files with 24 additions and 20 deletions

View File

@ -2,4 +2,4 @@ plugins {
id("lib-multisrc") id("lib-multisrc")
} }
baseVersionCode = 1 baseVersionCode = 2

View File

@ -13,7 +13,9 @@ import kotlinx.serialization.json.JsonTransformingSerializer
import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.put import kotlinx.serialization.json.put
import java.io.IOException
@Serializable @Serializable
class PageDto( class PageDto(
@ -76,42 +78,44 @@ private object DialogListSerializer :
put("y2", coordinates[3]) put("y2", coordinates[3])
put("textByLanguage", textByLanguage) put("textByLanguage", textByLanguage)
try { if (jsonElement.isArray) {
val obj = jsonElement.jsonObject return@buildJsonObject
}
jsonElement.jsonObject.let { obj ->
obj["fg_color"]?.let { put("fbColor", it) } obj["fg_color"]?.let { put("fbColor", it) }
obj["bg_color"]?.let { put("bgColor", it) } obj["bg_color"]?.let { put("bgColor", it) }
obj["angle"]?.let { put("angle", it) } obj["angle"]?.let { put("angle", it) }
obj["type"]?.let { put("type", it) } obj["type"]?.let { put("type", it) }
obj["is_bold"]?.let { put("isBold", it) } obj["is_bold"]?.let { put("isBold", it) }
put("isNewApi", true) put("isNewApi", true)
} catch (_: Exception) { } }
} }
}, },
) )
} }
private fun getCoordinates(element: JsonElement): JsonArray { private fun getCoordinates(element: JsonElement): JsonArray {
return try { return when (element) {
element.jsonArray[0].jsonArray is JsonArray -> element.jsonArray[0].jsonArray
} catch (_: Exception) { else -> element.jsonObject["bbox"]?.jsonArray
element.jsonObject["bbox"]!!.jsonArray ?: throw IOException("Dialog box position not found")
} }
} }
private fun getDialogs(element: JsonElement): JsonObject { private fun getDialogs(element: JsonElement): JsonObject {
return try { return buildJsonObject {
buildJsonObject { when (element) {
put("text", element.jsonArray[1]) is JsonArray -> put("text", element.jsonArray[1])
} else -> {
} catch (_: Exception) { element.jsonObject.entries
buildJsonObject { .filter { it.value.isString }
// There is a problem when the "angle" is processed .forEach { put(it.key, it.value) }
element.jsonObject.entries.forEach {
if (it.key in listOf("angle", "bbox")) {
return@forEach
}
put(it.key, it.value)
} }
} }
} }
} }
private val JsonElement.isArray get() = this is JsonArray
private val JsonElement.isObject get() = this is JsonObject
private val JsonElement.isString get() = this.isObject.not() && this.isArray.not() && this.jsonPrimitive.isString
} }