refactor: migrate to kotlinx serialization, eliminate gson dependency
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
package com.bitcointxoko.gudariwallet.api
|
||||
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonToken
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
import kotlinx.serialization.json.JsonArray
|
||||
import kotlinx.serialization.json.JsonDecoder
|
||||
import kotlinx.serialization.json.JsonNull
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
|
||||
/**
|
||||
* LNbits returns `comment` in PaymentExtra as either:
|
||||
@@ -11,31 +18,29 @@ import com.google.gson.stream.JsonWriter
|
||||
* - a JSON array ["Thanks!"]
|
||||
* - null
|
||||
*
|
||||
* Registered centrally in [GsonProvider] — but also applied via @JsonAdapter
|
||||
* on the field for extra safety.
|
||||
* Mirrors the behaviour of the old Gson FlexibleStringAdapter.
|
||||
*/
|
||||
class FlexibleStringAdapter : TypeAdapter<String?>() {
|
||||
object FlexibleStringSerializer : KSerializer<String?> {
|
||||
|
||||
override fun write(out: JsonWriter, value: String?) {
|
||||
if (value == null) out.nullValue() else out.value(value)
|
||||
override val descriptor: SerialDescriptor =
|
||||
buildClassSerialDescriptor("FlexibleString")
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
override fun serialize(encoder: Encoder, value: String?) {
|
||||
if (value == null) encoder.encodeNull()
|
||||
else encoder.encodeString(value)
|
||||
}
|
||||
|
||||
override fun read(reader: JsonReader): String? {
|
||||
return when (reader.peek()) {
|
||||
JsonToken.NULL -> { reader.nextNull(); null }
|
||||
JsonToken.BEGIN_ARRAY -> {
|
||||
val parts = mutableListOf<String>()
|
||||
reader.beginArray()
|
||||
while (reader.hasNext()) {
|
||||
when (reader.peek()) {
|
||||
JsonToken.NULL -> reader.nextNull()
|
||||
else -> parts.add(reader.nextString())
|
||||
}
|
||||
}
|
||||
reader.endArray()
|
||||
parts.joinToString(", ").ifEmpty { null }
|
||||
}
|
||||
else -> reader.nextString()
|
||||
override fun deserialize(decoder: Decoder): String? {
|
||||
val jsonDecoder = decoder as JsonDecoder
|
||||
return when (val element = jsonDecoder.decodeJsonElement()) {
|
||||
is JsonNull -> null
|
||||
is JsonPrimitive -> element.content // plain string → as-is
|
||||
is JsonArray -> element // array → join elements
|
||||
.mapNotNull { if (it is JsonNull) null else it.jsonPrimitive.content }
|
||||
.joinToString(", ")
|
||||
.ifEmpty { null }
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user