-
Notifications
You must be signed in to change notification settings - Fork 613
/
PackedArrayDecoder.kt
32 lines (26 loc) · 1.2 KB
/
PackedArrayDecoder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package kotlinx.serialization.protobuf.internal
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.protobuf.*
@OptIn(ExperimentalSerializationApi::class)
internal class PackedArrayDecoder(
proto: ProtoBuf,
reader: ProtobufReader,
descriptor: SerialDescriptor,
) : ProtobufDecoder(proto, reader, descriptor) {
private var nextIndex: Int = 0
// Tags are omitted in the packed array format
override fun SerialDescriptor.getTag(index: Int): ProtoDesc = MISSING_TAG
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
throw SerializationException("Packing only supports primitive number types. The input type however was a struct: $descriptor")
}
override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
// We need eof here as there is no tag to read in packed form.
if (reader.eof) return CompositeDecoder.DECODE_DONE
return nextIndex++
}
override fun decodeTaggedString(tag: ProtoDesc): String {
throw SerializationException("Packing only supports primitive number types. The actual reading is for string.")
}
}