tags:

views:

532

answers:

1

Hi all, I have a exception from lift-json when I want to deserialize a json string. (using v2M1).

Basically I have the following class:

@BeanInfo
case class Game(val id:Int,
val bad:Map[String,Plan],
val good:Map[String,Plan])

and I am using

net.liftweb.json.Serialization.read[Game](jsonInString) 

to deserialize the jsonInString into a Game case class. Unfortunately I am getting the following error:

net.liftweb.json.MappingException: Can't find primary constructor for class interface scala.collection.immutable.Map
 at net.liftweb.json.Meta$.fail(Meta.scala:93)
 at net.liftweb.json.Meta$Reflection$$anonfun$primaryConstructorOf$1.apply(Meta.scala:129)
 at net.liftweb.json.Meta$Reflection$$anonfun$primaryConstructorOf$1.apply(Meta.scala:129)
 at scala.Option.getOrElse(Option.scala:61)
 at net.liftweb.json.Meta$Reflection$.primaryConstructorOf(Meta.scala:129)
 at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:106)
 at net.liftweb.json.Extraction$.build$1(Extraction.scala:119)
 at net.liftweb.json.Extraction$$anonfun$2$$anonfun$apply$1.apply(Extraction.scala:119)
 at net.liftweb.json.Extraction$$anonfun$2$$anonfun$apply$1.apply(Extraction.scala:119)
 at scala.List.flatMap(List.scala:1132)
 at net.liftweb.json.Extraction$$anonfun$2.apply(Extraction.scala:119)
 at net.liftweb.json.Extraction$$anonfun$2.apply(Extraction.scala:119)
 at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:106)
 at net.liftweb.json.Extraction$.build$1(Extraction.scala:119)
 at net.liftweb.json.Extraction$.extract0(Extraction.scala:154)
 at net.liftweb.json.Extraction$.extract(Extraction.scala:37)
 at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:247)
 at net.liftweb.json.Serialization$.read(Serialization.scala:50)

I appreciate your comments, Thanks, -A

PS - I tried v2M2 (snapshot @ 10 Feb, 2010 from Scala-tools.org), the exception gone but the serialization is not correct ! See the comments below.

+2  A: 

Support to serialize scala.Map was just added a few days ago. It will be in Lift-2.0-M2 which is released in a day or two. If you need that feature now you can try nightly snapshot.

Note, @BeanInfo and explicit vals are not needed in case classes. You can just define:

case class Game(id: Int, bad: Map[String, Plan], good:Map[String,Plan])
Joni
Actually this doesn't work with the following use-case (now using snapshot release of Lift-JSON from scala-tools): case class Plan( plan:Option[Action] )case class Game( game:Map[String,Plan])case class Action(id:Int, subAction : Option[Action])val game = new Game(Map("a"->new Plan(new Some(new Action(1,None)))))implicit val formats = net.liftweb.json.DefaultFormats game must beEqualTo(Serialization.read[Game](Serialization.write(game)))
Ali
I added a ticket for this bug. The following link shows a work-around:http://www.assembla.com/spaces/liftweb/tickets/341-Type-hints-are-needed-in-JSON-serializization-for-non-polymorphic-Map-
Joni