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
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
  • 138,631
  • 12
  • 375
  • 680
5
votes
1 answer

this vs. (this) in Scala

I use the Scala circe library to convert objects of the case class Message to JSON and also to create Message objects from their JSON representation. This class is implemented as follows: import io.circe import…
5
votes
1 answer

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
  • 2,589
  • 3
  • 19
  • 37
5
votes
3 answers

Decode case class, String or Int in circe

I use some Rest API responding with json containing a sort of "mixed" field. By mixed I mean that it can take values of a different type. In my case Object, String and Int are allowed. The Object itself consists of 1 Int and 1 String. The object I…
Some Name
  • 8,555
  • 5
  • 27
  • 77
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
  • 452
  • 3
  • 12
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
  • 14,156
  • 3
  • 52
  • 95
5
votes
2 answers

How to use Circe to do a dynamic decoding?

My question is a little tricky. I have a case class looking like this case class Foo( id: String, name: String, field1: Boolean, field2: Boolean, field3: Boolean, field4: Boolean ) However, I have two types of input, one is…
wtian
  • 178
  • 3
  • 10
5
votes
2 answers

Flattening nested JSON objects with Circe

Suppose I have a JSON object like this: { "foo": true, "bar": { "baz": 1, "qux": { "msg": "hello world", "wow": [null] } } } And I want to flatten it recursively to a single layer, with the keys merged…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
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
  • 899
  • 1
  • 8
  • 22
5
votes
1 answer

Json.asString returns None even though Json.toString returns the correct value

Given the following case class LogMessage: import io.circe.{Decoder, Encoder} import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} import enumeratum.{CirceEnum, Enum, EnumEntry} import io.circe.syntax._ sealed trait LogLevel extends…
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50
5
votes
2 answers

Convert Json to a Map[String, String]

I have input json like {"a": "x", "b": "y", "c": "z", .... } I want to convert this json to a Map like Map[String, String] so basically a map of key value pairs. How can I do this using circe? Note that I don't know what keys "a", "b", "c" will be…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
5
votes
2 answers

Circe Scala - Encode & Decode Map[] and case classes

I'm trying to create an encoder and decoder for a case class I have: case class Road(id: String, light: RoadLight, names: Map[String, String]) RoadLight is a java class, with enum. public enum RoadLight { red,yellow,green } I have tried to do a…
Doe
  • 71
  • 1
  • 4
5
votes
3 answers

Circe Decode to sealed trait extended by multiple case classes

I've seen similar questions before, but none of them have worked. I think that they ask something different so I'm asking here. I have something like this in one file: sealed trait Thing case class SomeThing() extends Thing case class OtherThing()…
Kevin Fisher
  • 154
  • 2
  • 8
5
votes
1 answer

scala circe encoders/decoders for an abstract class with case classes

I want to save a collection of FieldMapping classes as a json string - abstract class Field { def clazz: Class[_] def name: String } case class StringField(name: String) extends Field { override def clazz: Class[_] = classOf[String] } case…
Alex
  • 2,589
  • 3
  • 35
  • 44
5
votes
1 answer

Using Akka Http and Circe for decoding JSON in Scala

I am trying to create Akka Http REST post endpoint mapping the JSON objects to the case class defined import io.circe.Decoder, io.circe.generic.auto._ case class JobEntity(id: Option[Long] = None, name: String, description: String, json_data…
Vishal
  • 1,442
  • 3
  • 29
  • 48