Circe:解码具有不同可能内容类型的容器类型
Circe: decoding a container type with different possible content types
我的目标是将 JSON 转换为以下模型:
case class Container(typeId: Int, timestamp: Long, content: Content)
sealed trait Content
case class ContentType1(...) extends Content
case class ContentType2(...) extends Content
case class ContentType3(...) extends Content
- 有一种容器类型,其结构看起来总是一样的。
- 容器的
content
由看起来完全不同的类型表示(关于属性的数量和类型)。但是,所有内容类型在编译时都是已知的并实现密封特征。
- 容器的
typeId
属性表示内容类型。例如。 N
的值表示 content
是 ContentTypeN
类型,依此类推。
- JSON 结构看起来与您期望的完全一样,并直接映射到上面显示的 Scala 类型。
- (顺便说一句:如果这是一个更优雅的解决方案,我愿意将容器类型更改为
Container[A <: Content]
)。
用 circe 解码它的好方法是什么?我想自动解码在这种情况下不起作用。
edit:json结构的文档将content字段描述为?mixed (object, integer, bool)
,所以也可以是简单的Int
或 Boolean
而不是 case class 对象。但是现在可以忽略这两种类型(尽管有一个解决方案会很好)。
我会使用 semiauto(与 deriveDecoder),validate and or。
case class Container[+C](typeId: Int, timestamp: Long, content: C)
sealed trait Content
@JsonCodec case class ContentType1(...) extends Content
@JsonCodec case class ContentType2(...) extends Content
object Content {
import shapeless._
import io.circe._
import io.circe.generic.semiauto._
def decodeContentWithGuard[T: Decoder](number: Int): Decoder[Content[T]] = {
deriveDecoder[Content[T]].validate(_.downField("typeId") == Right(number), s"wrong type number, expected $number")
}
implicit val contentDecoder: Decoder[Container[Content]] = {
decodeWithGuard[ContentType1](1) or
decodeWithGuard[ContentType2](2)
}
}
如果您不想要协变注释,则必须将内部 ContentTypeX
映射并向上转换为 Content
。
可能还有一个没有泛型的解决方案,但是你不能使用 semiauto。
可能无法编译,没有测试。
我的目标是将 JSON 转换为以下模型:
case class Container(typeId: Int, timestamp: Long, content: Content)
sealed trait Content
case class ContentType1(...) extends Content
case class ContentType2(...) extends Content
case class ContentType3(...) extends Content
- 有一种容器类型,其结构看起来总是一样的。
- 容器的
content
由看起来完全不同的类型表示(关于属性的数量和类型)。但是,所有内容类型在编译时都是已知的并实现密封特征。 - 容器的
typeId
属性表示内容类型。例如。N
的值表示content
是ContentTypeN
类型,依此类推。 - JSON 结构看起来与您期望的完全一样,并直接映射到上面显示的 Scala 类型。
- (顺便说一句:如果这是一个更优雅的解决方案,我愿意将容器类型更改为
Container[A <: Content]
)。
用 circe 解码它的好方法是什么?我想自动解码在这种情况下不起作用。
edit:json结构的文档将content字段描述为?mixed (object, integer, bool)
,所以也可以是简单的Int
或 Boolean
而不是 case class 对象。但是现在可以忽略这两种类型(尽管有一个解决方案会很好)。
我会使用 semiauto(与 deriveDecoder),validate and or。
case class Container[+C](typeId: Int, timestamp: Long, content: C)
sealed trait Content
@JsonCodec case class ContentType1(...) extends Content
@JsonCodec case class ContentType2(...) extends Content
object Content {
import shapeless._
import io.circe._
import io.circe.generic.semiauto._
def decodeContentWithGuard[T: Decoder](number: Int): Decoder[Content[T]] = {
deriveDecoder[Content[T]].validate(_.downField("typeId") == Right(number), s"wrong type number, expected $number")
}
implicit val contentDecoder: Decoder[Container[Content]] = {
decodeWithGuard[ContentType1](1) or
decodeWithGuard[ContentType2](2)
}
}
如果您不想要协变注释,则必须将内部 ContentTypeX
映射并向上转换为 Content
。
可能还有一个没有泛型的解决方案,但是你不能使用 semiauto。
可能无法编译,没有测试。