1

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"
3
  • 1
    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. Commented Sep 16, 2015 at 7:10
  • Wow, you are bang on. I was using Play-Json 2.10 and upgrading it to Play-Json 2.11 did the trick . Commented Sep 16, 2015 at 7:21
  • cchantep - You mind promoting it as an answer ill mark it as well. THanks Commented Sep 16, 2015 at 7:34

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.