0

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

2
  • can you post a sample of the JSON you are trying to parse? Also, is there a reason you have to use scala 2.7.7?
    – jcern
    Mar 25, 2013 at 12:29
  • json sample: {"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"} regarding the version: No, is there a problem with 2.7.7?
    – enzo
    Mar 25, 2013 at 12:32

1 Answer 1

0

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.

4
  • I updated now to 2.9 and removed the hint implicit val formats = DefaultFormats. I declared the JTest outside of the Tests-class. Nothing changed I get the same error message.
    – enzo
    Mar 25, 2013 at 16:35
  • Sorry, I was testing a few things for you and confused two issues. I clarified both items that I am able to get working, hopefully one of those will help you move forward. Both shoud work with your original code.
    – jcern
    Mar 25, 2013 at 17:03
  • Thanks for you're help! The problem is that I fetch the json from a server. So is it possible to parse the json remove the class hint and then deserialize it? Just tied something like json.filter { case JField("jsonClass", JString("JTest")) => false case _ => true } and json remove { _ == JField("jsonClass", JString("JTest")) }. Nothing worked for me correctly :S
    – enzo
    Mar 26, 2013 at 12:19
  • Thanks a lot I make a mistake. Removing after parsing worked for me :)
    – enzo
    Mar 26, 2013 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.