使用 play-json 库对自定义 Scala 对象进行 Scala 反序列化
Scala deserialization of custom scala object with play-json library
我的 ScalaTest 在尝试将 JSON 字符串解析为自定义 Scala 对象时中断。我正在使用 Play-Json 库进行 [反] 序列化。序列化工作正常,但反序列化中断,而 运行 在 Blah class 上进行单元测试。该测试调用 fromJsonString() 方法,我使用 ScalaTest 库进行单元测试。在这里感谢一些帮助。
异常跟踪:(完整跟踪 - http://pasted.co/e627b1ee)
An exception or error caused a run to abort: scala.collection.immutable.$colon$colon.hd()Ljava/lang/Object;
java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd()Ljava/lang/Object;
at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:144)
at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:108)
at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:103)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3536)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1872)
at play.api.libs.json.jackson.JacksonJson$.parseJsValue(JacksonJson.scala:226)
at play.api.libs.json.Json$.parse(Json.scala:21)
at com.project.gateway.model.Blah.fromJsonString(Blah.scala:98)
Scala 对象定义:
case class Blah(name: String, id: String) {
implicit val BlahWrites: Writes[Blah] = (
(JsPath \ "name").write[String] and
(JsPath \ "id").write[String]
)(unlift(Blah.unapply))
implicit val BlahReads: Reads[Blah] = (
(JsPath \ "name").read[String] and
(JsPath \ "id").read[String]
)(Blah)
def toJsonString(): String = {
Json.toJson(this).toString()
}
def fromJsonString(jsonString: String): Blah = {
val value = Json.parse(jsonString)
value.as[Blah]
//Json.fromJson[Blah](value).get
}
}
我的 SBT 文件:
name := "Project"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies += "com.typesafe.scala-logging" % "scala-logging_2.11" % "3.1.0"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
libraryDependencies += "com.typesafe.play" % "play-json_2.10" % "2.4.2"
我想 runtime/test 的 scala-library 版本与用于构建引发错误的依赖项的版本之间存在差异。
如果使用SBT或Maven,你可以检查使用的库(包括传递的),所以检查scala-library是否不兼容(通过不同的依赖项拉入不同的版本)。
我的 ScalaTest 在尝试将 JSON 字符串解析为自定义 Scala 对象时中断。我正在使用 Play-Json 库进行 [反] 序列化。序列化工作正常,但反序列化中断,而 运行 在 Blah class 上进行单元测试。该测试调用 fromJsonString() 方法,我使用 ScalaTest 库进行单元测试。在这里感谢一些帮助。
异常跟踪:(完整跟踪 - http://pasted.co/e627b1ee)
An exception or error caused a run to abort: scala.collection.immutable.$colon$colon.hd()Ljava/lang/Object;
java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd()Ljava/lang/Object;
at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:144)
at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:108)
at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:103)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3536)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1872)
at play.api.libs.json.jackson.JacksonJson$.parseJsValue(JacksonJson.scala:226)
at play.api.libs.json.Json$.parse(Json.scala:21)
at com.project.gateway.model.Blah.fromJsonString(Blah.scala:98)
Scala 对象定义:
case class Blah(name: String, id: String) {
implicit val BlahWrites: Writes[Blah] = (
(JsPath \ "name").write[String] and
(JsPath \ "id").write[String]
)(unlift(Blah.unapply))
implicit val BlahReads: Reads[Blah] = (
(JsPath \ "name").read[String] and
(JsPath \ "id").read[String]
)(Blah)
def toJsonString(): String = {
Json.toJson(this).toString()
}
def fromJsonString(jsonString: String): Blah = {
val value = Json.parse(jsonString)
value.as[Blah]
//Json.fromJson[Blah](value).get
}
}
我的 SBT 文件:
name := "Project"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies += "com.typesafe.scala-logging" % "scala-logging_2.11" % "3.1.0"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
libraryDependencies += "com.typesafe.play" % "play-json_2.10" % "2.4.2"
我想 runtime/test 的 scala-library 版本与用于构建引发错误的依赖项的版本之间存在差异。
如果使用SBT或Maven,你可以检查使用的库(包括传递的),所以检查scala-library是否不兼容(通过不同的依赖项拉入不同的版本)。