circe

Decoding Json with Circe when fields are incomplete

僤鯓⒐⒋嵵緔 提交于 2019-12-24 01:29:11
问题 I have a transcript in json format with a bunch of words in it { "words": [{ "duration": 123, "name": "world" "time": 234, "speaker": null }] } I have been using Circe to encode/decode Json. In this particular case: import io.circe.generic.auto._ import io.circe.parser._ val decoded = decode[Transcript](transcriptJson) And my ADT look like: case class Word( duration: Double, name: String, time: Float, para: String, speaker: Option[String], key: Option[String] = None, strike: Option[String] =

Avoid serialize to null using Circe json serializer

不羁岁月 提交于 2019-12-24 00:10:03
问题 How to avoid serializing None to null using Circe json serializer? I am not able to force this library to skip serializing fields which are None. Is it possible to achieve? 回答1: I found the solution. We can provide implicit variable of instance Printer with our configuration. https://github.com/circe/circe/blob/master/modules/core/shared/src/main/scala/io/circe/Printer.scala We can set dropNullKeys to null and everything is working like a charm. 来源: https://stackoverflow.com/questions

Decoding Case Class w/ Tagged Type

假装没事ソ 提交于 2019-12-23 09:30:01
问题 Given: Given the following on Ammonite: @ import $ivy.`io.circe::circe-core:0.9.0` @ import $ivy.`io.circe::circe-generic:0.9.0` @ import $ivy.`com.chuusai::shapeless:2.3.3` @ import shapeless.tag import shapeless.tag @ trait Foo defined trait Foo @ import io.circe._, io.circe.generic.semiauto._ import io.circe._, io.circe.generic.semiauto._ @ import shapeless.tag.@@ import shapeless.tag.@@ @ implicit def taggedTypeDecoder[A, B](implicit ev: Decoder[A]): Decoder[A @@ B] = ev.map(tag[B][A](_))

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"] }

Modifying a JSON array in Scala with circe

微笑、不失礼 提交于 2019-12-13 09:39:09
问题 I have a JSON string with array as the following: { "cars": { "Nissan": [ {"model":"Sentra", "doors":4}, {"model":"Maxima", "doors":4} ], "Ford": [ {"model":"Taurus", "doors":4}, {"model":"Escort", "doors":2} ] } } I would like to edit a new card brand, using circe at scala. Instead of "Nissan": [ {"model":"Sentra", "doors":4}, {"model":"Maxima", "doors":4}, ] I would like to have as a result: "Nissan": [ {"model":"Sentra", "doors":1000}, ], Thanks. 回答1: Made after reading the manual, please

How to get Circe to skip certain values from the enumeration?

前提是你 提交于 2019-12-13 01:34:50
问题 We have an "enumeration" like so: sealed abstract class StatusEnumeration(val value: String) case object Status { case object Mine extends StatusEnumeration("mine") case object Someone extends StatusEnumeration("someone") case object Neighbor extends StatusEnumeration("neighbor") case object Unknown extends StatusEnumeration("unknown") } This structure is dictated by our domain/needs. However, I'd like to convert this StatusEnumeration into an output JSON like so: case class Root(status:

Generic json decoder trait with Circe implementation [duplicate]

孤街醉人 提交于 2019-12-13 00:16:29
问题 This question already has answers here : Scala Circe with generics (2 answers) Closed 3 years ago . I have a trait used to inject json decoder as dependency to components of my project: trait JsonDecoder { def apply[T](s: String): Option[T] } When I try to implement it with Circe: import io.circe.generic.auto._ import io.circe.parser.decode case class CirceJsonDecoder() extends JsonDecoder { def apply[T](s: String): Option[T] = { decode[T](s).fold(_ => None, s => Some(s)) } } and run: case

Adding field to a json using Circe

流过昼夜 提交于 2019-12-12 14:08:11
问题 I am going trough the circe documentation and can't figure out how to handle the following. I would simply like to add a field with an object in said the main jason object. { Fieldalreadythere: {} "Newfield" : {} } I just want to add the newfield in the object. To give a bit of context i am dealing with Json-ld. I just want to add a context object. @context: {} See example bellow: { "@context": { "@version": 1.1, "xsd": "http://www.w3.org/2001/XMLSchema#", "foaf": "http://xmlns.com/foaf/0.1/"

How to skip over empty arrays in the generated Json with Circe?

纵饮孤独 提交于 2019-12-11 12:43:58
问题 //Domain case class Item(price: Int) case class Data(name: String, items: Vector[Item]) Data("stackoverflow", Vector(Item(100))).asJson //ouput: { "name": "stackoverflow", "items": [ { "price": 100 } ] } // With Empty items: Data("stackoverflow", Vector()).asJson // expected output: { "name": "stackoverflow", "items": null // Can be removed with Printer.noSpaces.copy(dropNullValues = true) } I tried doing something like: implicit val itemsEncoder: Encoder[Vector[Item]] = (items: Vector[Item])

Circe decoder for scalaz.Maybe

◇◆丶佛笑我妖孽 提交于 2019-12-11 00:30:56
问题 Here's a simple finch server, using circe as decoder: import com.twitter.finagle.http.RequestBuilder import com.twitter.io.Buf import io.circe.generic.auto._ import io.finch._ import io.finch.circe._ case class Test(myValue: Int) val api = post("foo" :: body.as[Test]) { test: Test => Ok(test) } val bodyPost = RequestBuilder() .url("http://localhost:8080/foo") .buildPost(Buf.Utf8("""{ "myValue" : 42 }""")) api.toService.apply(bodyPost).onSuccess { response => println(s"$response: ${response