-
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.
Support for encoding/decoding a sequence of values (#20)
* Support for streaming via Reader and Appendable * Handle Microsoft Excel's insistence on using a byte order marker * Cleaning up new unit tests for FetchSourceTest * Removed commented debugging println's for FetchSource * Sequence encoding and decoding * Streaming serialization bug fix * Cleanup formatting * Refactor asynchronous API to `CsvRecordReader` and `CsvRecordWriter` * Fix blocking initialization of readers --------- Co-authored-by: Sven Obser <[email protected]>
- Loading branch information
1 parent
5a358d8
commit bf89d3c
Showing
8 changed files
with
304 additions
and
80 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
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
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
Oops, something went wrong.