I am trying to change the implementation of this function from using plays json library like so
def apply[T](action: => ApiResponse[T])(implicit tjs: Writes[T], ec: ExecutionContext): Future[Result] = {
action.fold(
err =>
Status(err.statusCode) {
JsObject(Seq(
"status" -> JsString("error"),
"statusCode" -> JsNumber(err.statusCode),
"errors" -> Json.toJson(err.errors)
))
},
t =>
Ok {
JsObject(Seq(
"status" -> JsString("ok"),
"response" -> Json.toJson(t)
))
}
)
}
to use argonaut like so
def apply[T](action: => ApiResponse[T])(implicit encodeJson: EncodeJson[T], ec: ExecutionContext): Future[Result] = {
action.fold(
err =>
Status(err.statusCode) {
Json(
"status" -> jString("error"),
"statusCode" -> jNumber(err.statusCode),
"errors" -> err.errors.asJson
)
},
t =>
Ok {
Json(
"status" -> jString("ok"),
"response" -> t.asJson
)
}
)
}
but I get
Cannot write an instance of argonaut.Json to HTTP response. Try to define a Writeable[argonaut.Json]
for both the Status{} block and Ok{} block and I got a helpful answer to this problem here https://groups.google.com/forum/#!topic/play-framework/vBMf72a10Zc
so I tried to create the implicit conversion like so
implicit def writeableOfArgonautJson(implicit codec: Codec): Writeable[Json] = {
Writeable(jsval => codec.encode(jsval.toString))
}
which I think converts the json object to a string and provides it to codec.encode which should convert it to Array[Bytes] but I get
Cannot guess the content type to use for argonaut.Json. Try to define a ContentTypeOf[argonaut.Json]
jsval.nospaces.getBytes also return Array[Bytes] so I dont know if that can be used to help at all
so while I think that last error message means I just need to tell play that it should use content type application.json I also feel like this might be an unnecessary rabbit hole and there should be an easier way to do this.
edit: it wasnt such a rabbit hole as defining the contentType has things compiling at least but I still would like to know if this is correct