add CommonMultivariantStringSerializer

This commit is contained in:
InsanusMokrassar 2019-03-21 14:24:26 +08:00
parent 6dd9638059
commit 90cb96a08a
2 changed files with 34 additions and 4 deletions

View File

@ -1,35 +1,46 @@
package com.github.insanusmokrassar.SauceNaoAPI.models package com.github.insanusmokrassar.SauceNaoAPI.models
import kotlinx.serialization.Optional import com.github.insanusmokrassar.SauceNaoAPI.utils.CommonMultivariantStringSerializer
import kotlinx.serialization.SerialName import kotlinx.serialization.*
import kotlinx.serialization.Serializable import kotlinx.serialization.internal.ArrayListSerializer
import kotlinx.serialization.internal.StringSerializer
@Serializable @Serializable
data class ResultData( data class ResultData(
@Serializable(CommonMultivariantStringSerializer::class)
@SerialName("danbooru_id") @SerialName("danbooru_id")
@Optional @Optional
val danbooruId: String? = null, val danbooruId: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@SerialName("gelbooru_id") @SerialName("gelbooru_id")
@Optional @Optional
val gelbooruId: String? = null, val gelbooruId: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@SerialName("drawr_id") @SerialName("drawr_id")
@Optional @Optional
val drawrId: String? = null, val drawrId: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@SerialName("pixiv_id") @SerialName("pixiv_id")
@Optional @Optional
val pixivId: String? = null, val pixivId: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@Optional @Optional
val title: String? = null, val title: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@Optional @Optional
val creator: String? = null, val creator: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@Optional @Optional
val material: String? = null, val material: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@SerialName("member_name") @SerialName("member_name")
@Optional @Optional
val memberName: String? = null, val memberName: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@SerialName("member_id") @SerialName("member_id")
@Optional @Optional
val memberId: String? = null, val memberId: String? = null,
@Serializable(CommonMultivariantStringSerializer::class)
@Optional @Optional
val characters: String? = null, val characters: String? = null,
@SerialName("ext_urls") @SerialName("ext_urls")

View File

@ -0,0 +1,19 @@
package com.github.insanusmokrassar.SauceNaoAPI.utils
import kotlinx.serialization.*
import kotlinx.serialization.internal.ArrayListSerializer
import kotlinx.serialization.internal.StringSerializer
@Serializer(String::class)
object CommonMultivariantStringSerializer : KSerializer<String> by StringSerializer {
private val stringArraySerializer = ArrayListSerializer(StringSerializer)
override fun deserialize(decoder: Decoder): String {
return try {
decoder.decodeSerializableValue(StringSerializer)
} catch (e: Exception) {
decoder.decodeSerializableValue(stringArraySerializer).joinToString()
}
}
}