zio / zio-json

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

Implement inheritDiscriminator annotation #1113

Open pablf opened 1 month ago

pablf commented 1 month ago

This is a different approach than #1112. The PR implements a inheritDiscriminator annotation to be used on case classes. If the parent class doesn't have a jsonDiscriminator annotation it will throw a compilation error when deriving a codec. When there is a jsonDiscriminator it will include this in the encoding with the name of the case class or the jsonHint.

I don't know if this is the exact use case of @alphaho, but I guess it might be encoding and decoding with different encoders for convenience when interacting between different parts of the code base. In these cases it might be easier to use something like this annotation. And this isn't a breaking change. What do you think @987Nabil? fixes #1056 /claim #1056

The final code for the example of @alphaho would be

import zio.json._

@jsonDiscriminator("type")
sealed trait Animal
object Animal {
  @inheritDiscriminator @jsonHint("dog")
  case class Dog(name: String) extends Animal
  object Dog {
    implicit val dogCodec: JsonCodec[Dog] = DeriveJsonCodec.gen
  }

  @inheritDiscriminator @jsonHint("cat")
  case class Cat(name: String, weight: Double) extends Animal

  implicit val animalCodec: JsonCodec[Animal] = DeriveJsonCodec.gen
}
987Nabil commented 1 month ago

My personal opinion is, that this is a very special solution for a probably fringe use case that is also possible to have with more generic solution that already exists. I would not merge this. Maybe @fsvehla or @jdegoes can give their opinion too.

jdegoes commented 1 month ago

@pablf In your example, do you really need:

    implicit val dogCodec: JsonCodec[Dog] = DeriveJsonCodec.gen

or is this a copy/paste error?

987Nabil commented 1 month ago

@jdegoes the purpose was to have a Dog decoder that fails without the discriminator. How would this work, if you don't create one explicitly?

pablf commented 1 month ago

@jdegoes I think so. I don't understand completely the exact needs behind the feature request. Possibly the approach with map or transform should be enough for the use case of @alphaho.