我正在使用lift-json将JSON字符串反序列化为Scala案例类。我正在解析的JSON有一个共同的结构:数据,成功和错误字段,其中数据包含有趣的位。我已经创建了一个APIResponse类来解释这个结构,还有一个简单的解析方法,它接受'data'中包含的对象类型的类型参数:
import net.liftweb.json.Serialization.read
import net.liftweb.json.DefaultFormats
object JSONParseTest extends App {
implicit val formats = DefaultFormats
def parse[T: Manifest](json: String) = {
read[APIResponse[T]](json)
}
val resultA = parse[TypeA](""" { "data": { "foo": "string" }, "success": true } """)
println(resultA)
val resultB = parse[TypeB](""" { "data": { "bar": "string" }, "success": true } """)
println(resultB)
}
case class TypeA(foo: String)
case class TypeB(bar: String)
case class APIResponse[D](data: D, success: Boolean, error: Option[String]) {
override def toString: String =
if(success) {
"SUCCESSFUL: " + data.toString
} else {
"ERROR: " + error.get
}
}
一切都很好......对于解析的第一个对象。但是,出于某种原因,解析的第二个对象似乎“卡住”,表现就像是传递了第一个类型参数而不是第二个。正如您在下面的输出中看到的那样,lift-json正在寻找JSON字符串中的'foo'字段,但无法找到它; 'foo'存在于TypeA上,但不存在于TypeB上。我已经在解析方法中检查了清单,并且它在那里得到了正确的类型。如果我注释掉第一个解析/打印,第二个开始工作。关于为什么这不符合预期的任何指针?这让我很难过。
SUCCESSFUL: TypeA(string)
Exception in thread "main" net.liftweb.json.MappingException: No usable value for data
No usable value for foo
Did not find value which can be converted into java.lang.String
at net.liftweb.json.Meta$.fail(Meta.scala:191)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:357)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:317)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
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.Extraction$.instantiate$1(Extraction.scala:253)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:286)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:315)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:366)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:199)
at net.liftweb.json.Extraction$.extract(Extraction.scala:43)
at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:300)
at net.liftweb.json.Serialization$.read(Serialization.scala:58)
at jmullin.api.Test$.parse(API.scala:11)
at jmullin.api.Test$delayedInit$body.apply(API.scala:16)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:60)
at scala.App$$anonfun$main$1.apply(App.scala:60)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:76)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30)
at scala.App$class.main(App.scala:60)
at jmullin.api.Test$.main(API.scala:7)
at jmullin.api.Test.main(API.scala)
Caused by: net.liftweb.json.MappingException: No usable value for foo
Did not find value which can be converted into java.lang.String
at net.liftweb.json.Meta$.fail(Meta.scala:191)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:357)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:317)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
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.Extraction$.instantiate$1(Extraction.scala:253)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:286)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:315)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:351)
... 29 more
Caused by: net.liftweb.json.MappingException: Did not find value which can be converted into java.lang.String
at net.liftweb.json.Meta$.fail(Meta.scala:191)
at net.liftweb.json.Extraction$.convert(Extraction.scala:403)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:314)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:351)
... 42 more
答案 0 :(得分:2)
事实证明,lift-json正在对幕后的构造函数映射做一些类的memoization,而忽略了类型args。当使用不同的类型参数对同一个类执行查找时,将返回旧的构造函数,从而导致此处出现混淆。神秘解决了(虽然问题不是)。