hi, I'm looking at application/x-ndjson which is b...
# serialization
b
hi, I'm looking at application/x-ndjson which is basically just newline separated json, is there support for that built in?
✅ 1
e
I think that would be most useful in streams, where kxs doesn't really help anyhow. wouldn't you want to encode/decode each line separately?
b
I'm gonna push this with KTOR, so right now I'm looking into manually serializing and string concatenating everything
I've got an API that unfortunately chose that format
e
if it's small, sure. otherwise you could just write each line (same as how you'd produce ndjson in basically any other framework)
Copy code
call.respondTextWriter {
    results.collect {
        appendLine(Json.encodeToString(it))
b
I need to POST that
I don't think ktor offers a way to stream POST requests to a server?
b
thanks
I guess
Copy code
suspend fun <I : Any> indexMany(items: Iterable<I>, type: KType) {
        <http://client.post|client.post>(config.baseUrl) {
            url {
                appendPathSegments("api", "v1", "collections", config.collection, "documents", "import")
            }
            headers {
                append("X-BI-API-KEY", config.apiKey)
            }
            val serializer = serializer(type)
            val body = ChannelWriterContent(
                body = {
                    var first = true
                    items.forEach {
                        if (!first) {
                            writeStringUtf8("\n")
                        }
                        writeStringUtf8(json.encodeToString(serializer, it))
                        first = false
                    }
                },
                contentType = ContentType("application", "x-ndjson"),
            )
            setBody(body)
            contentType(ContentType.Application.Json)
        }
    }
e
consumers should handle trailing newline - it's how all UNIX text file formats work. so simply
Copy code
for (item in items) {
    writeStringUtf8(json.encodeToString(serializer, item))
    writeStringUtf8("\n")
}
without
first
b
so trailing new lines are fine?
thanks
e
1. usually
\n
is seen as a line terminator, not a record separator 2. even if it weren't, ndjson ignores empty lines
❤️ 1