応答には常にtotal-count
とentities
のAPIがありますが、entities
はさまざまな種類のAPIです。私がしようとしているのは、より包括的なcase-classesに変換するための解析を&にすることです。一般的なjson-sprayパーサーの階層を定義するにはどうすればよいですか?
だから、次の種類
case class StandardReturn[A](
`total-count`: Double,
entities: List[A]
)
case class System(
id: String,
name: String
)
し、次の例にしよう:
当然のこと/ Iはケースクラスを設定する方法を私にError:(34, 42) value entities is not a member of type parameter A
entityString.parseJson.convertTo[A].entities // Where I'm running into trouble
^
を与えている
object SystemProtocol extends DefaultJsonProtocol {
implicit val systemFormat: RootJsonFormat[System] =
jsonFormat2(System)
implicit def entityFormat[A: JsonFormat] =
jsonFormat(StandardReturn.apply[A], "total-count", "entities")
}
import SystemProtocol._
val response = """{
"total-count": 10,
"entities": [
{ "id": "1", "name": "me" }
]
}"""
class Example {
def transform[A: JsonReader](entityString: String) =
entityString.parseJson
.convertTo[A]
.entities // Where I'm running into trouble
}
object Example {
val transformed = new Example().transform[StandardReturn[System]](response)
}
Example.transformed
タイプは、transform
が確実にentities
が保証されますはA
(A
はStandardReturn[A]
)の型に変換した後に存在しますか?私はスカラ型システムにあまり慣れていないので、助けてくれてありがとう。