Circe: Privileged configurable generic derivation

Created on 15 Jan 2016  路  5Comments  路  Source: circe/circe

From your post https://meta.plasm.us/posts/2016/01/14/configuring-generic-derivation/

I was wondering if perhaps in a codebase, where there is a set convention (in my case snake_case over camelCase keys), it would be possible to configure that the priviledged .asJson to would always mean use SnakeCaseKeys, and do the same also for the decoding API.

enhancement question

Most helpful comment

I'm going to reopen this issue because I can imagine providing explicit support for this鈥攅.g. something like:

import io.circe.generic._, io.circe.generic.config._

val deriver: ConfiguredDeriver[SnakeCaseKeys] = ConfiguredDeriver[SnakeCaseKeys]

import deriver._

I'm not 100% sure I like this, but I think it's worth discussing.

All 5 comments

It's possible to do this now:

import io.circe._, io.circe.generic.auto._, io.circe.generic.config._

implicit def snakeCaseDecoder[A](implicit
  decoder: ConfiguredDecoder[SnakeCaseKeys, A]
): export.Export0[Decoder[A]] = new export.Export0(decoder)

This exports instances configured to use snake-case at a higher priority that the default for exported instances. These won't take priority over custom instances, but they do outrank the default unconfigured derived instances.

(Note that this is just a demonstration鈥攊n real code you'd probably want to define something like a SnakeCaseDecoder subclass of Decoder and export instances with @exports(HighPriority)).

That's awesome, especially the priority over custom instances part. Thank you.

I'm going to reopen this issue because I can imagine providing explicit support for this鈥攅.g. something like:

import io.circe.generic._, io.circe.generic.config._

val deriver: ConfiguredDeriver[SnakeCaseKeys] = ConfiguredDeriver[SnakeCaseKeys]

import deriver._

I'm not 100% sure I like this, but I think it's worth discussing.

That would be great to be able to make the whole scope to use snake case keys because usually APIs accept keys encoded in exactly one style.

Using a package object at the root and declaring the package as such:

package myrootpackage
package foo
Was this page helpful?
0 / 5 - 0 ratings