decodeStringChunked

abstract fun decodeStringChunked(consumeChunk: (chunk: String) -> Unit)(source)

Method allows decoding a string value by fixed-size chunks. Usable for handling very large strings that may not fit in memory. Chunk size is guaranteed to not exceed 16384 chars (but it may be smaller than that). Feeds string chunks to the provided consumer.

Parameters

consumeChunk
  • lambda function to handle string chunks

Example usage:

@Serializable(with = LargeStringSerializer::class)
data class LargeStringData(val largeString: String)

@Serializable
data class ClassWithLargeStringDataField(val largeStringField: LargeStringData)

object LargeStringSerializer : KSerializer<LargeStringData> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LargeStringContent", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): LargeStringData {
require(decoder is ChunkedDecoder) { "Only chunked decoder supported" }

val tmpFile = createTempFile()
val writer = FileWriter(tmpFile.toFile()).use {
decoder.decodeStringChunked { chunk ->
writer.append(chunk)
}
}
return LargeStringData("file://${tmpFile.absolutePathString()}")
}
}

In this sample, we need to be able to handle a huge string coming from json. Instead of storing it in memory, we offload it into a file and return the file name instead