circe

Circe Decode to sealed trait extended by multiple case classes

*爱你&永不变心* 提交于 2021-02-19 06:28:06
问题 I've seen similar questions before, but none of them have worked. I think that they ask something different so I'm asking here. I have something like this in one file: sealed trait Thing case class SomeThing() extends Thing case class OtherThing() extends Thing and in another file: val str = //valid json val decoded = decode[Thing](str) println(decoded) and I get: Left(DecodingFailure(...)) This works if I did: val str = //valid json val decoded = decode[SomeThing](str) println(decoded) 回答1:

How do I ignore decoding failures in a JSON array?

我们两清 提交于 2021-02-18 22:15:30
问题 Suppose I want to decode some values from a JSON array into a case class with circe. The following works just fine: scala> import io.circe.generic.auto._, io.circe.jawn.decode import io.circe.generic.auto._ import io.circe.jawn.decode scala> case class Foo(name: String) defined class Foo scala> val goodDoc = """[{ "name": "abc" }, { "name": "xyz" }]""" goodDoc: String = [{ "name": "abc" }, { "name": "xyz" }] scala> decode[List[Foo]](goodDoc) res0: Either[io.circe.Error,List[Foo]] = Right(List

How to Convert Seq to Json using Circe inside a function - keep getting “implicit value not found” error

前提是你 提交于 2020-06-26 12:11:05
问题 I am learning Circe and Scala for a project at work. To explain my issue, start with the following example: import io.circe.syntax._ object TestDrive extends App { val labels = Seq("Banana", "Banano", "Grapefruit") println(labels.asJson) } Ok so this outputs: ["Banana","Banano","Grapefruit"] This is good. Now I want to make my code a bit more general. I want to write a function that takes in a Sequence, whose elements can be of type AnyVal. Here is my attempt: import io.circe.syntax._ import

How to Convert Seq to Json using Circe inside a function - keep getting “implicit value not found” error

二次信任 提交于 2020-06-26 12:10:30
问题 I am learning Circe and Scala for a project at work. To explain my issue, start with the following example: import io.circe.syntax._ object TestDrive extends App { val labels = Seq("Banana", "Banano", "Grapefruit") println(labels.asJson) } Ok so this outputs: ["Banana","Banano","Grapefruit"] This is good. Now I want to make my code a bit more general. I want to write a function that takes in a Sequence, whose elements can be of type AnyVal. Here is my attempt: import io.circe.syntax._ import

Encode Map[String, MyCaseClass] into Seq[String, String] using circe

随声附和 提交于 2019-12-25 01:53:47
问题 As per the question, how to encode Map[String, MyCaseClass] into Seq[String, String] using circe? Model: case class MyCaseClass(name: String, enabled: Boolean) case class Parent(parentName: String, collection: Map[String, MyCaseClass]) Parent( "parent-name", Map( "external-name-a", MyCaseClass("internal-name-a", true), "external-name-b", MyCaseClass("internal-name-b", false) ) ) I'd like to encode this into a: Seq[name = <map key>, enabled = <boolean value from MyCaseClass>] For example: { ..

Instantiate types from recursive type grammar

我怕爱的太早我们不能终老 提交于 2019-12-24 08:46:45
问题 Given this recursive type grammar: case class Fix[F[_]](out: F[Fix[F]]) type FieldValue = Seq[String] :+: String :+: Int :+: Long :+: CNil type FieldLeaf[F] = FieldValue :+: SubField[F] :+: CNil type SubField[F] = Seq[F] type Field0[F] = (String, FieldLeaf[F]) type Field = Fix[Field0] And instances of Seq[Field] Is it feasible to instantiate concrete classes from the type grammar? I.e: def instantiate[T](fields:Seq[Field]):Option[T] = .... case class Person(id:Long, name:String) val fields