ZIO Json: Failing Fiber on List in Json

483 Views Asked by At

I've got some data structures which I'm trying to hydrate from JSON. The short version (or the full version here):

sealed trait CexPair:
    val timestamp: String
    // ... (cut for brevity)
  implicit val decoderCexPair: JsonDecoder[CexPair] = DeriveJsonDecoder.gen

case class CexPairWithBidAsk(
      timestamp: String,
      // snip
      bid: Double,
      ask: Double
  ) extends CexPair
  implicit val decoderWithBidAsk: JsonDecoder[CexPairWithBidAsk] = DeriveJsonDecoder.gen

case class CexPairWithoutBidAsk(
    timestamp: String,
    // snip
  ) extends CexPair
  implicit val decoderWithoutBidAsk: JsonDecoder[CexPairWithoutBidAsk] = DeriveJsonDecoder.gen

case class Tickers(data: Seq[CexPair])
implicit val decoderTickers: JsonDecoder[Tickers] = DeriveJsonDecoder.gen

implicit val decoderPairs: JsonDecoder[Seq[CexPair]] =
  decoderTickers.map(_.data)

But when I try to run this, I get:

Assertion failed:
  Fiber failed.
  A checked error was not handled.
  .data(expected '{' got '[')

This tells me that deserializing Tickers already goes haywire. Is this because a sealed trait is not the ideal basis for unmarshalling JSON or am I missing something entirely different?

1

There are 1 best solutions below

7
On

For you test case that is trying to parse tickers <- ZIO.fromEither(str.fromJson[Tickers]), he custom JsonDecoder decodePairs expects the data field of Tickers to also be parsed as Tickers (before mapping in a Seq) instead of a List \ Seq hence it is expecting { instead of [.

Just remove decoderPairs and it will fallback to the default decoder for Seq and hopefully it should parse fine if there are no other issues.