简体   繁体   中英

No instance of play.api.libs.json.Format is available for scala.Iterable[java.lang.String]

Trying to map a simple class using play version 2.6.2 and scala 2.11.11

import play.api.libs.json._
import play.api.libs.json.util._
import play.api.libs.json.Reads._
import play.api.libs.json.Writes._
import play.api.libs.json.Format._
import play.api.libs.functional.syntax._

case class ObjectInfo (
    names : Iterable[String],
    info : Iterable[String]
)

object ObjectInfo {

  /**
    * Mapping to and from JSON.
    */
  implicit val documentFormatter = Json.format[ObjectInfo]

}

getting:

No instance of play.api.libs.json.Format is available for scala.Iterable[java.lang.String], scala.Iterable[java.lang.String] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)

I was expecting Play to automatically map these fields since they're not complex object types but simple Collection of strings.

You provide "too much" implicit stuff with your imports. If you remove all imports but the first one, it will compile and do what you want.

If you enable implicit parameter logging via the scalac option -Xlog-implicits , you will see various "ambigouity" and "diverging implicit expansion" errors. The conflicting imports are import play.api.libs.json.Reads._ / import play.api.libs.json.Writes._ and import play.api.libs.json.Format._ . Maybe someone else can explain this conflict in more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM