我试图在 scala 中使用 spray-json 来识别在转换为 Json 并返回时 Ec2Provider 和 OpenstackProvider 之间的选择。我希望能够在“提供者”中提供选择,如果这些选择不适合可用的选择,那么它不应该验证。
我的尝试可以在以下代码中看到:
import spray.json._
import DefaultJsonProtocol._
case class Credentials(username: String, password: String)
abstract class Provider
case class Ec2Provider(endpoint: String,credentials: Credentials) extends Provider
case class OpenstackProvider(credentials: Credentials) extends Provider
case class Infrastructure(name: String, provider: Provider, availableInstanceTypes: List[String])
case class InfrastructuresList(infrastructures: List[Infrastructure])
object Infrastructures extends App with DefaultJsonProtocol {
implicit val credFormat = jsonFormat2(Credentials)
implicit val ec2Provider = jsonFormat2(Ec2Provider)
implicit val novaProvider = jsonFormat1(OpenstackProvider)
implicit val infraFormat = jsonFormat3(Infrastructure)
implicit val infrasFormat = jsonFormat1(InfrastructuresList)
println(
InfrastructuresList(
List(
Infrastructure("test", Ec2Provider("nova", Credentials("user","pass")), List("1", "2"))
)
).toJson
)
}
不幸的是,它失败了,因为它找不到Provider
抽象类的格式化程序。
test.scala:19: could not find implicit value for evidence parameter of type Infrastructures.JF[Provider]
有人对此有任何解决方案吗?