zio-json icon indicating copy to clipboard operation
zio-json copied to clipboard

Use Scala 3 derivation instead of Magnolia for Scala 3

Open fsvehla opened this issue 2 years ago • 4 comments

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.

fsvehla avatar Sep 15 '21 07:09 fsvehla

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.

plokhotnyuk avatar Sep 15 '21 08:09 plokhotnyuk

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

fsvehla avatar Sep 15 '21 19:09 fsvehla

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

plokhotnyuk avatar Sep 16 '21 05:09 plokhotnyuk

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
    }
  }

fsvehla avatar Sep 29 '21 10:09 fsvehla