3

我正在使用 lift-json 将 JSON 字符串反序列化为 Scala 案例类。我正在解析的 JSON 有一个共同的结构:数据、成功和错误字段,其中数据包含有趣的位。我创建了一个 APIResponse 类来解释这个结构,以及一个简单的解析方法,它接受一个类型参数来表示“数据”中包含的对象的类型:

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 方法中的清单,它在那里有正确的类型。如果我注释掉第一个解析/打印,第二个开始工作。关于为什么这不符合预期的任何指示?这让我有点难以置信。

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
4

1 回答 1

3

事实证明,lift-json 在后台对类到构造函数映射进行了一些记忆,而忽略了类型参数。当对具有不同类型参数的同一个类执行查找时,会返回旧的构造函数,从而导致此处出现混淆。谜团解决了(虽然问题不是)。

于 2013-03-08T19:17:13.987 回答