Cannot Serialise a Map with Sealed Trait as Key Type

163 views Asked by At

I cannot seem to specify the formatting for this type:

sealed trait Baz
object Baz {
  case object A extends Baz

  implicit val format: OFormat[Baz] = derived.oformat[Baz]()
}
final case class Foo(s: Map[Baz, String])
object Foo {
  implicit val format: OFormat[Foo] = Json.format[Foo]
}

I use 


```scala
    "org.julienrf"               %% "play-json-derived-codecs"   % "7.0.0"

for the serialisation of the sealed trait and sub-types and usual Play JSON formatting for the Foo type.

But I get this issue:

No instance of play.api.libs.json.Format is available for scala.collection.immutable.Map[Baz, java.lang.String] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)

I thought play-json-derived-codecs lib would provide the formatting for the Baz type and that would be sufficient. If Foo is changed to

final case class Foo(s : Map[String,String])

all is good.

0

There are 0 answers