Cannot find JsonWriter or JsonFormat class for case class

Following the guide http://www.smartjava.org/content/first-steps-rest-spray-and-scala , some unexpected error messages appear. What's happening? I defined implicit JsonWriter by calling implicit val personFormat = jsonFormat3(Person) ?

 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 ^ 
+6
source share
1 answer

From your session, it seems that you are defining a protocol before declaring your Person class, which means that you already have another Person class in the scope. After defining the protocol, you then override the Person class, so the format cannot be found. So, to summarize, make sure you first declare your Person class, and then define your format.

EDIT : Updated Answer

+6
source

Source: https://habr.com/ru/post/981255/


All Articles