首页 > 解决方案 > 无法序列化以 Sealed Trait 作为键类型的地图

问题描述

我似乎无法指定这种类型的格式:

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"

用于密封特征和子类型的序列化以及 Foo 类型的通常播放 JSON 格式。

但我得到了这个问题:

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)

我认为 play-json-derived-codecs lib 会为 Baz 类型提供格式,这就足够了。如果 Foo 更改为

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

一切都很好。

标签: scalaplayframeworkimplicitgeneric-derivation

解决方案


推荐阅读