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
9
votes
0 answers

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
  • 909
  • 7
  • 16
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
  • 135,682
  • 12
  • 352
  • 654
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,048
  • 2
  • 17
  • 23
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
  • 135,682
  • 12
  • 352
  • 654
8
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
  • 958
  • 12
  • 27
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
  • 38,251
  • 58
  • 190
  • 340
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
  • 20,591
  • 5
  • 43
  • 61
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
  • 4,996
  • 2
  • 24
  • 37
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,676
  • 3
  • 30
  • 50
7
votes
1 answer

Circe encoder for generic case class with default parameters

I am looking to provide JSON encoders for the following case class: import io.circe.generic.extras.Configuration final case class Hello[T]( source: String, version: Int = 1, data: T ) object Hello { implicit val configuration:…
Rich Ashworth
  • 1,837
  • 3
  • 18
  • 27
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
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
  • 189
  • 2
  • 14
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
  • 605
  • 3
  • 13
6
votes
2 answers

Adding field to a json using Circe

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 JSON object. { Fieldalreadythere: {} "Newfield" : {} } I just want to add the…
MaatDeamon
  • 7,821
  • 5
  • 46
  • 96
6
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
  • 135,682
  • 12
  • 352
  • 654
1
2
3
21 22