使用 Circe 和单片眼镜解析 Json

Parse Json using Circe and monocle lens

我写了这个示例代码

package com.abhi
import io.circe._
import io.circe.optics.JsonPath._

object CirceTest extends App {
   val id = root.id.long
   val date = root.date.long

   val input =
      """
        |{
        |  "id" : 0,
        |  "childIds" : [
        |    11, 12, 13
        |  ],
        |  "date" : 1480815583505
        |}
      """.stripMargin
   parser.parse(input) match {
      case Left(a) => println(s"failed ${a}")
      case Right(json) =>
         val myId = id.getOption(json).get
         val myDate = date.getOption(json).get
         println(s"id: ${myId} date: ${myDate}")
   }
}

但这不会编译

CirceTest.scala:26: constructor cannot be instantiated to expected type;
[error]  found   : scala.util.Right[A,B]
[error]  required: cats.data.Xor[io.circe.ParsingFailure,io.circe.Json]
[error]       case Right(json) =>
[error]            ^

我也试过了

val jsonEither = parser.parse(input)
if (jsonEither.isRight) {
   val json = jsonEither.right.get
   val myId = id.getOption(json).get
   val myDate = date.getOption(json).get
   println(s"id: ${myId} date: ${myDate}")
}

但这也失败了

[error] CirceTest.scala:27: value right is not a member of cats.data.Xor[io.circe.ParsingFailure,io.circe.Json]
[error]       val json = jsonEither.right.get
[error]                             ^
[error] one error found

我很惊讶。当我可以做 isRight 那么为什么编译器说我不能做 right

这是我的 build.sbt 文件

name := "CirceTest"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
   "io.circe" %% "circe-core" % "0.5.1",
   "io.circe" %% "circe-generic" % "0.5.1",
   "io.circe" %% "circe-parser" % "0.5.1",
   "io.circe" %% "circe-optics" % "0.5.1"
)

Circe 依赖于 Cats 库,该库最近删除了 cats.data.Xor,这是一种右偏向 Either 的类型。 Circe 0.5.0 及更早版本使用 cats.data.Xor 作为解析和解码的结果类型,但 0.6.0+ 使用标准库的 Either,因为 Xor 已不存在。

将您的 circe 依赖项更新为 0.6.1 将使您的代码按编写的方式工作。如果出于某种原因您停留在较早版本的 circe 上,则需要调整您的代码以使用 Xor。不过,我建议坚持使用最新版本——circe 和 Cats 都是新项目,而且进展很快。如果您停留在早期版本上,并且是因为依赖于 circe 的库,请联系 Gitter,我们将尝试与库维护者合作以更新内容。