Play 2.2 match if there exists an implicit Json co

2020-03-26 05:26发布

问题:

I have put all my Json converters in one file JsonUtil, and then have a convertToJson method which tries to convert any object passed to json.

Basically a structure like this:

implicit val format_A = format[A]
implicit val format_B = format[B]

def convertToJson(o: Any): JsValue =
  o match {
    case a: A => Json.toJson(a)
    case b: B => Json.toJson(b)
    case a: Any => Logger.warn("could not convert to json: "+a); JsNull
  }

but with alot more formatters / cases. I don't want to import all these implicits when I need conversions (for various reasons). Is there a way to match if there exists a valid toJson conversion, so I wouldn't have to write all the cases?

like:

case o: ExistJsonConversion => Json.toJson(o)

回答1:

This works.

  def convertToJson[A](a: A)(implicit ev: Format[A] = null): JsValue = {
    if (ev != null) {
      Json.toJson(a)(ev)
    } else {
      println("oops")
      JsNull
    }
  }

A bit better version below (perhaps ;)

  case class Perhaps[E](value: Option[E]) {
    def fold[F](ifAbsent: => F)(ifPresent: E => F): F = 
      value.fold(ifAbsent)(ifPresent)
  }

  implicit def perhaps[E](implicit ev: E = null) = Perhaps(Option(ev))

  def convertToJson[A](a: A)(implicit p: Perhaps[Format[A]]): JsValue = {
    p.fold[JsValue] {
      println("oops")
      JsNull
    } { implicit ev =>
      Json.toJson(a)
    }
  }