gpt4 book ai didi

json - Lift Framework无法反序列化JSON数据

转载 作者:行者123 更新时间:2023-12-04 03:34:44 30 4
gpt4 key购买 nike

我正在尝试使用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

最佳答案

序列化不支持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)
}

关于json - Lift Framework无法反序列化JSON数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4115295/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com