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
9
votes
1 answer

Generic derivation for ADTs in Scala with a custom representation

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:…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
9
votes
1 answer

Is there a way to have optional fields in a Circe decoder?

I have a case class on which every field is optional, like: case class Foo(name: Option[String], phone: Option[String], email: Option[String]) I was trying to create a manual Decoder for my case class and found that a…
Camilo Sampedro
  • 1,306
  • 1
  • 19
  • 32
9
votes
1 answer

Circe encoder/decoder for subclasses types

Given the following ADT sealed abstract class GroupRepository(val `type`: String) { def name: String def repositories: Seq[String] def blobstore: String } case class DockerGroup(name: String, repositories: Seq[String], blobstore: String =…
Simão Martins
  • 1,210
  • 11
  • 22
8
votes
1 answer

Print json string in one line using circe in scala

I want to print json to string in one line. case class Data(e: Option[String]) object Data { implicit val dEncoder = deriveDecoder[Data] implicit val dDecoder = deriveEncoder[Data] } case class Random(a: String,b:…
Avenger
  • 793
  • 11
  • 31
8
votes
3 answers

How to remove null values in list of objects using Circe

I'm trying to encode a list of objects using Circe, something that looks similar to: val test = Seq(MyObject("hello", None, 1, 2, None) I'm trying to parse this using Circe: test.asJson But this creates the JSON object: [ { name: "hello", …
Jan Swart
  • 6,761
  • 10
  • 36
  • 45
8
votes
1 answer

Decoding JSON values in circe where the key is not known at compile time

Suppose I've been working with some JSON like this: { "id": 123, "name": "aubergine" } By decoding it into a Scala case class like this: case class Item(id: Long, name: String) This works just fine with circe's generic derivation: scala> import…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
8
votes
2 answers

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
  • 630
  • 4
  • 14
8
votes
1 answer

How can I configure Circe to stop using nested class names as key names in encoded JSON?

I'm trying to encode a case class (where some properties are also case classes), and I'm getting the nested case class name as the key name in the JSON. Is there a simple way to avoid that without creating a custom encoder? The nested classes…
Steven Bakhtiari
  • 3,227
  • 2
  • 20
  • 24
8
votes
1 answer

How do I ignore decoding failures in a JSON array?

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…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
8
votes
1 answer

Decoding Case Class w/ Tagged Type

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 @…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
8
votes
3 answers

Ignore None field while Encoding to json with Circe for Scala

I am using scala 2.11.8 with circe 0.7.0 I am using scalajs to communicate with an API differentiating non existant field and null field in the sent JSON. I am looking for a way of encoding to JSON a scala case class containing Option[T] fields that…
amougel
  • 319
  • 5
  • 14
8
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
  • 515
  • 1
  • 4
  • 16
8
votes
3 answers

Transform Json with circe

Assuming the following json payload val json = """{ "choices" : [ { "name" : "A" }, { "name" : "B" }, { "name" : "C" }, { "name" : "D" } ], "domain" : "Quizz", "level" : "Test", …
Jean
  • 21,329
  • 5
  • 46
  • 64
8
votes
1 answer

Deriving circe Codec for a sealed case class family where base trait has a (sealed) type member

I can easily generically derive a codec for a sealed case class family like this: import io.circe._ import io.circe.generic.auto._ sealed trait Base case class X(x: Int) extends Base case class Y(y: Int) extends Base object Test extends App { …
Giovanni Caporaletti
  • 5,426
  • 2
  • 26
  • 39
7
votes
3 answers

(De)serialize enum as string in Scala 3

I am trying to find a simple and efficient way to (de)serialize enums in Scala 3 using circe. Consider the following example: import io.circe.generic.auto._ import io.circe.syntax._ enum OrderType: case BUY case SELL case class Order(id: Int,…
Amit Singh
  • 2,875
  • 14
  • 30
1
2
3
26 27