Creating a record [Argonaut.Json] to respond to the HTTP interface for the game

I am trying to change the implementation of this function using the json play library, for example:

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)
          ))
        }
    )
  }

use argonaut 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 argonaut.Json argument instance for HTTP. Try defining Recordable [argonaut.Json]

for the Status block {} and Ok {}, and I got a useful answer to this problem here https://groups.google.com/forum/#!topic/play-framework/vBMf72a10Zc

so I tried to create an implicit conversion this way

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 its .encode codec, which should convert it to Array [Bytes], but I get

, argonaut.Json. a ContentTypeOf [argonaut.Json]

jsval.nospaces.getBytes Array [Bytes], ,

, , , , application.json, , , .

edit: , contentType , , , .

+4
1

, , , Writable[A]:

  • A Array[Bytes]
  • ,

Codec, ContentTypeOf[A], A is argonaunt.Json:

implicit def contentTypeOf_ArgonautJson(implicit codec: Codec): ContentTypeOf[argonaut.Json] = {
  ContentTypeOf[argonaut.Json](Some(ContentTypes.JSON))
}

Writable[A], A, ContentTypeOf[A] ( ):

implicit def writeableOf_ArgonautJson(implicit codec: Codec): Writeable[argonaut.Json] = {
  Writeable(jsval => codec.encode(jsval.toString))
}

, . , , , , , , Ok(myArgonautObject) , , .

, ExtraJsonHelpers , .

+3

All Articles