消息:net.liftweb.json.MappingException:不知道如何反序列化

时间:2013-03-25 11:45:14

标签: scala exception serialization mapping lift

我是Scala / Lift的新手,遇到了以下问题:

class Tests {
case class JTest(
      thisUrl:String,
      id:Int,
      desc:String,
      results:String,
      isEnabled:Boolean,
      attackerAppliance:String,
      victimAppliance:String,
      lastModified:String)

def displayTest(in: NodeSeq): NodeSeq = {
implicit val formats = DefaultFormats.withHints(
           ShortTypeHints(List(classOf[JTest])))

val content = fromInputStream( url.openStream ).getLines.mkString("\n")

        val json = parse(content)
        val test = json.extract[JTest]
        Helpers.bind("test", in,
          AttrBindParam("thisUrl", test.thisUrl, "href"),
          "id" -> test.id,
          "desc" -> test.desc,
          "results" -> test.results,
          "isEnabled" -> test.isEnabled,
          "attackerAppliance" -> test.attackerAppliance,
          "victimAppliance" -> test.victimAppliance,
          "lastModified" -> test.lastModified)

}
}

我收到以下错误消息:

Message: net.liftweb.json.MappingException: Do not know how to deserialize 'JTest'

示例JSON:

{"jsonClass":"JTest","thisUrl":"/api/test/1","id":1,"desc":"Vulnerability in Server Service","results":"/api/test/1/results","isEnabled":true,"attackerAppliance":"/api/appliance/2","victimAppliance":"/api/appliance/6","lastModified":"2012-08-08 11:46:29.004"}

我尝试了几件事,但是它卡在了这里。任何人都可以指出我正确的方向吗? BTW。我正在使用scala 2.7.7!

干杯,恩佐

1 个答案:

答案 0 :(得分:0)

如果从JSON输入中删除jsonClass提示,则应解析。类似的东西:

{"thisUrl":"/api/test/1",
"id":1,
"desc":"Vulnerability in Server Service",
"results":"/api/test/1/results",
"isEnabled":true,
"attackerAppliance":"/‌​api/appliance/2",
"victimAppliance":"/api/appliance/6",
"lastModified":"2012-08-08 11:46:29.004"}

否则,您可以尝试在输入周围添加[],例如:

[{"jsonClass":"JTest",
"thisUrl":"/api/test/1",
"id":1,
"desc":"Vulnerability in Server Service",
"results":"/api/test/1/results",
"isEnabled":true,
"attackerAppliance":"/‌​api/appliance/2",
"victimAppliance":"/api/appliance/6",
"lastModified":"2012-08-08 11:46:29.004"}]

我认为它正在寻找一个集合,因为jsonClass属性用于帮助确定将当前项反序列化的子类型。可能有一种方法可以使用单个元素,但我不确定它是什么。

我询问scala版本的原因是,2.7.7在这一点上相当陈旧(2.10是最新的),而较新的版本可能会让事情变得更容易。