circe

Scala Circe with generics

假装没事ソ 提交于 2019-11-29 05:11:59
I am trying to use the scala json library Circe, wrapping it in a simple trait to provide conversion to/from json for which I have the following: import io.circe.generic.auto._ import io.circe.parser._ import io.circe.syntax._ trait JsonConverter { def toJson[T](t : T) : String def fromJson[T](s: String) : T } case class CirceJsonConverter() extends JsonConverter{ override def toJson[T](t: T): String = t.asJson.noSpaces override def fromJson[T](s: String): T = decode[T](s).getOrElse(null).asInstanceOf[T] } The aim of this is to simply be able to call JsonConverter with any object and have it

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