Questions tagged [circe]

Circe is a JSON library for Scala (and Scala.js).

Circe is a JSON library for Scala powered by Cats.

A simple usage of this library is:

import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._

sealed trait Foo
case class Bar(xs: Vector[String]) extends Foo
case class Qux(i: Int, d: Option[Double]) extends Foo

val foo: Foo = Qux(13, Some(14.0))

val json = foo.asJson.noSpaces
println(json) // prints: {"Qux":{"i":13,"d":14}}

val decodedFoo = decode[Foo](json)
println(decodedFoo) // prints: Right(Qux(13,Some(14)))

For reading about it please refer:

396 questions
7
votes
2 answers

Strange NPE with io.circe.Decoder

I have 2 variables declared as follows, implicit val decodeURL: Decoder[URL] = Decoder.decodeString.emapTry(s => Try(new URL(s))) // #1 implicit val decodeCompleted = Decoder[List[URL]].prepare(_.downField("completed")) // #2 Both lines…
thlim
  • 2,908
  • 3
  • 34
  • 57
7
votes
2 answers

Adding field to a JSON using Circe

I am going through the Circe documentation and can't figure out how to handle the following. I would simply like to add a field with an object inside the main JSON object. { Fieldalreadythere: {} "Newfield" : {} } I just want to add the…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
7
votes
1 answer

Encoding ADT case classes with a discriminator, even when typed as the case class

Suppose I have a ADT in Scala: sealed trait Base case class Foo(i: Int) extends Base case class Baz(x: String) extends Base I want to encode values of this type into the JSON that looks like the following: { "Foo": { "i": 10000 }} { "Baz": { "x":…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
7
votes
1 answer

Use circe to preprocess dot-notation style fields

I have some json that includes some fields that are being flattened into a bson-ish format as in {"foo.bar" : "bash"}. I'd like to transform this to the following representation {"foo" : { "bar" : "bash"}} and wondering where in circe I'd do such an…
7
votes
1 answer

Circe Encoders and Decoders with Http4s

I am trying to use http4s, circe and http4s-circe. Below I am trying to use the auto derivation feature of circe. import org.http4s.client.blaze.SimpleHttp1Client import org.http4s.Status.ResponseClass.Successful import io.circe.syntax._ import…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
7
votes
5 answers

Rename JSON fields with circe

I want to have different names of fields in my case classes and in my JSON, therefore I need a comfortable way of renaming in both, encoding and decoding. Does someone have a good solution ?
tobi
  • 329
  • 4
  • 11
7
votes
1 answer

Merge several json arrays in circe

Let's say we have 2 json arrays. How to merge them into a single array with circe? Example: Array 1: [{"id": 1}, {"id": 2}, {"id": 3}] Array 2: [{"id": 4}, {"id": 5}, {"id": 6}] Needed: [{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5},…
Haspemulator
  • 11,050
  • 9
  • 49
  • 76
6
votes
1 answer

Circe deriveDecoder/deriveEncoder could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder

I am trying to generate encoders and decoders for two case classes: object EventBusCases { case class ValuationRequest(function: RequestValue = ALL_DAY_VALS, interval: RequestValue = IntraDayIntervals.MIN_5) implicit val requestDecoder:…
LeYAUable
  • 1,613
  • 2
  • 15
  • 30
6
votes
2 answers

How to decode a JSON null into an empty collection

Suppose I have a Scala case class like this: case class Stuff(id: String, values: List[String]) And I want to be able to decode the following JSON values into it: { "id": "foo", "values": ["a", "b", "c"] } { "id": "bar", "values": [] } { "id":…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
6
votes
1 answer

Parsing primitive types in Circe

I'm having an issue with json parsing when field can have different primitive value types. For example, I can get json: { "name" : "john", "age" : 31 } Or it can be in this form: { "name" : "john", "age" : "thirty one" } Or in this way: { …
oybek
  • 630
  • 4
  • 14
6
votes
0 answers

Is there a way to use circe-optics' JsonPath with strings, just as in jq CLI tool?

What I'd like to do, is having field descriptor defined as field1.field2[1].field3, access value two of json: { "field1": { "field2": [ { "field3": "one" }, { "field3": "two" } ] } } I know I can…
Bartek Andrzejczak
  • 1,292
  • 2
  • 14
  • 27
6
votes
1 answer

how to decode json in scala with circe by ignoring fieldname case sensitivity

I have the following failing test case: case class ActionRequest(action: String, `type`:String, key: String) "Actions " should " be decoded correctly" in { val actionJson = """[ |{"Action":"view","Type":"Product","Key":"1210"}, …
6
votes
1 answer

Generic derivation of AnyVal types with Circe

I want to derive Encoder instances for value classes. With the semiauto mechanism I am not able to derive nested classes. Image the following case class structure { case class Recipient(email: Recipient.Email, name: Recipient.Name) object…
Muki
  • 3,513
  • 3
  • 27
  • 50
6
votes
2 answers

Scala Circe with generics

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…
RichyHBM
  • 798
  • 1
  • 7
  • 18
6
votes
0 answers

Is it possible to automatically derive a sealed trait family/ADT?

I have a method that is able to persist any type, as long as that type has a io.circe.Encoder[A] instance, something like this: def persist[A](a: A)(implicit ea: Encoder[A]): Boolean Now while testing this, I can create any old case class, or set…
Noel M
  • 15,812
  • 8
  • 39
  • 47
1 2
3
26 27