Circe: Custom encoders/decoders example failing with Scala 2.11.x

Created on 22 Sep 2017  路  6Comments  路  Source: circe/circe

The example from the documentation fails on Scala 2.11.11, but I can run it successfully on Scala 2.12.x

scala> class Thing(val foo: String, val bar: Int)
defined class Thing

scala> implicit val encodeFoo: Encoder[Thing] = new Encoder[Thing] {
     |   final def apply(a: Thing): Json = Json.obj(
     |     ("foo", Json.fromString(a.foo)),
     |     ("bar", Json.fromInt(a.bar))
     |   )
     | }
encodeFoo: io.circe.Encoder[Thing] = $anon$1@bfe134d

scala> implicit val decodeFoo: Decoder[Thing] = new Decoder[Thing] {
     |   final def apply(c: HCursor): Decoder.Result[Thing] =
     |     for {
     |       foo <- c.downField("foo").as[String]
     |       bar <- c.downField("bar").as[Int]
     |     } yield {
     |       new Thing(foo, bar)
     |     }
     | }
<console>:17: error: value flatMap is not a member of io.circe.Decoder.Result[String]
             foo <- c.downField("foo").as[String]
                                         ^
<console>:18: error: value map is not a member of io.circe.Decoder.Result[Int]
             bar <- c.downField("bar").as[Int]

Most helpful comment

To overcome this issue you can use cats either, simply import it:
import cats.syntax.either._

All 6 comments

got the same error on scala 2.10.6

I have the same issue on Scala 2.11, with Circe 0.5.2 version

I think the problem is that the Scala 2.11.x Either does not have the map/flatMap, the new methods are introduced in the 2.12 version

To overcome this issue you can use cats either, simply import it:
import cats.syntax.either._

Why not update this in the document? new users may get stuck in this problem.

@djvulee This has slipped through the cracks, but a PR would be very welcome.

Was this page helpful?
0 / 5 - 0 ratings