generic-derivation

Generic derivation for ADTs in Scala with a custom representation

不问归期 提交于 2019-12-18 13:28:08
问题 I'm paraphrasing a question from the circe Gitter channel here. Suppose I've got a Scala sealed trait hierarchy (or ADT) like this: sealed trait Item case class Cake(flavor: String, height: Int) extends Item case class Hat(shape: String, material: String, color: String) extends Item …and I want to be able to map back and forth between this ADT and a JSON representation like the following: { "tag": "Cake", "contents": ["cherry", 100] } { "tag": "Hat", "contents": ["cowboy", "felt", "black"] }

Generic derivation for ADTs in Scala with a custom representation

六月ゝ 毕业季﹏ 提交于 2019-11-30 19:19:08
I'm paraphrasing a question from the circe Gitter channel here. Suppose I've got a Scala sealed trait hierarchy (or ADT) like this: sealed trait Item case class Cake(flavor: String, height: Int) extends Item case class Hat(shape: String, material: String, color: String) extends Item …and I want to be able to map back and forth between this ADT and a JSON representation like the following: { "tag": "Cake", "contents": ["cherry", 100] } { "tag": "Hat", "contents": ["cowboy", "felt", "black"] } By default circe's generic derivation uses a different representation: scala> val item1: Item = Cake(

How to decode an ADT with circe without disambiguating objects

你。 提交于 2019-11-28 16:24:15
Suppose I've got an ADT like this: sealed trait Event case class Foo(i: Int) extends Event case class Bar(s: String) extends Event case class Baz(c: Char) extends Event case class Qux(values: List[String]) extends Event The default generic derivation for a Decoder[Event] instance in circe expects the input JSON to include a wrapper object that indicates which case class is represented: scala> import io.circe.generic.auto._, io.circe.parser.decode, io.circe.syntax._ import io.circe.generic.auto._ import io.circe.parser.decode import io.circe.syntax._ scala> decode[Event]("""{ "i": 1000 }""")

How to decode an ADT with circe without disambiguating objects

て烟熏妆下的殇ゞ 提交于 2019-11-27 19:58:29
问题 Suppose I've got an ADT like this: sealed trait Event case class Foo(i: Int) extends Event case class Bar(s: String) extends Event case class Baz(c: Char) extends Event case class Qux(values: List[String]) extends Event The default generic derivation for a Decoder[Event] instance in circe expects the input JSON to include a wrapper object that indicates which case class is represented: scala> import io.circe.generic.auto._, io.circe.parser.decode, io.circe.syntax._ import io.circe.generic