When I use the following code:
import JsonImpl.graphFormat
val js = Json.toJson(g)(graphFormat)
My code compiles and works fine but when I do this it doesn't work and says: "No Json serializer found for type SGraph. Try to implement an implicit Writes or Format for this type."
import JsonImpl.graphFormat
val js = Json.toJson(g)
JsonImpl
is:
object JsonImpl{
implicit val graphFormat = Json.format[SGraph]
}
I don't want to use companion object for my SGraph
class. What is the problem and why it cannot find the implicit value?
For the sake of completeness:
Json.format
is a macro, and when you're dealing with macros it's a good idea to make sure what you're getting back is appropriately statically typed:In fact this is a good idea whenever you're dealing with implicit values, and it'll save you a lot of confusion (sometimes because you've done something wrong, and sometimes because the compiler has).