Play 2.2 match if there exists an implicit Json converter

微笑、不失礼 提交于 2019-12-01 07:41:02

问题


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


来源:https://stackoverflow.com/questions/20015658/play-2-2-match-if-there-exists-an-implicit-json-converter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!