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

2

5

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?

Christcrossrow answered 23/5, 2019 at 10:22 Comment(0)
M
7

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

Melcher answered 23/5, 2019 at 11:22 Comment(0)
C
1

There is even simpler solution for this. If you want to handle case class JSON encoding for HTTP responses, you just can add these imports:

import io.circe.generic.auto._
import org.http4s.circe.CirceEntityCodec._

BTW, the same imports handle decoding of incoming JSON requests into case classes as well

Christcrossrow answered 12/6, 2019 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.