title: Scala and Play2: ClassCastException: java.lang.Object cannot be cast to play.api.libs.json.JsValue
body: <p>This is the second time I've bumped into this.
  Edit: I discovered it's also the second time I've asked on stack and is the THIRD or FOURTH time I've run into this. Other question is here: <a href="https://stackoverflow.com/questions/14541682/cast-class-exception-in-scala-java-lang-object-to-scala-caseclass">Cast Class Exception in Scala: java.lang.Object to Scala CaseClass</a></p>
  
  <p>Edited heavily... I can simplify the question after much analysis. Appears this is a problem with how I'm using traits and implementing/overriding the toJson method... I'm sure this is something easy but I haven't been able to find what I'm doing wrong.</p>
  
  <p>I get this class cast exception and I can't see any problem with this.</p>
  
  <p>I have this: </p>
  
  <pre><code>package service
  
  import play.api.libs.json.{JsObject, Json, JsValue}
  
  /**
   * author: jason goodwin
   */
  trait Jsonable{
  
    def toJson: JsValue = {null}
  }
  
  object Jsonable {
    def renderJsonableListToJson(entities: Iterable[Jsonable]): String = {
  
      val jsonList = Json.toJson(entities.map(x =&gt; x.toJson).toSeq)
      val result = JsObject("meta" -&gt; JsObject(Seq()) :: "data" -&gt; jsonList :: Nil)
  
      Json.stringify(result)
    }
  }
  </code></pre>
  
  <p>and this which is interfacing with a java library:</p>
  
  <pre><code>trait GraphedArticle extends BaseFramedEntity with Jsonable {
  
    @Property("articleId")
    def setArticleId (userId: String)
  
    @Property("articleId")
    def getArticleId:String
  
    @Property("title")
    def setTitle (title: String)
  
    @Property("title")
    def getTitle:String
  
    @Property("preview")
    def setPreview (preview: String)
  
    @Property("preview")
    def getPreview: String
  
    @Property("imageUrl")
    def setImageUrl (imageUrl: String)
  
    @Property("imageUrl")
    def getImageUrl: String
  
    @Property("articleUrl")
    def setArticleUrl (imageUrl: String)
  
    @Property("articleUrl")
    def getArticleUrl: String
  
    override def toJson: JsValue = {
      val ret = JsObject(
        "articleId" -&gt; Json.toJson(getArticleId)
          :: "graphId" -&gt; Json.toJson(asVertex().getId.toString)
          :: "created_time" -&gt; Json.toJson(123123123)
          :: "url" -&gt; Json.toJson(getArticleUrl)
          :: "title" -&gt; Json.toJson(getTitle)
          :: "preview" -&gt; Json.toJson("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut euismod pellentesque facilisis. Morbi odio sem, dapibus eget cursus vel, sodales in diam. Nunc porttitor mauris vitae diam imperdiet nec bibendum magna eleifend. Mauris sapien lectus, sodales et tincidunt at, aliquam a leo. Nullam quis enim vitae ligula condimentum tempus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam ut facilisis quam. Aenean sed dapibus sapien. Nulla facilisi.")
          :: "image" -&gt; Json.toJson(getImageUrl)
          :: "topics" -&gt; JsArray(Seq(Json.toJson("Tiger Dog"), Json.toJson("Lemon Party")))
          :: "from_user" -&gt; JsArray(
          JsObject(
            "username" -&gt; Json.toJson("fudgeface")
              :: "full_name" -&gt; Json.toJson("Mr. Fudge Face")
              :: "id" -&gt; Json.toJson("#3:27")
              :: "profile_picture" -&gt; Json.toJson("url here")
              :: Nil
          ) :: Nil)
          :: "shares" -&gt; JsArray(
          JsObject(
            "count" -&gt; Json.toJson("100")
              :: Nil
          ) :: Nil)
          :: Nil).asInstanceOf
  
      println(ret.toString, "here")
      ret
  
    }
  }
  </code></pre>
  
  <p>If I try to access toJson it dies. The other methods work.</p>
  
  <pre><code>println("\n\n"+getArticleList(defaultPage, defaultPageSize).head.toJson)
  
  error] application - 
  
  ! @6dckhec3c - Internal server error, for request [GET /articles] -&gt;
  
  play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[ClassCastException: java.lang.Object cannot be cast to play.api.libs.json.JsValue]]
      at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1.jar:2.0.4]
      at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1.jar:2.0.4]
      at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor.jar:2.0.2]
      at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.4]
      at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor.jar:2.0.2]
      at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor.jar:2.0.2]
  Caused by: java.lang.ClassCastException: java.lang.Object cannot be cast to play.api.libs.json.JsValue
      at $Proxy53.toJson(Unknown Source) ~[na:na]
      at controllers.ArticleController$$anonfun$index$1.apply(ArticleController.scala:111) ~[classes/:2.0.4]
      at controllers.ArticleController$$anonfun$index$1.apply(ArticleController.scala:97) ~[classes/:2.0.4]
      at play.api.mvc.Action$$anon$1.apply(Action.scala:170) ~[play_2.9.1.jar:2.0.4]
      at play.core.ActionInvoker$$anonfun$receive$1$$anonfun$6.apply(Invoker.scala:126) ~[play_2.9.1.jar:2.0.4]
      at play.core.ActionInvoker$$anonfun$receive$1$$anonfun$6.apply(Invoker.scala:126) ~[play_2.9.1.jar:2.0.4]
  [debug] application - [securesocial] calling deleteExpiredTokens()
  </code></pre>
  
  <p>Any idea why? That looks okay to me.</p>