Skip to content

Commit

Permalink
Merge branch 'main' into sequences
Browse files Browse the repository at this point in the history
# Conflicts:
#	library/src/main/kotlin/kotlinx/serialization/csv/Csv.kt
  • Loading branch information
Sven Obser committed Dec 13, 2024
2 parents 4c1f95f + 5a358d8 commit 515d5c6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 44 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Update to Kotlin 2.1.0.
- Update to Kotlin 2.1.0 (🏅 kudos to theyoz).
- Update to Kotlinx-Serialization 1.7.3.
- Make `CsvConfig` public.
- Support for streaming via Reader and Appendable (🏅 kudos to UnknownJoe796).
- Handle Microsoft Excel's insistence on using a byte order marker (🏅 kudos to UnknownJoe796).

### Removed

Expand Down
75 changes: 32 additions & 43 deletions library/src/main/kotlin/kotlinx/serialization/csv/Csv.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ sealed class Csv(val config: CsvConfig) : StringFormat {
output.encode(serializer, value)
}

/**
* Serialize [values] into CSV record(s).
*
* @param serializer The serializer used to serialize the given object.
* @param values The [Serializable] objects as a sequence.
* @param appendable The output where the CSV will be written.
*/
fun <T> encodeSequenceToAppendable(serializer: KSerializer<T>, values: Sequence<T>, appendable: Appendable) {
values.forEach(beginEncodingToAppendable(serializer, appendable))
}

/**
* Start serializing values into CSV record(s).
*
Expand Down Expand Up @@ -107,38 +96,6 @@ sealed class Csv(val config: CsvConfig) : StringFormat {
fun <T> decodeFrom(deserializer: DeserializationStrategy<T>, input: Reader): T =
FetchSource(input).decode(deserializer)

/**
* Serialize [value] into CSV record(s).
*
* @param serializer The serializer used to serialize the given object.
* @param value The [Serializable] object.
*/
private fun <T> Appendable.encode(serializer: SerializationStrategy<T>, value: T) {
RootCsvEncoder(
csv = this@Csv,
output = this
).encodeSerializableValue(serializer, value)
}

/**
* Parse CSV from [this] input into [Serializable] object.
*
* @param deserializer The deserializer used to parse the given CSV string.
*/
private fun <T> Source.decode(deserializer: DeserializationStrategy<T>): T {
val reader = CsvReader(
source = this,
config = config
)

return RootCsvDecoder(
csv = this@Csv,
reader = reader,
).decodeSerializableValue(deserializer).also {
require(reader.isDone) { "Reader has not consumed the whole input: $reader" }
}
}

/**
* Parse CSV line-by-line from the given [reader] into a sequence.
*
Expand Down Expand Up @@ -182,6 +139,38 @@ sealed class Csv(val config: CsvConfig) : StringFormat {
}
}

/**
* Serialize [value] into CSV record(s).
*
* @param serializer The serializer used to serialize the given object.
* @param value The [Serializable] object.
*/
private fun <T> Appendable.encode(serializer: SerializationStrategy<T>, value: T) {
RootCsvEncoder(
csv = this@Csv,
output = this
).encodeSerializableValue(serializer, value)
}

/**
* Parse CSV from [this] input into [Serializable] object.
*
* @param deserializer The deserializer used to parse the given CSV string.
*/
private fun <T> Source.decode(deserializer: DeserializationStrategy<T>): T {
val reader = CsvReader(
source = this,
config = config
)

return RootCsvDecoder(
csv = this@Csv,
reader = reader,
).decodeSerializableValue(deserializer).also {
require(reader.isDone) { "Reader has not consumed the whole input: $reader" }
}
}

internal class Impl(config: CsvConfig) : Csv(config)

/**
Expand Down

0 comments on commit 515d5c6

Please sign in to comment.