CustardBun
CustardBun

Reputation: 3867

Play JSON - How to generify this in Scala for Json handling?

I currently have this in scala, and it does what I want:

  private def prepareResponse(response: Array[SomeItem]): String = {
    implicit val writes = Json.writes[SomeItem]

    Json.stringify(JsObject(Map("message" -> Json.toJson(response))))
  }

however, I want to generify this so that I could put it anything as the response and, as long as there are Json.writes defined for the type I'm trying to convert to Json, it would stringify it.

For example:

  private def prepareResponse(response: Any): String = {
    implicit val writes = Json.writes[SomeItem]
    implicit val writes2 = Json.writes[SomeOtherItem]
    ...

    Json.stringify(JsObject(Map("message" -> Json.toJson(response))))
  }

This doesn't work, of course, as it says that there is no implicit write defined for Any. Adding one for Any also doesn't work, as I get the error:

 No unapply or unapplySeq function found
[scalac-2.11]     implicit val writeAny = Json.writes[Any]
[scalac-2.11]     

What's an ideal way to do this the "right" way (if any)?

Thanks in advance!

Upvotes: 1

Views: 54

Answers (1)

Mario Galic
Mario Galic

Reputation: 48430

import play.api.libs.json._

case class SomeItem(a: String, b: String)

object SomeItem {
  implicit val codec = Json.format[SomeItem]
}

case class SomeOtherItem(a: String, b: String, c: String)

object SomeOtherItem {
  implicit val codec = Json.format[SomeOtherItem]
}

// ...

object PlayJson extends App {
  def prepareResponse[T](response: T)(implicit tjs: Writes[T]): String = {
    Json.stringify(JsObject(Map("message" -> Json.toJson(response))))
  }

  println(prepareResponse(SomeItem("aa", "bb")))
  println(prepareResponse(SomeOtherItem("aa", "bb", "cc")))
  // ...
}

Upvotes: 1

Related Questions