spray-json JsonFormat案例类

时间:2014-10-21 11:48:26

标签: json scala spray-json

我正在尝试为Generic的案例类实现JsonFormat对象时遇到此问题。 这是我的班级:

case class SimpleQuery[T](field : String, op : Operator, value : T) extends Query{
  def getType = ????
}

我正在尝试使用喷json的github页面提示的格式:

implicit def SimpleQueryJsonFormat[A <: JsonFormat] = jsonFormat4(SimpleQuery.apply[A])

但是我得到了这个编译错误

trait JsonFormat takes type parameters

spray-json github页面的示例如下:

case class NamedList[A](name: String, items: List[A])

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit def namedListFormat[A :JsonFormat] = jsonFormat2(NamedList.apply[A])
}

这似乎与我的相似。

我还会在github页面中打开一个问题。

提前谢谢

1 个答案:

答案 0 :(得分:0)

我认为您可能会在定义的type参数中混淆<::

在你的[A <: JsonFormat]中,A表示&#34; JsonFormat延伸[A :JsonFormat]&#34;。

在示例中,A表示带有隐式JsonFormat[A]&#34;的&#34; implicit aFormat: JsonFormat[A]。它与要求隐式(但未命名)参数相同,例如value: T。这需要格式化您班级的<:部分。

TL; DR,尝试将:切换为{{1}}