
com.jakewharton.rs.kotlinx.serialization.KotlinxSerializationMessageBodyReader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jax-rs-kotlinx-serialization Show documentation
Show all versions of jax-rs-kotlinx-serialization Show documentation
Message body reader/writer and parameter converter which uses Kotlinx Serialization
@file:JvmName("KotlinxSerializationMessageBodyReader")
package com.jakewharton.rs.kotlinx.serialization
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.StringFormat
import kotlinx.serialization.serializerByTypeToken
import java.io.InputStream
import java.lang.reflect.Type
import java.nio.charset.Charset
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.ext.MessageBodyReader
@JvmName("create")
fun StringFormat.asMessageBodyReader(vararg mediaTypes: MediaType): MessageBodyReader {
return StringFormatMessageBodyReader(this, mediaTypes.toSet())
}
@JvmName("create")
fun BinaryFormat.asMessageBodyReader(vararg mediaTypes: MediaType): MessageBodyReader {
return BinaryFormatMessageBodyReader(this, mediaTypes.toSet())
}
private class StringFormatMessageBodyReader(
private val format: StringFormat,
private val mediaTypes: Set
) : MessageBodyReader {
override fun isReadable(
type: Class<*>,
genericType: Type,
annotations: Array?,
mediaType: MediaType
): Boolean {
return mediaType in mediaTypes
}
override fun readFrom(
type: Class,
genericType: Type,
annotations: Array?,
mediaType: MediaType,
httpHeaders: MultivaluedMap,
entityStream: InputStream
): Any {
val charset = try {
mediaType.parameters["charset"]?.let(Charset::forName)
} catch (e: RuntimeException) {
null
} ?: Charsets.UTF_8
val string = entityStream.readBytes().toString(charset) // Not closing per parameter Javadoc.
return format.parse(serializerByTypeToken(genericType), string)
}
}
private class BinaryFormatMessageBodyReader(
private val format: BinaryFormat,
private val mediaTypes: Set
) : MessageBodyReader {
override fun isReadable(
type: Class<*>?,
genericType: Type?,
annotations: Array?,
mediaType: MediaType?
): Boolean {
return mediaType in mediaTypes
}
override fun readFrom(
type: Class,
genericType: Type,
annotations: Array?,
mediaType: MediaType,
httpHeaders: MultivaluedMap,
entityStream: InputStream
): Any {
val bytes = entityStream.readBytes() // Not closing per parameter Javadoc.
return format.load(serializerByTypeToken(genericType), bytes)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy