parseJsonArray

inline fun <T : Any> Flow<ByteArray>.parseJsonArray(objectMapper: ObjectMapper = defaultObjectMapper): Flow<T>

Converts a Flow of byte arrays, representing a JSON array, into a Flow of parsed JSON objects of a specified type.

This function reads from a Flow of byte arrays, treating the incoming data as a JSON array. As the array is streamed in, each JSON object it contains is parsed into an instance of type T and emitted as soon as it's fully received.

This approach allows for memory-efficient and backpressure-aware processing of large JSON arrays, even when the data source is slow (e.g., a network call).

Return

A Flow of parsed objects of type T.

Example usage:

val jsonBytesFlow = flowOf(
"[",
"""{"name":"Alice"""",
""","age":30},""",
"""{"name":"Bob"""",
""","age":25}""",
"]"
).map { it.toByteArray() }

jsonBytesFlow
.rootJsonNodes()
.collect { jsonNode -> println(jsonNode) }

Parameters

T

The type into which JSON objects should be parsed. This is identified at runtime using reified type parameters.

objectMapper

The ObjectMapper to use for JSON parsing. If not specified, a default ObjectMapper is used.