我正在尝试使用Lift框架反序列化JSON文本,并且它们似乎不支持Seq特征(尽管支持List)。举个例子......
一些代表员工的JSON数据(名字和姓氏)......
{"employees":[{"fname":"Bob","lname":"Hope"},{"fname":"Bob","lname":"Smith"}]}
这是员工域对象:
case class Employee(fname: String, lname: String) { }
case class Employees(employees: Seq[Employee]) { }
这是我的JSON反序列化代码......
class EmployeeTest {
@Test def test() {
val jsonText: String = ....
val e = deserialize(jsonText)
}
def deserialize(in: String): Employees = {
implicit val formats = net.liftweb.json.DefaultFormats
net.liftweb.json.Serialization.read[Employees](in)
}
}
如果我将Employees域对象更改为使用List而不是Seq,那么它可以工作。但如果可以的话,我真的很想使用Seq。
这是我在运行上面的代码时看到的异常(使用Seq):我能做些什么来让它工作吗?谢谢你的帮助!
net.liftweb.json.MappingException: unknown error
at net.liftweb.json.Extraction$.extract(Extraction.scala:43)
at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:288)
at net.liftweb.json.Serialization$.read(Serialization.scala:50)
at EmployeeTest.deserialize(EmployeeTest.scala:20)
at EmployeeTest.test(EmployeeTest.scala:13)
Caused by: java.lang.UnsupportedOperationException: tail of empty list
at scala.collection.immutable.Nil$.tail(List.scala:388)
at scala.collection.immutable.Nil$.tail(List.scala:383)
at net.liftweb.json.Meta$Constructor.bestMatching(Meta.scala:60)
at net.liftweb.json.Extraction$.findBestConstructor$1(Extraction.scala:187)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:192)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:222)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:240)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:269)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:242)
at net.liftweb.json.Extraction$$anonfun$4.apply(Extraction.scala:194)
at net.liftweb.json.Extraction$$anonfun$4.apply(Extraction.scala:194)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:206)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:206)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:61)
at scala.collection.immutable.List.foreach(List.scala:45)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:206)
at scala.collection.immutable.List.map(List.scala:45)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:194)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:222)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:240)
at net.liftweb.json.Extraction$.extract(Extraction.scala:284)
at net.liftweb.json.Extraction$.extract0(Extraction.scala:172)
at net.liftweb.json.Extraction$.extract(Extraction.scala:40)
... 33 more
答案 0 :(得分:13)
序列化不支持Seq,因为它不是具体类型。在反序列化期间,没有可用于决定具体实现的类型信息。我们可以使用例如List作为默认实现,但是以下属性将不再适用于所有类型:
deserialize(serialize(x)) == x
这个具体案例可以按如下方式反序列化:
import net.liftweb.json._
import net.liftweb.json.JsonAST._
case class Employee(fname: String, lname: String)
case class Employees(employees: Seq[Employee])
object Test extends Application {
implicit val formats = DefaultFormats
val s = """ {"employees":[{"fname":"Bob","lname":"Hope"},{"fname":"Bob","lname":"Smith"}]} """
val json = JsonParser.parse(s)
val employees = Employees(for {
JArray(emps) <- json \ "employees"
emp <- emps
} yield emp.extract[Employee])
println(employees)
}