spray / spray-json

A lightweight, clean and simple JSON implementation in Scala
Apache License 2.0
969 stars 190 forks source link

How to debug "Cannot find JsonWriter or JsonFormatter type class" errors #323

Open eswenson1 opened 4 years ago

eswenson1 commented 4 years ago

I've been trying for hours to get some code to compile and about to give up on spray-json as I'm having no luck. I'm trying to get rid of the error:

Error:(18, 24) Cannot find JsonWriter or JsonFormat type class for com.intertrust.modulus.services.audit.Event
        val result = e.toJson

I've used spray-json for years and am always confounded by errors such as this, and seek some methodology for debugging these and isolating WHY I get these occasionally when I'm pretty certain that I have implicit json formatters in-scope for all the case classes that extend the base trait (here Event).

When the hierarchy is:

    trait Event extends Product with Serializable 
    sealed trait AccountEvent extends Event
    case class AccountAdded(name: String, password: String) extends AccountEvent
    case class AccountDeleted(name: String) extends AccountEvent
    case class AccountPasswordChanged(name: String, password: String) extends AccountEvent
    sealed trait FooEvent extends Event
    case class FooEvent1() extends FooEvent

The compiler should be able to find that the ONLY types that extend Event are those case classes (since the trait from which they derive is sealed). So why can't it find the implicit jsonFormatter I have defined in a trait that is extended by the class in which the error is occurring?