I have the following JSON:
[{"type": "first_variant", ...}, {"type": "second_variant", ...}]
And I want to parse this JSON to an array of Variant enums like this:
enum Variant:
case FirstVariant(...)
case SecondVariant(...)
How can I do it with circe?
In Scala 3 and Circe 0.14.6, you can require a discriminator field in snake case by providing the appropriate
ConfigurationandCodec:This outputs:
References: I derived this example from the (outdated) Circle documentation on ADTs, the answer to a similar question for Circe 0.7.0 and the source code of Circe 0.14.6 (
Configuration.scalaandConfiguredDerivesSuite.scala).