Custom encoders/decoders

If you want to write your own codec instead of using automatic or semi-automatic derivation, you can do so in a couple of ways.

Firstly, you can write a new Encoder[A] and Decoder[A] from scratch:

import io.circe.{ Decoder, Encoder, HCursor, Json }

class Thing(val foo: String, val bar: Int)

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: Encoder[Thing] = repl.Session$App$$anon$1@40838c90

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)
    }
}
// decodeFoo: Decoder[Thing] = repl.Session$App$$anon$2@682be297

But in many cases you might find it more convenient to piggyback on top of the decoders that are already available. For example, a codec for java.time.Instant might look like this:

import io.circe.{ Decoder, Encoder }
import java.time.Instant
import scala.util.Try

implicit val encodeInstant: Encoder[Instant] = Encoder.encodeString.contramap[Instant](_.toString)
// encodeInstant: Encoder[Instant] = io.circe.Encoder$$anon$1@2a71e163

implicit val decodeInstant: Decoder[Instant] = Decoder.decodeString.emapTry { str =>
  Try(Instant.parse(str))
}
// decodeInstant: Decoder[Instant] = io.circe.Decoder$$anon$14@4af8788f

Older scala versions

If you are using custom codecs and an older versions of scala (below 2.12) and you get errors like this value flatMap is not a member of io.circe.Decoder.Result[Option[String]] or value map is not a member of io.circe.Decoder.Result[Option[String]] then you need to use the following import: import cats.syntax.either._ to fix this.

Custom key types

If you need to encode/decode Map[K, V] where K is not String (or Symbol, Int, Long, etc.), you need to provide a KeyEncoder and/or KeyDecoder for your custom key type.

For example:

import io.circe._, io.circe.syntax._

case class Foo(value: String)

implicit val fooKeyEncoder: KeyEncoder[Foo] = new KeyEncoder[Foo] {
  override def apply(foo: Foo): String = foo.value
}
// fooKeyEncoder: KeyEncoder[Foo] = repl.Session$App$$anon$3@16438a4c
val map = Map[Foo, Int](
  Foo("hello") -> 123,
  Foo("world") -> 456
)
// map: Map[Foo, Int] = Map(Foo("hello") -> 123, Foo("world") -> 456)

val json = map.asJson
// json: Json = JObject(object[hello -> 123,world -> 456])

implicit val fooKeyDecoder: KeyDecoder[Foo] = new KeyDecoder[Foo] {
  override def apply(key: String): Option[Foo] = Some(Foo(key))
}
// fooKeyDecoder: KeyDecoder[Foo] = repl.Session$App$$anon$4@7ea44693

json.as[Map[Foo, Int]]
// res0: Decoder.Result[Map[Foo, Int]] = Right(
//   Map(Foo("hello") -> 123, Foo("world") -> 456)
// )

Custom key mappings via annotations

It’s often necessary to work with keys in your JSON objects that aren’t idiomatic case class member names in Scala. While the standard generic derivation doesn’t support this use case, the experimental circe-generic-extras module does provide two ways to transform your case class member names during encoding and decoding.

In many cases the transformation is as simple as going from camel case to snake case, in which case all you need is a custom implicit configuration:

import io.circe.generic.extras._, io.circe.syntax._

implicit val config: Configuration = Configuration.default.withSnakeCaseMemberNames
// config: Configuration = Configuration(
//   io.circe.generic.extras.Configuration$$$Lambda$7101/1793879894@5c19308a,
//   io.circe.generic.extras.Configuration$$$Lambda$7100/1456820902@63c9a0a3,
//   false,
//   None,
//   false
// )

@ConfiguredJsonCodec case class User(firstName: String, lastName: String)

User("Foo", "McBar").asJson
// res1: Json = JObject(object[first_name -> "Foo",last_name -> "McBar"])

In other cases you may need more complex mappings. These can be provided as a function:

import io.circe.generic.extras._, io.circe.syntax._

implicit val config: Configuration = Configuration.default.copy(
  transformMemberNames = {
    case "i" => "my-int"
    case other => other
  }
)
// config: Configuration = Configuration(
//   <function1>,
//   io.circe.generic.extras.Configuration$$$Lambda$7100/1456820902@63c9a0a3,
//   false,
//   None,
//   false
// )

@ConfiguredJsonCodec case class Bar(i: Int, s: String)

Bar(13, "Qux").asJson
// res3: io.circe.Json = JObject(object[my-int -> 13,s -> "Qux"])

Since this is a common use case, we also support for mapping member names via an annotation:

import io.circe.generic.extras._, io.circe.syntax._

implicit val config: Configuration = Configuration.default
// config: Configuration = Configuration(
//   io.circe.generic.extras.Configuration$$$Lambda$7099/2003630340@d446ae3,
//   io.circe.generic.extras.Configuration$$$Lambda$7100/1456820902@63c9a0a3,
//   false,
//   None,
//   false
// )

@ConfiguredJsonCodec case class Bar(@JsonKey("my-int") i: Int, s: String)

Bar(13, "Qux").asJson
// res5: io.circe.Json = JObject(object[my-int -> 13,s -> "Qux"])

It’s worth noting that if you don’t want to use the experimental generic-extras module, the completely unmagical forProductN version isn’t really that much of a burden:

import io.circe.Encoder, io.circe.syntax._

case class User(firstName: String, lastName: String)
case class Bar(i: Int, s: String)

implicit val encodeUser: Encoder[User] =
  Encoder.forProduct2("first_name", "last_name")(u => (u.firstName, u.lastName))
// encodeUser: Encoder[User] = io.circe.ProductEncoders$$anon$2@4f4f42a6

implicit val encodeBar: Encoder[Bar] =
  Encoder.forProduct2("my-int", "s")(b => (b.i, b.s))
// encodeBar: Encoder[Bar] = io.circe.ProductEncoders$$anon$2@33ee5d85

User("Foo", "McBar").asJson
// res7: io.circe.Json = JObject(
//   object[first_name -> "Foo",last_name -> "McBar"]
// )
Bar(13, "Qux").asJson
// res8: io.circe.Json = JObject(object[my-int -> 13,s -> "Qux"])

While this version does involve a bit of boilerplate, it only requires circe-core, and may have slightly better runtime performance in some cases.