Search code examples
scalahttp4szio

What is the right way to send JSON response in http4s?


Not so long time ago I switched from akka-http to http4s. One of the basic things which I wanted to do correctly — JSON handling, in particular sending a JSON response.

I decided to use http4s with ZIO instead of cats, so here is how an http route looks like:

import fs2.Stream
import org.http4s._
import org.http4s.dsl.io._
import org.http4s.implicits._
import scalaz.zio.Task
import scalaz.zio.interop.catz._
import io.circe.generic.auto._
import io.circe.syntax._

class TweetsRoutes {

  case class Tweet(author: String, tweet: String)

  val helloWorldService = HttpRoutes.of[Task] {
    case GET -> Root / "hello" / name => Task {
      Response[Task](Ok)
        .withBodyStream(Stream.emits(
          Tweet(name, "dummy tweet text").asJson.toString.getBytes
        ))
    }
  }.orNotFound

}

As you see, JSON serialization part is pretty verbose:

.withBodyStream(Stream.emits(
  Tweet(name, "dummy tweet text").asJson.toString.getBytes
))

Is there any other way to send JSON in a response?


Solution

  • Yes, there is: define and Encoder and Decoder for Task:

    implicit def circeJsonDecoder[A](
          implicit decoder: Decoder[A]
      ): EntityDecoder[Task, A] = jsonOf[Task, A]
      implicit def circeJsonEncoder[A](
          implicit encoder: Encoder[A]
      ): EntityEncoder[Task, A] = jsonEncoderOf[Task, A]
    

    this way there is no need to transform to bytes.

    EDIT: there is a full example here: https://github.com/mschuwalow/zio-todo-backend/blob/develop/src/main/scala/com/schuwalow/zio/todo/http/TodoService.scala

    HT: @mschuwalow