Skip to content

Instantly share code, notes, and snippets.

@taig
Created November 26, 2016 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taig/03386fa584bd48f392f9d812b1c86072 to your computer and use it in GitHub Desktop.
Save taig/03386fa584bd48f392f9d812b1c86072 to your computer and use it in GitHub Desktop.
Circe Value Class codecs
implicit def decoderValueClass[T <: AnyVal, V](
implicit
g: Lazy[Generic.Aux[T, V :: HNil]],
d: Decoder[V]
): Decoder[T] = Decoder.instance { cursor ⇒
d( cursor ).map { value ⇒
g.value.from( value :: HNil )
}
}
implicit def encoderValueClass[T <: AnyVal, V](
implicit
g: Lazy[Generic.Aux[T, V :: HNil]],
e: Encoder[V]
): Encoder[T] = Encoder.instance { value ⇒
e( g.value.to( value ).head )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment