kotlinx.serialization.csv.decode.CollectionRecordCsvDecoder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-serialization-csv Show documentation
Show all versions of kotlinx-serialization-csv Show documentation
Library to easily use Kotlin Serialization to serialize to/from CSV.
package kotlinx.serialization.csv.decode
import kotlinx.serialization.csv.Csv
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.CompositeDecoder
/**
* Decode collection record.
*
* If the CSV record is a collection (list, set, map) the collection elements fill the whole line.
* Therefore, the number of elements is determined by reading until the end of line and the size of the collection is
* not required and consequently not expected as the first value.
*/
internal class CollectionRecordCsvDecoder(
csv: Csv,
reader: CsvReader,
parent: RecordListCsvDecoder
) : CsvDecoder(csv, reader, parent) {
private var elementIndex = 0
private val recordNo = reader.recordNo
override fun decodeElementIndex(descriptor: SerialDescriptor): Int = when {
// TODO Check for END_OF_RECORD
reader.isDone || reader.recordNo != recordNo -> CompositeDecoder.DECODE_DONE
else -> elementIndex
}
override fun endChildStructure(descriptor: SerialDescriptor) {
super.endChildStructure(descriptor)
elementIndex++
}
override fun decodeColumn(): String {
val value = super.decodeColumn()
elementIndex++
return value
}
}