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:

328 questions
6
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
  • 26,151
  • 46
  • 158
  • 314
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,261
  • 3
  • 24
  • 47
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
  • 14,881
  • 7
  • 35
  • 45
6
votes
1 answer

Transforming JSON with state in circe

Note: I'm copying this question over from the circe Gitter channel for the sake of posterity. Suppose we want to translate this JSON document: { "places": [{ "id": "dadcc0d9-0615-4e46-9df4-2619f49930a0" }, { "id":…
Travis Brown
  • 135,682
  • 12
  • 352
  • 654
5
votes
2 answers

Handling PATCH requests with Akka HTTP and circe for nullable fields

Is there a common approach to handle PATCH requests in REST API using circe library? By default, circe does not allow decoding partial JSON with only a part of the fields specified, i.e. it requires all fields to be set. You could use a withDefaults…
Alexey Sirenko
  • 402
  • 3
  • 11
5
votes
2 answers

Delegate to a more specific Context Bound (additional implicit parameter)

I am trying to create an example of a ZIO Module, that has two implementations: Using YAML with circe-yaml Using HOCON with pureConfig My general Interface looks like this: trait Service[R] { def load[T <: Component](ref: CompRef): RIO[R,…
pme
  • 11,442
  • 2
  • 35
  • 62
5
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
  • 135,682
  • 12
  • 352
  • 654
5
votes
1 answer

Circe parse json from snake case keys

I have the following case class: final case class Camel(firstName: String, lastName: String, waterPerDay: Int) and circe configuration: object CirceImplicits { import io.circe.syntax._ import io.circe.generic.semiauto._ import…
oybek
  • 605
  • 3
  • 13
5
votes
2 answers

Recursive traverse JSON with circe-optics

I have a json with complex structure. Something like this: { "a":"aa", "b":"bb", "c":[ "aaa", "bbb" ], "d":{ "e":"ee", "f":"ff" } } And I want to uppercase all string values. The Documentation…
Oleg
  • 705
  • 6
  • 19
5
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,152
  • 2
  • 11
  • 25
5
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"}, …
5
votes
1 answer

Circe cannot find implicit encoder

I am trying to encode a few classes into json strings, however no matter what I try, my classes do not seem to be able to find an implicit encoder for the case classes I am using. Here's the smallest example I was able to pare it down to. import…
Davis Broda
  • 4,021
  • 5
  • 20
  • 36
5
votes
3 answers

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

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"…
mattmar10
  • 357
  • 1
  • 3
  • 10
5
votes
1 answer

Incompatible Jackson version: 2.7.1 in sbt?

I'm getting this error when running TwitterServer from sbt: SEVERE: LoadService: failed to instantiate 'com.twitter.finagle.stats.MetricsExporter' for the requested service…
mikebridge
  • 3,207
  • 2
  • 31
  • 40
4
votes
0 answers

Http4s Client Encode Entity as x-www-form-urlencoded Recursively

I have a request like the following val request = Request[IO]( method = POST, uri = Uri.uri("..."), headers = Headers( Authorization(BasicCredentials("...", "...")) ) ) …
sinanspd
  • 1,989
  • 2
  • 14
  • 28
1 2
3
21 22