当字段可以具有不同的原始值类型时,我在解析 json 时遇到问题。例如,我可以得到 json:
{
"name" : "john",
"age" : 31
}
或者它可以是这种形式:
{
"name" : "john",
"age" : "thirty one"
}
或者以这种方式:
{
"name" : "john",
"age" : 31.0
}
我希望能够解析字段
age
到以下 ADT 实例:sealed trait PrimitiveWrapper
case class IntWrapper(v: Int) extends PrimitiveWrapper
case class StringWrapper(v: String) extends PrimitiveWrapper
case class FloatWrapper(v: Float) extends PrimitiveWrapper
所以最后我可以得到这样的东西:
case class Person(name: String, age: PrimitiveWrapper)
我怎样才能做到这一点?我找到了这个话题:How to decode an ADT with circe without disambiguating objects
但是在那个解决方案中,我们解析的不是原始字段。
最佳答案
你可以这样做:
import cats.syntax.functor._
import io.circe.Decoder, io.circe.generic.auto._
sealed trait PrimitiveWrapper
case class IntWrapper(v: Int) extends PrimitiveWrapper
case class StringWrapper(v: String) extends PrimitiveWrapper
case class FloatWrapper(v: Float) extends PrimitiveWrapper
case class Person(name: String, age: PrimitiveWrapper)
object GenericDerivation {
implicit val decodePrimitiveWrapper: Decoder[PrimitiveWrapper] =
List[Decoder[PrimitiveWrapper]](
Decoder.decodeInt.map(IntWrapper).widen,
Decoder.decodeString.map(StringWrapper).widen,
Decoder.decodeFloat.map(FloatWrapper).widen
).reduceLeft(_ or _)
def main(args: Array[String]): Unit = {
import io.circe.parser.decode
println(decode[Person]("""{"name" : "john", "age" : 31 }"""))
println(decode[Person]("""{"name" : "john", "age" : "thirty one" }"""))
println(decode[Person]("""{"name" : "john", "age" : 31.3 }"""))
// Prints
// Right(Person(john,IntWrapper(31)))
// Right(Person(john,StringWrapper(thirty one)))
// Right(Person(john,FloatWrapper(31.3)))
}
}
注:以下使用
IntWrapper
进行解析 println(decode[Person]("""{"name" : "john", "age" : 31.0 }"""))
更新:正如@Travis 指出的那样
decodePrimitiveWrapper
可以这样写: implicit val decodePrimitiveWrapper: Decoder[PrimitiveWrapper] =
Decoder.decodeInt.map(IntWrapper).widen[PrimitiveWrapper] or
Decoder.decodeString.map(StringWrapper).widen[PrimitiveWrapper] or
Decoder.decodeFloat.map(FloatWrapper).widen[PrimitiveWrapper]
关于json - 在 Circe 中解析原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56039119/