Skip to content

Commit

Permalink
feat: WarpStringSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Apr 17, 2024
1 parent d7c54b0 commit 34a8c53
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/main/kotlin/xyz/cssxsh/bilibili/data/dynamic/RoomInfo.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package xyz.cssxsh.bilibili.data.dynamic

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*

@Serializable
data class RoomInfo(
Expand All @@ -14,23 +11,7 @@ data class RoomInfo(
@SerialName("type")
val type: Int
) {
object AsStringSerializer : KSerializer<RoomInfo> {

override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor(this::class.qualifiedName!!, PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: RoomInfo) {
val json = Json { serializersModule = encoder.serializersModule }
val text = json.encodeToString(serializer = serializer(), value = value)
encoder.encodeString(value = text)
}

override fun deserialize(decoder: Decoder): RoomInfo {
val json = Json { serializersModule = decoder.serializersModule }
val text = decoder.decodeString()
return json.decodeFromString(deserializer = serializer(), string = text)
}
}
object AsStringSerializer : KSerializer<RoomInfo> by WarpStringSerializer(original = serializer())

@Serializable
data class LivePlay(
Expand Down Expand Up @@ -58,7 +39,7 @@ data class RoomInfo(
@SerialName("parent_area_name")
val parentAreaName: String,
@SerialName("pendants")
val pendants: JsonElement, // TODO
val pendants: Pendants,
@SerialName("play_type")
val playType: Int,
@SerialName("room_id")
Expand All @@ -72,7 +53,25 @@ data class RoomInfo(
@SerialName("uid")
val uid: Long,
@SerialName("watched_show")
val watchedShow: WatchedShow = WatchedShow()
val watched: WatchedShow = WatchedShow()
)

@Serializable
data class Pendants(
@SerialName("list")
val list: List<Pendant>?
)

@Serializable
data class Pendant(
@SerialName("id")
val id: Long,
@SerialName("name")
val name: Long,
@SerialName("image")
val image: String,
@SerialName("jumpUrl")
val jumpUrl: String
)

@Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package xyz.cssxsh.bilibili.data.dynamic

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*

@PublishedApi
internal class WarpStringSerializer<T>(private val original: KSerializer<T>) : KSerializer<T> {

override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor(this::class.qualifiedName!!, PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: T) {
val json = (encoder as JsonEncoder).json
val text = json.encodeToString(serializer = original, value = value)
encoder.encodeString(value = text)
}

override fun deserialize(decoder: Decoder): T {
val json = (decoder as JsonDecoder).json
val text = decoder.decodeString()
return json.decodeFromString(deserializer = original, string = text)
}
}

0 comments on commit 34a8c53

Please sign in to comment.