Search code examples
scalaexceptionserializationmappinglift

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


I am pretty new to Scala/Lift and ran into following problem:

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)

}
}

I get following error message:

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

Sample 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"}

I tried several things, but it seams I stuck here. Can any one point me in the right direction? BTW. I am using scala 2.7.7!

Cheers, enzo


Solution

  • If you remove the jsonClass hint from your JSON input, it should parse. Something like:

    {"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"}
    

    Otherwise, you can try adding [] around your input like:

    [{"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"}]
    

    I think it is looking for a collection since the jsonClass attribute is used to help determine which subtype to deserialize the current item to. There is probably a way of getting it to work with a single element, but I am not sure what that is.

    The reason I asked about the scala version is, 2.7.7 is pretty old at this point (2.10 is the latest) and a newer version may make finding things a bit easier.