Support Lists

This commit is contained in:
Him188 2020-03-06 19:09:34 +08:00
parent 2260d331d4
commit 92df108740
2 changed files with 23 additions and 25 deletions

View File

@ -36,12 +36,14 @@ internal class JceDecoder(
) )
} }
fun SerialDescriptor.getJceTagId(index: Int): Int { private fun SerialDescriptor.getJceTagId(index: Int): Int {
return getElementAnnotations(index).filterIsInstance<JceId>().single().id return getElementAnnotations(index).filterIsInstance<JceId>().single().id
} }
private val ByteArraySerializer = ByteArraySerializer() companion object {
private val ByteArraySerializer: KSerializer<ByteArray> = ByteArraySerializer()
}
// TODO: 2020/3/6 can be object // TODO: 2020/3/6 can be object
private inner class SimpleByteArrayReader : CompositeDecoder by this { private inner class SimpleByteArrayReader : CompositeDecoder by this {
@ -64,16 +66,14 @@ internal class JceDecoder(
// TODO: 2020/3/6 can be object // TODO: 2020/3/6 can be object
private inner class ListReader : CompositeDecoder by this { private inner class ListReader : CompositeDecoder by this {
override fun decodeSequentially(): Boolean = true override fun decodeSequentially(): Boolean = true
override fun decodeElementIndex(descriptor: SerialDescriptor): Int { override fun decodeElementIndex(descriptor: SerialDescriptor): Int = error("should not be reached")
error("should not be reached")
}
override fun endStructure(descriptor: SerialDescriptor) { override fun endStructure(descriptor: SerialDescriptor) {
println("endStructure: ${descriptor.serialName}") println("endStructure: ${descriptor.serialName}")
} }
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int { override fun decodeCollectionSize(descriptor: SerialDescriptor): Int {
return jce.useHead { jce.readJceIntValue(it) } // 不读下一个 head
return jce.currentHead.let { jce.readJceIntValue(it) }.also { println("listSize=$it") }
} }
} }
@ -86,24 +86,23 @@ internal class JceDecoder(
override fun beginStructure(descriptor: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeDecoder { override fun beginStructure(descriptor: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeDecoder {
println("beginStructure: ${descriptor.serialName}") println("beginStructure: ${descriptor.serialName}")
if (descriptor == ByteArraySerializer.descriptor) {
println("!! ByteArray")
println("decoderTag: $currentTagOrNull")
println("jceHead: " + jce.currentHeadOrNull)
return jce.skipToHeadAndUseIfPossibleOrFail(popTag().id) {
println("listHead: $it")
when (it.type) {
Jce.SIMPLE_LIST -> SimpleByteArrayReader().also { jce.prepareNextHead() } // 无用的元素类型
Jce.LIST -> error(""); //ListReader()
else -> error("type mismatch. Expected SIMPLE_LIST or LIST, got ${it.type} instead")
}
}
}
return when (descriptor.kind) { return when (descriptor.kind) {
StructureKind.MAP -> { StructureKind.MAP -> {
error("map") error("map")
} }
StructureKind.LIST -> ListReader() StructureKind.LIST -> {
println("!! ByteArray")
println("decoderTag: $currentTagOrNull")
println("jceHead: " + jce.currentHeadOrNull)
return jce.skipToHeadAndUseIfPossibleOrFail(popTag().id) {
println("listHead: $it")
when (it.type) {
Jce.SIMPLE_LIST -> SimpleByteArrayReader().also { jce.prepareNextHead() } // 无用的元素类型
Jce.LIST -> ListReader()
else -> error("type mismatch. Expected SIMPLE_LIST or LIST, got ${it.type} instead")
}
}
}
else -> this@JceDecoder else -> this@JceDecoder
} }

View File

@ -45,7 +45,7 @@ internal class JceInputTest {
@Serializable @Serializable
data class TestSerializableClassA( data class TestSerializableClassA(
@JceId(0) val byteArray: ByteArray = byteArrayOf(1, 2, 3), @JceId(0) val byteArray: ByteArray = byteArrayOf(1, 2, 3),
@JceId(3) val byteArray2: ByteArray = byteArrayOf(1, 2, 3, 4) @JceId(3) val byteArray2: List<Byte> = listOf(1, 2, 3, 4)
) { ) {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
@ -54,14 +54,13 @@ internal class JceInputTest {
other as TestSerializableClassA other as TestSerializableClassA
if (!byteArray.contentEquals(other.byteArray)) return false if (!byteArray.contentEquals(other.byteArray)) return false
if (!byteArray2.contentEquals(other.byteArray2)) return false if (byteArray2 != other.byteArray2) return false
return true return true
} }
override fun hashCode(): Int { override fun hashCode(): Int {
var result = byteArray.contentHashCode() var result = byteArray.contentHashCode()
result = 31 * result + byteArray2.contentHashCode() result = 31 * result + byteArray2.hashCode()
return result return result
} }
} }