lift-json无法反序列化可选字段

时间:2014-01-29 22:27:09

标签: lift lift-json

使用lift-json尝试使用可选字段序列化/反序列化case类时出现异常。

scala> import net.liftweb.json._
import net.liftweb.json._

scala> import net.liftweb.json.Serialization.{read, write}
import net.liftweb.json.Serialization.{read, write}

scala> implicit val formats = DefaultFormats
formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@707a7686

scala> case class Person(Name:String,Age:Option[Int])
defined class Person

scala> val friends=List(Person("Dan",Some(21)),Person("Ben",None))
friends: List[Person] = List(Person(Dan,Some(21)), Person(Ben,None))

scala> read[List[Person]](write(friends))
java.lang.InternalError: Malformed class name
    at java.lang.Class.getSimpleName(Class.java:1169)
    at net.liftweb.json.ScalaSigReader$$anonfun$findClass$3.apply(ScalaSig.scala:45)
    at net.liftweb.json.ScalaSigReader$$anonfun$findClass$3.apply(ScalaSig.scala:45)
    at scala.collection.LinearSeqOptimized$class.find(LinearSeqOptimized.scala:100)
    at scala.collection.immutable.List.find(List.scala:76)
    at net.liftweb.json.ScalaSigReader$.findClass(ScalaSig.scala:45)
    at net.liftweb.json.ScalaSigReader$.findClass(ScalaSig.scala:41)
    at net.liftweb.json.ScalaSigReader$.readConstructor(ScalaSig.scala:24)
    at net.liftweb.json.Meta$Reflection$.term$1(Meta.scala:275)
    at net.liftweb.json.Meta$Reflection$.typeParameters(Meta.scala:292)
    at net.liftweb.json.Meta$.mkContainer$1(Meta.scala:107)
    at net.liftweb.json.Meta$.fieldMapping$1(Meta.scala:134)
    at net.liftweb.json.Meta$.toArg$1(Meta.scala:154)
    at net.liftweb.json.Meta$$anonfun$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:98)
    at net.liftweb.json.Meta$$anonfun$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:97)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
    at scala.collection.immutable.List.foreach(List.scala:76)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:233)
    at scala.collection.immutable.List.map(List.scala:76)
    at net.liftweb.json.Meta$$anonfun$constructors$1$1.apply(Meta.scala:97)
    at net.liftweb.json.Meta$$anonfun$constructors$1$1.apply(Meta.scala:96)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
    at scala.collection.immutable.List.foreach(List.scala:76)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:233)
    at scala.collection.immutable.List.map(List.scala:76)
    at net.liftweb.json.Meta$.constructors$1(Meta.scala:96)
    at net.liftweb.json.Meta$$anonfun$mappingOf$1.apply(Meta.scala:168)
    at net.liftweb.json.Meta$$anonfun$mappingOf$1.apply(Meta.scala:160)
    at net.liftweb.json.Meta$Memo.memoize(Meta.scala:197)
    at net.liftweb.json.Meta$.mappingOf(Meta.scala:160)
    at net.liftweb.json.Extraction$.mkMapping$1(Extraction.scala:193)
    at net.liftweb.json.Extraction$.mkMapping$1(Extraction.scala:190)
    at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:198)
    at net.liftweb.json.Extraction$.extract(Extraction.scala:42)
    at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:300)
    at net.liftweb.json.Serialization$.read(Serialization.scala:58)
    at .<init>(<console>:16)
    at .<clinit>(<console>)
    at .<init>(<console>:11)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:914)
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:546)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:577)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543)
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:694)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:745)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:651)
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:542)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:550)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:851)
    at xsbt.ConsoleInterface.run(ConsoleInterface.scala:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:73)
    at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:64)
    at sbt.Console.console0$1(Console.scala:23)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
    at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
    at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)

注意:

  1. 如果我使用像“case class Person(Name:String,Age:Option [String])”而不是Int的字符串,它将返回正确的结果。
  2. 如果我像“case class Person(Name:String,Age:Option [java.lang.Integer])”那样使用“java.lang.Integer”,它也会返回正确的结果。
  3. 我的问题是为什么我必须在这里使用java类型?是否有更好/更清晰的表达方式?

1 个答案:

答案 0 :(得分:1)

请参阅this README末尾的常见问题解答。对于在REPL中定义的类,提取无法正常工作。