CatCode2 针对 kotlinx-serialization 所实现的序列化模块。 CatCode2 Serialization Kotlinx 允许通过实体类作为一个猫猫码的基本载体使用。
@Serializable
@SerialName("at") // or use @CatCodeTypeName("at")
data class At(val code: Long, val name: String)
@Test
fun sample() {
val at = At(123456L, "forte")
val catcode = CatCode.encodeToString(At.serializer(), at)
val newAt = CatCode.decodeFromString(At.serializer(), catcode)
assertTrue { at == newAt } // pass.
println(catcode) // [CAT:at,code=123456,name=forte]
val cqcode = CatCode.encodeToString(At.serializer(), at, "CQ")
println(cqcode) // [CQ:at,code=123456,name=forte]
}