Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing case classes with Map[String,Any] properties with lift-json

I've been struggling with something that should be simple through lift-json for days: serializing a map to JSON.

I know, I know – "Root object can't yet be List or Map" – but I'm willing to wrap in a case class for now, and I still haven't been able to get this to work. Thanks to some stack overflow help, I've got serialization working, but I can't deserialize it from a string. I get errors like "No usable value for _" and "No information known about type."

There are other, older posts on the web that indicate type hints are the answer, but that just leads to a different error like "Do not know how to deserialize __."

For Scala 2.8.0 and Lift 2.2:

import net.liftweb.json._
import net.liftweb.json.Serialization.{read, write}

case class MapWrap(data: Map[String, Any])

object Scaffold {
    def main(args: Array[String]) {

        implicit val formats = Serialization.formats(NoTypeHints)
        //implicit val formats = Serialization.formats(ShortTypeHints(List(classOf[MapWrap])))
        //implicit val formats = Serialization.formats(FullTypeHints(List(classOf[MapWrap])))

        val ser = write(new MapWrap(Map[String,Any]("key" -> "value")))
        println("JSON: " + ser)
        println(read[MapWrap](ser))

    }
}

The line println(read[MapWrap](ser)) results in the complaint "net.liftweb.json.MappingException: No usable value for data."

How can I deserialize this case class wrapper (or achieve my ultimate goal: read(write(Map("key" -> "value"))))?

like image 533
Jeremy Avatar asked Feb 19 '11 14:02

Jeremy


1 Answers

This example works if you change your Map to Map[String, String]. Then the serializer knows you expect String values. Type hints are needed if your Map values are polymorphic. Like Map[String, Animal]; Dog extends Animal; Cat extends Animal etc. Now a type hint is required for Animal type. It adds "jsonClass" field to JSON which is used to decide the concrete target type.

If you can upgrade to 2.3-M1 then you no longer need to wrap the Map but can serialize the Map directly:

http://www.scala-tools.org/repo-releases/net/liftweb/lift-json_2.8.1/2.3-M1/

like image 68
Joni Avatar answered Oct 11 '22 17:10

Joni