2016-09-12 6 views
2

ケースクラスをspray.io jsonでjsonに変換しようとしています。以下のコード:ネストされたケースクラスとスプラッシュjson implicitsの使用方法

case class Value(amt: Int) 
case class Item(name: String, count: Value) 
object MyJsonProtocol extends DefaultJsonProtocol { 
    implicit val itemFormat = jsonFormat2(Item) 
} 
import MyJsonProtocol._ 
import spray.json._ 
val json = Item("mary", Value(2)).toJson 
println(json) 

が与える:

could not find implicit value for evidence parameter of type onextent.bluecase.examples.ex1.ExampleJson2.MyJsonProtocol.JF[Value] 

私は同様に価値のためJsonProtocolを定義しますが、同じ取得しようとしました。 stackoverflowの検索私はgenericsに関連するこのエラーのみを表示しますが、これはそうではありません。

私には何が欠けていますか? (今implicitsについての再読...)

答えて

3

あなたのValueクラスのためのjsonフォーマットがあなたのItemクラスの一部であるので、これは必要です。したがって、あなたのオブジェクトは次のようになります:

object MyJsonProtocol extends DefaultJsonProtocol { 
implicit val valueFormat = jsonFormat1(Value) 
implicit val itemFormat = jsonFormat2(Item) 
} 
関連する問題