Configuring generic derivation

This post is a kind of sequel to my previous article on type classes and generic derivation, although unfortunately there’s a lot of intermediate content that should go between there and here that I haven’t written yet. This post introduces a new feature in circe that I’m pretty excited about, though, so I’m not going to worry about skipping over that stuff for now.

One of the limitations of generic derivation in the current version of circe (0.2) is that it bakes in a lot of decisions about the mapping that should be used between your types and JSON. For example, suppose we’ve got an ADT like this (from the San Francisco city lots tutorial):

We can derive decoders and encoders for these types with circe:

This is great, but what if we want to process incoming JSON that looks like this?

The current answer is: you’re out of luck and have to write your own by hand. This isn’t too bad, since we can use the derived instances for the ADT leaves:

But still, it would be nice to be able to configure the derivation a bit.

One common way to solve this kind of problem in Scala is to pass a configuration object implicitly to the methods that provide our derived instances. Lots of Scala libraries and frameworks do it, and lots of Scala developers seem happy with it as a solution, but I’ve always found it confusing and error-prone. I don’t want to have to reason about what bits of information about configuration I’ve put into scope where, and as I say in the circe design guidelines, I wanted to avoid configuration-via-implicits until I was completely convinced there wasn’t another solution.

I’ve just submitted a pull request that proposes such a solution. Now if we’ve got our Geometry type and a document like this:

We can derive a configured decoder like this:

And print configured values like this:

The TypeField is a phantom type that tells circe to look for the discriminator in a field with the key “type”. It’s provided for convenience, but if we want to use a different value for the key, that’s also pretty easy:

There are currently four different options you can configure:

  1. The ADT discriminator: should it be an object wrapper (the default) or a field?
  2. Should case class member names be used verbatim as field names, or converted to snake case?
  3. Should case objects in an ADT be represented by empty objects or just their names?
  4. Should defaults values be used for case classes if they’re available?

These options can be composed, so if we’ve got an ADT like this:

We can configure our decoder to use snake case for keys, to represent the case object as a string, and to fill in missing fields with default values (if they exist):

Now suppose that we want to use snake case keys for the members of Foo, but not Baz. This is also pretty easy:

And then:

This overhaul of circe’s generic derivation also happens to fix an old issue where derivation would fail for large case classes.

The implementation in the pull request still needs some tests and clean-up, but these configuration options should be available in the circe 0.3.0 snapshot in the next couple of days.