Instead of using TurnJsonIntoHttp in each place:
class MyEndpoint extends Endpoint[HttpRequest, HttpResponse] {
def route = {
case Method.Get -> Root / "items" ⇒
List() ! TurnJsonIntoHttp[List[MyObj]]
case Method.Get -> Root / "item" / Long(id) ⇒
GetDetail(id) ! TurnJsonIntoHttp[MyObj]
//...
}
}
==========
We could do something like:
implicit class ToJsonOps[Req, Resp](service: Service[Req, Resp]) {
def asJson(implicit encode: EncodeJson[Resp]) =
new Service[Req, HttpResponse] {
def apply(req: Req) = service(req) flatMap TurnJsonIntoHttp[Resp]
}
}
And in route:
class MyEndpoint extends Endpoint[HttpRequest, HttpResponse] {
def route = {
case Method.Get -> Root / "items" ⇒
List().asJson
case Method.Get -> Root / "item" / Long(id) ⇒
GetDetail(id).asJson
//...
}
}
Right. I see the point.
Since, you don't have this intermediate JSON AST layer, you probably don't need endpoints returning the JSON type. You can do HTTP straightforward. So, you have implicit decoders for
MyObj
andList[MyObj]
types that convert them to JSON string. But, can we do better here? What about to have an implicit functiondef jsonToHttp(...)
converting aService[HttpRequest, A]
inService[HttpRequest, HttpResponse]
having an implicit decoder imported. It's almost the same that you suggested, but w/o call toasJson
.We would have something like this: