简体   繁体   中英

Scala deserialization of custom scala object with play-json library

My ScalaTest breaks while trying to parse a JSON string into a custom Scala object. I'm using Play-Json library for [de]serialization. Serialization works fine but the deserialization breaks while running a unit test on the Blah class. The test invokes the fromJsonString() method and Im using ScalaTest library for unit testing. Appreciate some help here.

Exception trace: (Full trace - http://pasted.co/e627b1ee )

An exception or error caused a run to abort: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object; 
java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()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 Object definition:

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
      }
    }

My SBT file:

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"

I guess there is difference between the version of scala-library at runtime/test, and the one used to build the dependency which is raising the error.

If using SBT or Maven, you can check the used libraries (including the transitive one), so check there is no incompatibility regarding the scala-library (pull in different versions by different dependencies).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM