-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor asynchronous API to
CsvRecordReader
and CsvRecordWriter
- Loading branch information
Sven Obser
committed
Dec 13, 2024
1 parent
515d5c6
commit ea7f0c1
Showing
7 changed files
with
227 additions
and
157 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
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
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
48 changes: 48 additions & 0 deletions
48
library/src/main/kotlin/kotlinx/serialization/csv/CsvRecordReader.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,48 @@ | ||
package kotlinx.serialization.csv | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.csv.decode.CsvReader | ||
import kotlinx.serialization.csv.decode.FetchSource | ||
import kotlinx.serialization.csv.decode.RecordListCsvDecoder | ||
import kotlinx.serialization.encoding.CompositeDecoder.Companion.DECODE_DONE | ||
import java.io.Reader | ||
|
||
/** | ||
* Record reader that allows reading CSV line-by-line. | ||
*/ | ||
interface CsvRecordReader<T : Any> : Iterator<T> { | ||
/** | ||
* Read next record | ||
*/ | ||
fun read(): T? = if (hasNext()) next() else null | ||
} | ||
|
||
/** | ||
* Parse CSV line-by-line from the given [input]. | ||
* | ||
* @param deserializer The deserializer used to parse the given CSV string. | ||
* @param input The CSV reader to parse. This function *does not close the reader*. | ||
*/ | ||
@ExperimentalSerializationApi | ||
fun <T : Any> Csv.recordReader(deserializer: KSerializer<T>, input: Reader): CsvRecordReader<T> { | ||
val decoder = RecordListCsvDecoder( | ||
csv = this, | ||
reader = CsvReader(FetchSource(input), config) | ||
) | ||
val listDescriptor = ListSerializer(deserializer).descriptor | ||
var previousValue: T? = null | ||
|
||
return object : CsvRecordReader<T> { | ||
override fun hasNext(): Boolean = | ||
decoder.decodeElementIndex(listDescriptor) != DECODE_DONE | ||
|
||
override fun next(): T { | ||
val index = decoder.decodeElementIndex(listDescriptor) | ||
return decoder.decodeSerializableElement(listDescriptor, index, deserializer, previousValue).also { | ||
previousValue = it | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
library/src/main/kotlin/kotlinx/serialization/csv/CsvRecordWriter.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,34 @@ | ||
package kotlinx.serialization.csv | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.csv.encode.CsvWriter | ||
import kotlinx.serialization.csv.encode.RecordListCsvEncoder | ||
|
||
/** | ||
* Record writer that allows writing CSV line by line. | ||
*/ | ||
fun interface CsvRecordWriter<T : Any> { | ||
/** | ||
* Write next record. | ||
*/ | ||
fun write(record: T) | ||
} | ||
|
||
/** | ||
* Create [CsvRecordWriter] that allows writing CSV line-by-line. | ||
* | ||
* @param serializer The serializer used to serialize the given object. | ||
* @param output The output where the CSV will be written. | ||
*/ | ||
@ExperimentalSerializationApi | ||
fun <T : Any> Csv.recordWriter(serializer: KSerializer<T>, output: Appendable): CsvRecordWriter<T> { | ||
val encoder = RecordListCsvEncoder( | ||
csv = this, | ||
writer = CsvWriter(output, config) | ||
) | ||
|
||
return CsvRecordWriter { | ||
encoder.encodeSerializableValue(serializer, it) | ||
} | ||
} |
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
Oops, something went wrong.