I have a Scala project that uses a bunch of Java code, for example this Java source:
public enum Category { FOO, BAR };
I then have a bunch of Scala case classes that I serialise to and from JSON using Argonaut like this:
case class Thing (a: String, b: Int, c: Float)
object Thing {
implicit val j = casecodec3 (Thing.apply, Thing.unapply)("a", "b", "c")
implicit val e: Equal[Guild] = Equal.equal (_ == _)
}
Fine, now I want to write a Scala case class that uses a Java enum like so:
case class Thing (a: String, b: Int, c: Float, d: Category)
object Thing {
implicit val j = casecodec4 (Thing.apply, Thing.unapply)("a", "b", "c", "d")
implicit val e: Equal[Guild] = Equal.equal (_ == _)
}
This will yield a compilation error because there is no implicit codec for the Category enum.
I guess I could write my own codec specifically for dealing with the Category enum by doing something like this:
package object ArgonautImplicits {
implicit val dx: DecodeJson[Category] = StringDecodeJson.map(x => Category.valueOf(x))
implicit val ex: EncodeJson[Category] = EncodeJson(x => jString(x.toString))
}
But I want to know if there is a way to write a single codec that will automatically handle any Java enum.