Using Circe to Encode and Decode JSON in Scala

Поделиться
HTML-код
  • Опубликовано: 19 сен 2024

Комментарии • 6

  • @JanekBogucki
    @JanekBogucki 4 года назад

    Nice to see a full example buiilt up like this.
    Just wondering why you could not go "full potato" with automatic derivation given adequate test coverage.

    • @appliedfunctor
      @appliedfunctor  4 года назад +1

      Hah :) Good question. You absolutely can, it's a preference to avoid magic. The additional boilerplate arguably makes it more explicit and avoids having to include imports for auto and syntax at the calling sites.
      For anyone not sure, to use auto in the video example remove all the semiauto encoders/decoders except for the value class special case 'Summink' which requires custom codecs, and change the lhs test assertion for the decoder to incomingJson.as[Message] and the encoder to msg.asJson. Also need to add imports for io.circe.generic.auto._ and io.circe.syntax._ at the call locations (just the spec file in this case).
      Thanks for the feedback.

    • @JanekBogucki
      @JanekBogucki 4 года назад

      @@appliedfunctor I totally get the reasons for semiauto but good to know the tiny type Summink can accommodated in the fully automatic mode.

  • @bekturdiyuldashev5776
    @bekturdiyuldashev5776 3 года назад +1

    Why did you use the final case class, can we use the case class itself?

    • @appliedfunctor
      @appliedfunctor  3 года назад +1

      Habit. Practically, there's not really a benefit, it just prevents extending the case class: nrinaudo.github.io/scala-best-practices/tricky_behaviours/final_case_classes.html

    • @bekturdiyuldashev5776
      @bekturdiyuldashev5776 3 года назад

      @@appliedfunctor Thanks, I will read this