Last active
January 15, 2018 02:09
-
-
Save n4to4/4f325ae5e7fab665c0918755e4f4a061 to your computer and use it in GitHub Desktop.
circe AutoDerivation // http://immutables.pl/2017/02/25/customizing-circes-auto-generic-derivation/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Foo(fooId: Int, fooName: String) | |
object Foo extends io.circe.generic.AutoDerivation | |
case class Bar(barId: Int, barName: String) | |
object Bar { | |
import io.circe.Encoder | |
import io.circe.generic.semiauto._ | |
import io.circe.generic.extras.Configuration | |
implicit val config: Configuration = Configuration.default.withSnakeCaseMemberNames | |
implicit val encoder: Encoder[Bar] = deriveEncoder[Bar] | |
} | |
case class Baz(id: Int, name: String) | |
object Baz { | |
import io.circe.Encoder | |
implicit val encoder: Encoder[Baz] = | |
Encoder.forProduct2("baz-id", "baz-name")(b => (b.id, b.name)) | |
} | |
object Main extends App { | |
import io.circe.syntax._ | |
println(Foo(1, "name").asJson.noSpaces) | |
println(Bar(2, "name").asJson.noSpaces) | |
println(Baz(3, "name").asJson.noSpaces) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment