我正在尝试从JValue(从json4s)到Scala案例类(包含尖顶数字存在类型)制作通用映射器,反之亦然,scala案例类类似于:
case class SomeStat[T : Numeric](a:T, b:T)
对于通用映射器,我有类似的东西:
final class SynchronizerClass[B : Numeric, A <: SomeStat[B]](implicit manifest: Manifest[B], a:Manifest[A])
{format => (
{case j:JValue => {
implicit val formats = format
def extractNumber(j:JValue) = {
for {
a <- scala.util.Try((j \ "a").extract[B]).toOption
b <- scala.util.Try((j \ "b").extract[B]).toOption
} yield {
SomeStat(a,b).asInstanceOf[A]
}
}
(extractNumber(j)) getOrElse {
throw new MappingException("Invalid Number Type")
}
}},
{case x:SomeStat[A] =>
implicit val formats = format
Map(
"a" -> Extraction.decompose(x.a),
"b" -> Extraction.decompose(x.b)
)}
)}
问题是spire隐含有两个用于Numeric,作为一个例子我试图构造这样的东西
val j:JValue = ??? // Some Json Value
val someStat = Extraction.decompose(SomeStat[BigDecimal](j))