使用 Spray 编组“地图”

Marshalling of `Map`s with Spray

我一直在尝试整理一堆地图,但出现错误。以下是定义:

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: spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Map[String,Integer]], implicit evidence: ClassManifest[org.allenai.example.webapp.SchemaMap])spray.json.RootJsonFormat[org.allenai.example.webapp.SchemaMap].
Unspecified value parameters evidence, evidence.
  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: spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Map[String,org.allenai.example.webapp.SchemaMap]], implicit evidence: ClassManifest[org.allenai.example.webapp.Profile])spray.json.RootJsonFormat[org.allenai.example.webapp.Profile].
Unspecified value parameters evidence, evidence.
  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: spray.json.DefaultJsonProtocol.JF[scala.collection.immutable.Seq[org.allenai.example.webapp.Profile]], implicit evidence: ClassManifest[org.allenai.example.webapp.Profiles])spray.json.RootJsonFormat[org.allenai.example.webapp.Profiles].
Unspecified value parameters evidence, evidence.
  implicit val profilesMarshall = jsonFormat1(Profiles.apply)

                                         ^

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

一次一个错误。这里的问题是您的地图正在使用 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@53e166ad

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