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?
For you test case that is trying to parse
tickers <- ZIO.fromEither(str.fromJson[Tickers])
, he customJsonDecoder
decodePairs
expects thedata
field ofTickers
to also be parsed asTickers
(before mapping in aSeq
) instead of aList
\Seq
hence it is expecting{
instead of[
.Just remove
decoderPairs
and it will fallback to the default decoder forSeq
and hopefully it should parse fine if there are no other issues.