我已经写了这个示例代码
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
我很惊讶。当我可以做 aisRight
那么为什么编译器说我不能做 a 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"
)