编组`地图`s与浪花

时间:2015-07-01 07:35:48

标签: scala spray spray-json spray-client

我一直试图整理一堆地图但我收到错误。以下是定义:

import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._

import scala.collection.JavaConverters._


case class SchemaMap( schemaMap: scala.collection.immutable.Map[String, Integer] ) /**  FIRST ERROR IS HERE!! **/ 
case class Profile( counts: scala.collection.immutable.Map[String, SchemaMap] )
case class Profiles( profiles: scala.collection.immutable.Seq[Profile] )

object Profiles {
  implicit val schemaMapMarshall = jsonFormat1(SchemaMap.apply)
  implicit val profileMarshall = jsonFormat1(Profile.apply)
  implicit val profilesMarshall = jsonFormat1(Profiles.apply)

  def convertAProfileToScala(javaProfile: edu.illinois.cs.cogcomp.profilerNew.model.Profile): Profile = {
    val out = javaProfile.getAllSchema.asScala.map{item =>
      (item._1, SchemaMap(item._2.asScala.toMap))
    }.toMap
    Profile(out)
  }

  def convert[Profiles](sq: collection.mutable.Seq[Profiles]): collection.immutable.Seq[Profiles] =
    collection.immutable.Seq[Profiles](sq:_*)

  def convertProfilesToScala(javaProfiles: java.util.List[edu.illinois.cs.cogcomp.profilerNew.model.Profile]): Profiles = {
    val a: collection.mutable.Seq[Profile]  = javaProfiles.asScala.map{ convertAProfileToScala }
    val immutableSeq = collection.immutable.Seq[Profile](a:_*)
    Profiles(immutableSeq)
  }
}

这是错误:

Error:(16, 47) could not find implicit value for evidence parameter of type spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Map[String,Integer]]
  implicit val schemaMapMarshall = jsonFormat1(SchemaMap.apply)
                                              ^
Error:(16, 47) not enough arguments for method jsonFormat1: (implicit evidence$1: spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Map[String,Integer]], implicit evidence$2: ClassManifest[org.allenai.example.webapp.SchemaMap])spray.json.RootJsonFormat[org.allenai.example.webapp.SchemaMap].
Unspecified value parameters evidence$1, evidence$2.
  implicit val schemaMapMarshall = jsonFormat1(SchemaMap.apply)
                                              ^
Error:(17, 45) could not find implicit value for evidence parameter of type spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Map[String,org.allenai.example.webapp.SchemaMap]]
  implicit val profileMarshall = jsonFormat1(Profile.apply)
                                            ^
Error:(17, 45) not enough arguments for method jsonFormat1: (implicit evidence$1: spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Map[String,org.allenai.example.webapp.SchemaMap]], implicit evidence$2: ClassManifest[org.allenai.example.webapp.Profile])spray.json.RootJsonFormat[org.allenai.example.webapp.Profile].
Unspecified value parameters evidence$1, evidence$2.
  implicit val profileMarshall = jsonFormat1(Profile.apply)
                                            ^
Error:(18, 46) could not find implicit value for evidence parameter of type spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Seq[org.allenai.example.webapp.Profile]]
  implicit val profilesMarshall = jsonFormat1(Profiles.apply)
                                             ^
Error:(18, 46) not enough arguments for method jsonFormat1: (implicit evidence$1: spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Seq[org.allenai.example.webapp.Profile]], implicit evidence$2: ClassManifest[org.allenai.example.webapp.Profiles])spray.json.RootJsonFormat[org.allenai.example.webapp.Profiles].
Unspecified value parameters evidence$1, evidence$2.
  implicit val profilesMarshall = jsonFormat1(Profiles.apply)

                                         ^

注意:有一个类似的帖子here,但答案在这里没有帮助。正如您所看到的,我已经在其答案中建议了导入,并且拥有所有变量immutable

1 个答案:

答案 0 :(得分:1)

一次出错一次。这里的问题是你的地图正在使用Integer,Spray并不知道如何映射。如果您使用Int,那么它将起作用:

scala> case class SchemaMap( schemaMap: scala.collection.immutable.Map[String, Integer] )
defined class SchemaMap

scala> val schemaMapMarshall = jsonFormat1(SchemaMap.apply)
<console>:12: error: could not find implicit value for evidence parameter of type spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Map[String,Integer]]
scala> case class SchemaMap( schemaMap: scala.collection.immutable.Map[String, Int])
defined class SchemaMap

scala> val schemaMapMarshall = jsonFormat1(SchemaMap.apply)
schemaMapMarshall: spray.json.RootJsonFormat[SchemaMap] = spray.json.ProductFormatsInstances$$anon$1@53e166ad

或者,您可以定义自己的JsonFormat[Integer]