
org.http4s.circe.CirceInstances.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http4s-circe_2.11 Show documentation
Show all versions of http4s-circe_2.11 Show documentation
Provides Circe codecs for http4s
package org.http4s
package circe
import io.circe.{Encoder, Decoder, Json, Printer}
import io.circe.jawn.CirceSupportParser.facade
import org.http4s.headers.`Content-Type`
// Originally based on ArgonautInstances
trait CirceInstances {
implicit val jsonDecoder: EntityDecoder[Json] = jawn.jawnDecoder(facade)
def jsonOf[A](implicit decoder: Decoder[A]): EntityDecoder[A] =
jsonDecoder.flatMapR { json =>
decoder.decodeJson(json).fold(
failure =>
DecodeResult.failure(InvalidMessageBodyFailure(s"Could not decode JSON: $json", Some(failure))),
DecodeResult.success(_)
)
}
implicit val jsonEncoder: EntityEncoder[Json] =
EntityEncoder[String].contramap[Json] { json =>
// Comment from ArgonautInstances (which this code is based on):
// TODO naive implementation materializes to a String.
// See https://github.com/non/jawn/issues/6#issuecomment-65018736
Printer.noSpaces.pretty(json)
}.withContentType(`Content-Type`(MediaType.`application/json`))
def jsonEncoderOf[A](implicit encoder: Encoder[A]): EntityEncoder[A] =
jsonEncoder.contramap[A](encoder.apply)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy