0

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?

Aarkon
  • 484
  • 1
  • 6
  • 16

1 Answers1

0

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.

yangzai
  • 962
  • 5
  • 11
  • That gives me another error: `Fiber failed. A checked error was not handled. .data[0](invalid disambiguator)` – Aarkon Dec 30 '21 at 11:06
  • We need to see the data you are parsing, can you provide a small sample that reproduces the errors? – yangzai Dec 30 '21 at 11:10
  • Of course, I could have thought about that myself: https://cex.io/rest-api#ticker – Aarkon Dec 30 '21 at 11:13
  • 1
    You cannot use a `sealed trait` (ADT) for data like this, the paser would not be able to disambiguate between `CexPairWithBidAsk` and `CexPairWithoutBidAsk`. See in the fruit example how it needs the "Banana" and "Apple" keys: https://zio.github.io/zio-json/docs/overview/overview_decoding. I would suggest you use a single case class to represent `CexPair` and for the fields that might be missing use a `Maybe` type. – yangzai Dec 30 '21 at 11:25
  • I mean `Option` type. `Maybe` is for Haskell. – yangzai Dec 30 '21 at 11:31
  • I didn't ask if you needed but if you wanted them, trying to be polite. I don't see how I gave you the impression that I'm lazy here though. Never mind, thank you anyway. – Aarkon Dec 30 '21 at 11:55
  • "I didn't ask if you needed but if you wanted them". So which of your existing comments did you "ask if I wanted them"? Noone is obligated to tell you how they voted. You can make a report if you think the votes aren't fair. – yangzai Dec 30 '21 at 13:16