Cannot find JsonWriter or JsonFormat type class for a case class

為{幸葍}努か 提交于 2019-12-22 05:51:35

问题


Following the tutorial from http://www.smartjava.org/content/first-steps-rest-spray-and-scala, there are some unexpected error messages. What is going on? Have I defined implicit JsonWriter by the implicit val personFormat = jsonFormat3(Person) call?

scala> import spray.json.DefaultJsonProtocol
import spray.json.DefaultJsonProtocol

scala> object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val personFormat = jsonFormat3(Person)
}
     |      | defined object MyJsonProtocol

scala> case class Person(name: String, fistName: String, age: Long)
defined class Person

scala> import spray.json._
import spray.json._

scala> import MyJsonProtocol._
import MyJsonProtocol._

scala> Person(name="a", fistName="b", age = 10).toJson
<console>:45: error: Cannot find JsonWriter or JsonFormat type class for Person
              Person(name="a", fistName="b", age = 10).toJson
                                                       ^

回答1:


From your session it seems as if you define the protocol before you declare your Person class, which would mean that you already have another Person class in scope. After defining the protocol you then re-define the Person class, thus the format can't be found. So to sum up, make sure that you first declare your Person class and then define your Format.

EDIT: Updated answer



来源:https://stackoverflow.com/questions/28040828/cannot-find-jsonwriter-or-jsonformat-type-class-for-a-case-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!