找不到案例类的JsonWriter或JsonFormat类型类 [英] Cannot find JsonWriter or JsonFormat type class for a case class

查看:190
本文介绍了找不到案例类的JsonWriter或JsonFormat类型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循 http://www.smartjava的教程. org/content/first-steps-rest-spray-and-scala ,有一些意外的错误消息.到底是怎么回事?我是否通过implicit val personFormat = jsonFormat3(Person)调用定义了隐式JsonWriter?

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
                                                       ^

推荐答案

在您的会话中,好像您在声明Person类之前定义了协议,这意味着您已经在作用域中有了另一个Person类.定义协议后,您然后重新定义Person类,因此找不到格式.因此,总而言之,请确保首先声明Person类,然后定义格式.

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.

编辑:更新的答案

这篇关于找不到案例类的JsonWriter或JsonFormat类型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆