Json4s and Enumeration
json4s の EnumSerializer
がちょっと使いづらい。何となれば二つの Enumeration を同時に扱えない。
import org.json4s.DefaultFormats import org.json4s.ext.EnumSerializer import org.json4s.native.JsonMethods.parse import org.json4s.native.Serialization.write object E1 extends Enumeration { val v1 = Value } object E2 extends Enumeration { val v2 = Value } case class C(e1: E1.Value, e2: E2.Value) implicit val f = DefaultFormats + new EnumSerializer(E1) + new EnumSerializer(E2) val c = C(E1.v1, E2.v2) println(c) // C(v1,v2) println(parse(write(c)).extract[C]) // C(v2,v2)
EnumSerializer の実装を見ると EnumerationClass で他のクラスと区別をつけているようだが、残念なことに classOf[E1.Value]
と classOf[E2.Value]
は区別がつかない。
println(classOf[E1.Value]) // class scala.Enumeration$Value println(classOf[E2.Value]) // class scala.Enumeration$Value
さてどうしたものか。
追記: ScalaType や TypeInfo を拡張して Type なり TypeTag なりを持たせるようにしていけば何とかできなくもなさそうだけど、コンストラクタを読み取る部分なども変えないといけないし割に合わなさそうなので当面はあきらめることにした。