circe

How to Use Circe for Decoding JSON Lists/Arrays in Scala

别说谁变了你拦得住时间么 提交于 2019-12-07 06:46:20
问题 I have the code snippet cursor.downField("params").downField("playlist").downField("items").as[List[Clip]] Where Clip is a simple case class of strings and numbers. The incoming Json should contain a json object "playlist" with an array of "items" where each item is a clip. So the json should look like { "playlist": { "name": "Sample Playlist", "items": [ { "clipId":"xyz", "name":"abc" }, { "clipId":"pqr", "name":"def" } ] } } With the code snippet above, I'm getting the compile error: Error:

Circe and Scala's Enumeration type

荒凉一梦 提交于 2019-12-06 01:48:01
问题 I'm trying to wrap my head around Circe. So, here's the model I've been given: object Gender extends Enumeration { type Gender = Value val Male, Female, Unisex, Unknown = Value } case class Product(id: String, gender: Gender.Value) I want to a) encode this simple example to a JSON string val product = Product(id = "1234", gender = Gender.Female) b) map the resulting JSON back onto the Product case class. My own attempt didn't get me very far: object JsonProtocol { implicit val productDecoder:

Update case class from incomplete JSON with Argonaut or Circe

这一生的挚爱 提交于 2019-12-05 17:43:10
问题 I need to create an updated instance from a case class instance (with any needed DecodeJson s implicitly derived), given an incomplete json (some fields missing). How can this be accomplished with Argonaut (preferably) or Circe (if I have to)? Example: case class Person(name:String, age:Int) val person = Person("mr complete", 42) val incompletePersonJson = """{"name":"mr updated"}""" val updatedPerson = updateCaseClassFromIncompleteJson(person, incompletePersonJson) println(updatedPerson) /

How to Use Circe for Decoding JSON Lists/Arrays in Scala

牧云@^-^@ 提交于 2019-12-05 10:02:27
I have the code snippet cursor.downField("params").downField("playlist").downField("items").as[List[Clip]] Where Clip is a simple case class of strings and numbers. The incoming Json should contain a json object "playlist" with an array of "items" where each item is a clip. So the json should look like { "playlist": { "name": "Sample Playlist", "items": [ { "clipId":"xyz", "name":"abc" }, { "clipId":"pqr", "name":"def" } ] } } With the code snippet above, I'm getting the compile error: Error:(147, 81) could not find implicit value for parameter d: io.circe.Decoder[List[com.packagename.model

Encoding Scala None to JSON value using circe

送分小仙女□ 提交于 2019-11-30 19:55:21
Suppose I have the following case classes that need to be serialized as JSON objects using circe: @JsonCodec case class A(a1: String, a2: Option[String]) @JsonCodec case class B(b1: Option[A], b2: Option[A], b3: Int) Now I need to encode val b = B(None, Some(A("a", Some("aa")), 5) as JSON but I want to be able to control whether it is output as { "b1": null, "b2": { "a1": "a", "a2": "aa" }, "b3": 5 } or { "b2": { "a1": "a", "a2": "aa" }, "b3": 5 } Using Printer 's dropNullKeys config, e.g. b.asJson.noSpaces.copy(dropNullKeys = true) would result in omitting None s from output whereas setting

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(

Circe instances for encoding/decoding sealed trait instances of arity 0?

旧城冷巷雨未停 提交于 2019-11-30 11:36:24
问题 I'm using sealed traits as enums for exhaustive pattern matching. In cases where I have case objects instead of case classes extending my trait, I'd like to encode and decode (via Circe) as just a plain string. For example: sealed trait State case object On extends State case object Off extends State val a: State = State.Off a.asJson.noSpaces // trying for "Off" decode[State]("On") // should be State.On I understand that this will be configurable in 0.5.0, but can anyone help me write

Scala Circe with generics

回眸只為那壹抹淺笑 提交于 2019-11-30 07:42:46
问题 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)

Encoding Scala None to JSON value using circe

痞子三分冷 提交于 2019-11-30 04:16:03
问题 Suppose I have the following case classes that need to be serialized as JSON objects using circe: @JsonCodec case class A(a1: String, a2: Option[String]) @JsonCodec case class B(b1: Option[A], b2: Option[A], b3: Int) Now I need to encode val b = B(None, Some(A("a", Some("aa")), 5) as JSON but I want to be able to control whether it is output as { "b1": null, "b2": { "a1": "a", "a2": "aa" }, "b3": 5 } or { "b2": { "a1": "a", "a2": "aa" }, "b3": 5 } Using Printer 's dropNullKeys config, e.g. b

Circe instances for encoding/decoding sealed trait instances of arity 0?

坚强是说给别人听的谎言 提交于 2019-11-30 00:34:54
I'm using sealed traits as enums for exhaustive pattern matching. In cases where I have case objects instead of case classes extending my trait, I'd like to encode and decode (via Circe ) as just a plain string. For example: sealed trait State case object On extends State case object Off extends State val a: State = State.Off a.asJson.noSpaces // trying for "Off" decode[State]("On") // should be State.On I understand that this will be configurable in 0.5.0, but can anyone help me write something to tide me over until that's released? To highlight the problem—assuming this ADT: sealed trait