1

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.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Mojo
  • 1,152
  • 1
  • 8
  • 16
  • Well what would you expect the serialized map to look like? Maps are serialized to JSON objects, and JSON objects can only have strings keys. – Matthias Berndt Sep 11 '21 at 22:33
  • Oh damn, I didn't think of that! So this is impossible – Mojo Sep 11 '21 at 22:34
  • 1
    Some json libs have key serializer/deserializer concept and allow using custom types as keys. Usually you need to provide yet another encoder for it (in addition to the one you derived for values). – michaJlS Sep 12 '21 at 08:08
  • 3
    You'll need to define a `KeyReads` and `KeyWrites` for `Baz` (strangely there doesn't seem to be a `KeyFormat`). I don't expect play-json-derived-codecs will help there. – Levi Ramsey Sep 12 '21 at 09:52
  • You could also define a format for a `Map[K, V]` which serialized as something other than a JSON object (e.g., a list of key-value lists) if there's no good encoding of `Baz` as a string. – Levi Ramsey Sep 12 '21 at 09:56

0 Answers0