Scala 类型参数似乎越来越"stuck"



我使用lift-json将JSON字符串反序列化到Scala case类中。我正在解析的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上不存在。我检查了parse方法中的manifest,它有正确的类型。如果我注释掉第一个解析/打印,第二个就开始工作。有什么提示为什么这不是预期的行为吗?这让我有点困惑。

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

结果是lift-json在幕后做了一些类到构造函数映射的记忆,忽略了类型参数。当对具有不同类型参数的同一个类执行查找时,将返回旧的构造函数,从而导致此处所见的混乱。谜底揭开了(虽然问题还没有)

相关内容

  • 没有找到相关文章

最新更新