My goal is to transform JSON into the following model:
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
- There is a container type whose structure always looks the same.
- The
content
of the container is represented by types that look totally different (regarding the amount and type of attributes). However, all content types are known at compile time and implement a sealed trait. - the
typeId
attribute of the container indicates the content type. E.g. a value ofN
means thatcontent
is of typeContentTypeN
and so on. - The JSON structure looks exactly like you would expect it to do and maps direcly to the Scala types shown above.
- (Btw: I'm open to change the container type to
Container[A <: Content]
if that's a more elegant solution).
What would be a good way to decode that with circe? I guess automatic decoding does not work in this case.
edit: the documentation of the json structure describes the content field as ?mixed (object, integer, bool)
, so it can also be a simple Int
or Boolean
instead of a case class object. But for now it would be ok to ignore these two types (although it would be nice to have a solution for that).