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"))))?