generated from project-mirai/mirai-console-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/xyz/cssxsh/bilibili/data/dynamic/WarpStringSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |