io.quarkus.resteasy.reactive.kotlin.serialization.runtime.KotlinSerializationMessageBodyReader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-resteasy-reactive-kotlin-serialization Show documentation
Show all versions of quarkus-resteasy-reactive-kotlin-serialization Show documentation
Kotlin Serialization support for RESTEasy Reactive. This extension is not compatible with the quarkus-resteasy extension, or any of the extensions that depend on it.
package io.quarkus.resteasy.reactive.kotlin.serialization.runtime
import jakarta.ws.rs.core.MediaType
import jakarta.ws.rs.core.MultivaluedMap
import java.io.InputStream
import java.lang.reflect.Type
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.serializer
import org.jboss.resteasy.reactive.common.providers.serialisers.AbstractJsonMessageBodyReader
import org.jboss.resteasy.reactive.common.util.StreamUtil
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext
class KotlinSerializationMessageBodyReader(private val json: Json) :
AbstractJsonMessageBodyReader(), ServerMessageBodyReader {
override fun isReadable(
type: Class<*>,
genericType: Type,
annotations: Array?,
mediaType: MediaType
) = isReadable(mediaType, type)
override fun isReadable(
type: Class<*>,
genericType: Type,
lazyMethod: ResteasyReactiveResourceInfo,
mediaType: MediaType
) = isReadable(mediaType, type)
override fun readFrom(
type: Class,
genericType: Type,
annotations: Array,
mediaType: MediaType,
httpHeaders: MultivaluedMap,
entityStream: InputStream
): Any? {
return doReadFrom(type, entityStream)
}
override fun readFrom(
type: Class,
genericType: Type,
mediaType: MediaType,
context: ServerRequestContext
): Any? {
return doReadFrom(type, context.inputStream)
}
@ExperimentalSerializationApi
private fun doReadFrom(type: Class, entityStream: InputStream): Any? {
return if (StreamUtil.isEmpty(entityStream)) null
else {
json.decodeFromStream(serializer(type), entityStream)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy