Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find JsonWriter or JsonFormat type class for a case class

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
                                                       ^
like image 898
mirelon Avatar asked Jan 20 '15 08:01

mirelon


1 Answers

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

like image 114
edi Avatar answered Oct 04 '22 06:10

edi