<commit_before>package io.quarkus.kotlin.serialization

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import kotlinx.serialization.serializer
import org.jboss.resteasy.reactive.common.providers.serialisers.JsonMessageBodyWriterUtil
import org.jboss.resteasy.reactive.server.providers.serialisers.json.JsonMessageServerBodyWriterUtil
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter.AllWriteableMessageBodyWriter
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext
import java.io.OutputStream
import java.lang.reflect.Type
import javax.inject.Inject
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap

@Produces("application/json", "application/*+json", "text/json")
@OptIn(ExperimentalSerializationApi::class)
class KotlinSerializationMessageBodyWriter(@Inject var json: Json) : AllWriteableMessageBodyWriter() {
    override fun writeTo(
        o: Any, type: Class<*>, genericType: Type, annotations: Array<Annotation>, mediaType: MediaType,
        httpHeaders: MultivaluedMap<String, Any>, entityStream: OutputStream
    ) {
        JsonMessageBodyWriterUtil.setContentTypeIfNecessary(httpHeaders)
        if (o is String) { // YUK: done in order to avoid adding extra quotes...
            entityStream.write(o.toByteArray())
        } else {
            json.encodeToStream(o, entityStream)
        }
    }

    override fun writeResponse(o: Any, genericType: Type, context: ServerRequestContext) {
        JsonMessageServerBodyWriterUtil.setContentTypeIfNecessary(context)
        val originalStream = context.orCreateOutputStream
        val stream: OutputStream = NoopCloseAndFlushOutputStream(originalStream)

        if (o is String) { // YUK: done in order to avoid adding extra quotes...
            stream.write(o.toByteArray())
        } else {
            val encodeToString = json.encodeToString(json.serializersModule.serializer(genericType), o)
            stream.write(encodeToString.toByteArray())
        }
        // we don't use try-with-resources because that results in writing to the http output without the exception mapping coming into play
        originalStream.close()
    }
<commit_msg>use Json.encodeToStream<commit_after>package io.quarkus.kotlin.serialization

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import kotlinx.serialization.serializer
import org.jboss.resteasy.reactive.common.providers.serialisers.JsonMessageBodyWriterUtil
import org.jboss.resteasy.reactive.server.providers.serialisers.json.JsonMessageServerBodyWriterUtil
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter.AllWriteableMessageBodyWriter
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext
import java.io.OutputStream
import java.lang.reflect.Type
import javax.inject.Inject
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap

@Produces("application/json", "application/*+json", "text/json")
@OptIn(ExperimentalSerializationApi::class)
class KotlinSerializationMessageBodyWriter(@Inject var json: Json) : AllWriteableMessageBodyWriter() {
    override fun writeTo(
        o: Any, type: Class<*>, genericType: Type, annotations: Array<Annotation>, mediaType: MediaType,
        httpHeaders: MultivaluedMap<String, Any>, entityStream: OutputStream
    ) {
        JsonMessageBodyWriterUtil.setContentTypeIfNecessary(httpHeaders)
        if (o is String) { // YUK: done in order to avoid adding extra quotes...
            entityStream.write(o.toByteArray())
        } else {
            json.encodeToStream(o, entityStream)
        }
    }

    override fun writeResponse(o: Any, genericType: Type, context: ServerRequestContext) {
        JsonMessageServerBodyWriterUtil.setContentTypeIfNecessary(context)
        val originalStream = context.orCreateOutputStream
        val stream: OutputStream = NoopCloseAndFlushOutputStream(originalStream)

        if (o is String) { // YUK: done in order to avoid adding extra quotes...
            stream.write(o.toByteArray())
        } else {
            json.encodeToStream(serializer(genericType), o, stream)
        }
        // we don't use try-with-resources because that results in writing to the http output without the exception mapping coming into play
        originalStream.close()
    }

{'fraction_non_alphanumeric': 0.08598657136668833, 'fraction_numerical': 0.0, 'mean_word_length': 4.295871559633028, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7596350', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1253, 'n_words': 269}