zio / zio-json

Fast, secure JSON library with tight ZIO integration.
https://zio.dev/zio-json
Apache License 2.0
410 stars 146 forks source link

Use Scala 3 derivation instead of Magnolia for Scala 3 #434

Open fsvehla opened 3 years ago

fsvehla commented 3 years ago

Magnolia is moving quite slowly, and I think that I can implement all of it’s functionality using Scala 3’s auto derivation + a few simple macros for case class annotations.

plokhotnyuk commented 3 years ago

It would be great to have some handy approach for derivation configuration without annotations in Scala 3.

As an example, jsoniter-scala-macros for Scala 2 uses a configuration parameter for the make macro, which is passed to the macro implementation as an expression and evaluated in the compile-time using Eval.eval. That allowed to derive codecs with different customization without adding of annotations.

fsvehla commented 3 years ago

I’ll look into this, @plokhotnyuk, thanks for the pointer :)

plokhotnyuk commented 3 years ago

@fsvehla The problem only that Scala 3 doesn't have Eval.eval anymore...

fsvehla commented 3 years ago

Work on Decoder looks good, with the snag that I have yet to figure out why all sum type members need to define a Decoder.

  object examplesum {
    sealed abstract class Parent

    object Parent {
      implicit val decoder: JsonDecoder[Parent] = DeriveJsonDecoder.gen
    }

    case class Child1() extends Parent

    object Child1 {
      // not necessary with Magnolia
      implicit val decoder: JsonDecoder[Child1] = DeriveJsonDecoder.gen
    }

    case class Child2() extends Parent

    object Child2 {
      // not necessary with Magnolia
      implicit val decoder: JsonDecoder[Child2] = DeriveJsonDecoder.gen
    }
  }