score:16

Accepted answer

I think this answer solves your question, Scala: “No manifest available for type T”. It easily solved by implicitly passing on the manifest for the type method. I add an example of the code and a simple function for error handling.

  val jsonStr: String = """{"airports":[{"name":"sfo","score":1},{"name":"phx","score":1},{"name":"sjc","score":1}]}"""

  case class AirPortScores(name: String, score: Double)
  case class JsonRulesHandler(airports: List[AirPortScores])

  val json: JsonRulesHandler = getJsonObj[JsonRulesHandler](jsonStr)
  println(json)


  def getJsonObj[T](jsonString:String)(implicit m: Manifest[T]): T = {
    extractFrom(jsonString) match {
      case Success(jsonParsed) ⇒
        jsonParsed
      case Failure(exc) ⇒
        throw new IllegalArgumentException(exc)
    }
  }

  private def extractFrom[T](jsonString:String)(implicit m: Manifest[T]): Try[T] = {
    implicit val formats: DefaultFormats.type = DefaultFormats

    Try {
      parse(jsonString).extract[T]
    }
  }

Related Query

More Query from same tag