All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.format.ConfigurableJacksonYaml.kt Maven / Gradle / Ivy

There is a newer version: 5.41.0.0
Show newest version
package org.http4k.format

import com.fasterxml.jackson.databind.ObjectMapper
import org.http4k.core.Body
import org.http4k.core.ContentType.Companion.TEXT_YAML
import org.http4k.lens.BiDiBodyLensSpec
import org.http4k.lens.ContentNegotiation
import org.http4k.lens.ContentNegotiation.Companion.None
import org.http4k.lens.string
import org.http4k.websocket.WsMessage
import kotlin.reflect.KClass

open class ConfigurableJacksonYaml(val mapper: ObjectMapper) : AutoMarshalling() {
    override fun  asA(input: String, target: KClass): T = mapper.readValue(input, target.java)

    override fun asFormatString(input: Any): String = mapper.writeValueAsString(input)

    inline fun  WsMessage.Companion.auto() = WsMessage.string().map(mapper.read(), mapper.write())

    inline fun  Body.Companion.auto(description: String? = null, contentNegotiation: ContentNegotiation = None) = autoBody(description, contentNegotiation)

    inline fun  autoBody(description: String? = null, contentNegotiation: ContentNegotiation = None): BiDiBodyLensSpec = httpBodyLens(description, contentNegotiation, TEXT_YAML).map(mapper.read(), mapper.write())
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy