mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-26 12:08:43 +00:00
add for Game its own serializer and hide usage of RawMessageEntity
This commit is contained in:
parent
2530b85400
commit
44751d94b3
@ -5,9 +5,12 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.files.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.files.*
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
import kotlinx.serialization.internal.StringDescriptor
|
||||||
|
import kotlinx.serialization.json.*
|
||||||
|
import java.lang.UnsupportedOperationException
|
||||||
|
|
||||||
@Serializable
|
@Serializable(GameSerializer::class)
|
||||||
data class Game internal constructor(
|
data class Game(
|
||||||
@SerialName(titleField)
|
@SerialName(titleField)
|
||||||
override val title: String,
|
override val title: String,
|
||||||
@SerialName(descriptionField)
|
@SerialName(descriptionField)
|
||||||
@ -17,18 +20,11 @@ data class Game internal constructor(
|
|||||||
val photo: Photo,
|
val photo: Photo,
|
||||||
@SerialName(textField)
|
@SerialName(textField)
|
||||||
override val caption: String? = null,
|
override val caption: String? = null,
|
||||||
@Serializable(RawMessageEntitiesSerializer::class)
|
|
||||||
@SerialName(textEntitiesField)
|
@SerialName(textEntitiesField)
|
||||||
private val rawEntities: RawMessageEntities? = null,
|
override val captionEntities: List<MessageEntity> = emptyList(),
|
||||||
@SerialName(animationField)
|
@SerialName(animationField)
|
||||||
val animation: AnimationFile? = null
|
val animation: AnimationFile? = null
|
||||||
) : Titled, CaptionedInput {
|
) : Titled, CaptionedInput {
|
||||||
@Transient
|
|
||||||
override val captionEntities: List<MessageEntity> = caption ?.let {
|
|
||||||
rawEntities ?.map {
|
|
||||||
it.asMessageEntity(caption)
|
|
||||||
}
|
|
||||||
} ?: emptyList()
|
|
||||||
|
|
||||||
@Deprecated(
|
@Deprecated(
|
||||||
"Missinterfaced field",
|
"Missinterfaced field",
|
||||||
@ -46,3 +42,43 @@ data class Game internal constructor(
|
|||||||
val textEntities: List<MessageEntity>?
|
val textEntities: List<MessageEntity>?
|
||||||
get() = captionEntities
|
get() = captionEntities
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal object GameSerializer : KSerializer<Game> {
|
||||||
|
override val descriptor: SerialDescriptor = StringDescriptor.withName("GameSerializer")
|
||||||
|
|
||||||
|
override fun deserialize(decoder: Decoder): Game {
|
||||||
|
val json = JsonObjectSerializer.deserialize(decoder)
|
||||||
|
|
||||||
|
val text = json.getPrimitiveOrNull(textField) ?.content
|
||||||
|
|
||||||
|
return Game(
|
||||||
|
json.getPrimitive(titleField).content,
|
||||||
|
json.getPrimitive(descriptionField).content,
|
||||||
|
Json.nonstrict.fromJson(
|
||||||
|
PhotoSerializer,
|
||||||
|
json.getObject(photoField)
|
||||||
|
),
|
||||||
|
text,
|
||||||
|
text ?.let {
|
||||||
|
Json.nonstrict.fromJson(
|
||||||
|
RawMessageEntitiesSerializer,
|
||||||
|
json.getArray(textEntitiesField)
|
||||||
|
).map {
|
||||||
|
it.asMessageEntity(text)
|
||||||
|
}
|
||||||
|
} ?: emptyList(),
|
||||||
|
json.getObjectOrNull(
|
||||||
|
animationField
|
||||||
|
) ?.let { animatedFileJson ->
|
||||||
|
Json.nonstrict.fromJson(
|
||||||
|
AnimationFile.serializer(),
|
||||||
|
animatedFileJson
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun serialize(encoder: Encoder, obj: Game) = throw UnsupportedOperationException(
|
||||||
|
"Objects of class Game can't be serialized for now"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user