在Circe中解析基本类型

时间:2019-05-08 10:55:20

标签: json scala parsing circe

当field可以具有不同的原始值类型时,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

但是在这种解决方案中,我们不解析原始字段。

1 个答案:

答案 0 :(得分:3)

这是您可以执行的操作:

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]
相关问题