Finding the implicit value for Json in play

211 views Asked by At

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?

1

There are 1 answers

0
Travis Brown On BEST ANSWER

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:

object JsonImpl{
  implicit val graphFormat: Format[SGraph] = Json.format[SGraph]
}

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).