json - 在 Circe 中解析原始类型

标签 json scala parsing circe

当字段可以具有不同的原始值类型时,我在解析 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/

相关文章:

ios - 从 JSON 响应中获取货币符号 unicode 字符以呈现在 UILabel 中

android - 想要在每个标记上显示设备 ID,从服务器接收到的设备 ID

c++ - 我是否应该为 C++ 中的每种类型都有一个解析器?

java - Skype4Java 在 Mac OS 上通过 Scala

ruby - 是什么阻止了静态类型语言拥有像 Ruby 的 method_missing 这样的东西?

Java DOM : getElementsByTagName is returning no elements?

java - 查看更改时出现错误 JSON 输入意外结束

regex - Scala 正则表达式 : how to return matches as array or list

c++ - 无序时解析逗号分隔的语法

php - 解析 WordPress 帖子内容