开发者

serializing and deserializing a json object through Lift-JSON

开发者 https://www.devze.com 2022-12-19 08:15 出处:网络
I have a exception from lift-json when I want to deserialize a json string. (using v2M1). Basically I have the following class:

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 belo开发者_开发知识库w.


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])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号