Reputation: 31546
I have written this sample code
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}")
}
}
but this won't event compile
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] ^
I also tried
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}")
}
but this also fails
[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
I am pretty surprised. when I can do a isRight
then why does compiler say that I can't do a right
?
Here is my build.sbt file
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"
)
Upvotes: 1
Views: 1036
Reputation: 139038
Circe depends on the Cats library, which recently removed cats.data.Xor
, which was a right-biased Either
-like type. Circe 0.5.0 and earlier versions used cats.data.Xor
as the result type for parsing and decoding, but 0.6.0+ use the standard library's Either
, since Xor
is gone.
Updating your circe dependency to 0.6.1 will make your code work as written. If for some reason you're stuck on an earlier version of circe, you'll need to adjust your code to work with Xor
. I'd suggest sticking to the most recent version, though—both circe and Cats are young projects and things are moving quickly. If you are stuck on an earlier version, and it's because of a library that depends on circe, please get in touch on Gitter and we'll try to work with the library maintainers to get things updated.
Upvotes: 3