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
31
votes
2 answers

How to decode an ADT with circe without disambiguating objects

Suppose I've got an ADT like this: sealed trait Event case class Foo(i: Int) extends Event case class Bar(s: String) extends Event case class Baz(c: Char) extends Event case class Qux(values: List[String]) extends Event The default generic…
Travis Brown
  • 135,682
  • 12
  • 352
  • 654
16
votes
1 answer

Decoding structured JSON arrays with circe in Scala

Suppose I need to decode JSON arrays that look like the following, where there are a couple of fields at the beginning, some arbitrary number of homogeneous elements, and then some other field: [ "Foo", "McBar", true, false, false, false, true, 137…
Travis Brown
  • 135,682
  • 12
  • 352
  • 654
15
votes
1 answer

My coproduct encoding is ambiguous

This question has come up a few times recently, so I'm FAQ-ing it here. Suppose I've got some case classes like this: import io.circe._, io.circe.generic.semiauto._ object model { case class A(a: String) case class B(a: String, i: Int) case…
Travis Brown
  • 135,682
  • 12
  • 352
  • 654
15
votes
4 answers

Transform all keys from `underscore` to `camel case` of json objects in circe

Origin { "first_name" : "foo", "last_name" : "bar", "parent" : { "first_name" : "baz", "last_name" : "bazz", } } Expected { "firstName" : "foo", "lastName" : "bar", "parent" : { "firstName" : "baz", …
jilen
  • 5,190
  • 1
  • 29
  • 74
15
votes
1 answer

Circe instances for encoding/decoding sealed trait instances of arity 0?

I'm using sealed traits as enums for exhaustive pattern matching. In cases where I have case objects instead of case classes extending my trait, I'd like to encode and decode (via Circe) as just a plain string. For example: sealed trait…
Andrew Roberts
  • 690
  • 7
  • 12
14
votes
1 answer

How to encode/decode Timestamp for json in circe?

While using circe in slick to get data in json,I could fetch data having no date(Timestamp/DateTime) fields in Entities. But when I use Timestamp fields in Entities, the error is thrown: [error]…
Sujit Baniya
  • 775
  • 5
  • 22
12
votes
1 answer

How to convert sealed trait case objects to string using circe

I am using Scala and Circe. I have the following sealed trait. sealed trait Mode case object Authentication extends Mode case object Ocr extends Mode The output of this case object when called SessionModel.Authentication is the…
Kay
  • 11,044
  • 31
  • 100
  • 173
11
votes
1 answer

Capturing unused fields while decoding a JSON object with circe

Suppose I have a case class like the following, and I want to decode a JSON object into it, with all of the fields that haven't been used ending up in a special member for the leftovers: import io.circe.Json case class Foo(a: Int, b: String,…
Travis Brown
  • 135,682
  • 12
  • 352
  • 654
11
votes
1 answer

Circe - Use default fields in case class when decoding/encoding json

Lets say I have this case class: case class Foo(bar: String, baz: Boolean = false) which is used in when decoding/encoding API requests/responses using akka-http-json in an example similar to this: import akka.actor.ActorSystem import…
simen-andresen
  • 1,739
  • 3
  • 18
  • 35
11
votes
2 answers

Encoding Scala None to JSON value using circe

Suppose I have the following case classes that need to be serialized as JSON objects using circe: @JsonCodec case class A(a1: String, a2: Option[String]) @JsonCodec case class B(b1: Option[A], b2: Option[A], b3: Int) Now I need to encode val b =…
msilb
  • 415
  • 4
  • 14
11
votes
3 answers

Circe and Scala's Enumeration type

I'm trying to wrap my head around Circe. So, here's the model I've been given: object Gender extends Enumeration { type Gender = Value val Male, Female, Unisex, Unknown = Value } case class Product(id: String, gender: Gender.Value) I…
simou
  • 2,277
  • 3
  • 22
  • 37
11
votes
1 answer

With Circe Json why is implicit resolution slower at runtime

Why is Circe Json slower with implicit decoder lookup compared to saving the implicit decoder to a val. I would expect these to be the same because implicit resolution is done at runtime. import io.circe._ import io.circe.generic.auto._ import…
Stephen
  • 3,445
  • 4
  • 22
  • 37
11
votes
2 answers

Update case class from incomplete JSON with Argonaut or Circe

I need to create an updated instance from a case class instance (with any needed DecodeJsons implicitly derived), given an incomplete json (some fields missing). How can this be accomplished with Argonaut (preferably) or Circe (if I have…
eirirlar
  • 795
  • 5
  • 23
10
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
  • 135,682
  • 12
  • 352
  • 654
9
votes
1 answer

Decoding Shapeless Tagged Types

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 @ trait Foo…
Kevin Meredith
  • 38,251
  • 58
  • 190
  • 340
1
2 3
21 22