How can provide JsonFormats for case class that references itself ?
I'm following this guideline and wrote following code
case class Item(name: String, desc: Option[String], prices: Array[String], subitems: Option[List[Item]])
import spray.json._
import DefaultJsonProtocol._ // !!! IMPORTANT, else `convertTo` and `toJson` won't work
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val menuItemFormat = jsonFormat(Item, "name", "desc", "prices", "subitems")
}
import MyJsonProtocol._
and I get following error message meaning of which I unfortunately don't understand.
could not find implicit value for evidence parameter of type Hi.MyJsonProtocol.JF[Option[List[mypkg.Item]]]
implicit val menuItemFormat = jsonFormat(Item, "name", "desc", "prices", "subitems")
^
How can I fix it ?
for the recursive implicit to find itself, you need to give it an explicit type definition. Change your implicit to:
implicit val menuItemFormat: RootJsonFormat[Item] = jsonFormat(Item.apply, "name", "desc", "prices", "subitems")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With