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

How to derive a decoder semiautomatically for a list of some type with Circe?

I have an implicit class that decodes server's response into a JSON and latter in the right case class to avoid repeating calls to .as and .getOrElse all around the tests: implicit class RouteTestResultBody(testResult: RouteTestResult) { def body:…
Tae
  • 1,665
  • 5
  • 24
  • 45
2
votes
2 answers

Scala & Circe: JSON encoding with optional fields

I am trying to use Circe to do some JSON encoding as follows: import io.circe.{Encoder, Json} import io.circe.syntax._ case class Person(name: String, nickname: Option[String] = None) object EncodingTest extends App { val persons = List…
Stratos K
  • 341
  • 2
  • 14
2
votes
1 answer

When decoding a message with Circe, is it possible to extract the invalid value from a DecodingFailure

When attempting to decode a json message with circe, I have a requirement to return the original value that caused the decoding failure, if possible. As an example, I receive some invalidJson that contains an invalidUuid. Having access to both the…
2
votes
1 answer

StackOverflowError on encoding to JSON with circe

I am writing tests for my case classes and in the following test I have a StackOverflowError: test("ValuationRequest - Conversion between case object and Json works") { val caseObject = ValuationRequest(TIME_SERIES_INTRADAY, "",…
LeYAUable
  • 1,613
  • 2
  • 15
  • 30
2
votes
1 answer

Scala, Circe, Http4s - is it any way to encode Throwable in Circe?

I have created hierarchy of errors: sealed trait MyError extends Throwable final case class SecondError(msg: String) extends MyError Now I could get this kind of error in my http4s routes: case GET -> Root / "things" => for { response <-…
Developus
  • 1,400
  • 2
  • 14
  • 50
2
votes
0 answers

Parsing JSON by json path using circe lib for scala

I am new in scala developing and maybe it's repeated question here, trying to parse json by circe lib, I know how to parse json to get specific value from it but I want to it dynamically by giving json path for any json. For example we have this…
Baktiyar Bekbergen
  • 374
  • 2
  • 4
  • 24
2
votes
0 answers

How can I Throw error inside scodec Attempt?

I have the following code to decode a BitVector. How can I remove the exception and throw a failure? def decode(b: BitVector) = { if (!applies) { Attempt.successful(DecodeResult.apply(null, b)) } else { string.decode(b) match { …
mat656
  • 129
  • 1
  • 7
2
votes
0 answers

Decode a nested array with circe-optics

I have JSON like this: "data": { "project": { "activityChildren": [ { "id": 2, "parents": [ { "id": 1 } ] }, ] } } I'd like to decode this…
user355252
2
votes
1 answer

Not able to generate Circe Decoder when using with Cats and Refined Types

I wrote this code import io.circe._ import io.circe.refined._ import cats.data._ import cats.implicits._ import eu.timepit.refined.auto._ final case class Translation(lang: LanguageCode, name: ProductName) final case class Product(id: ProductId,…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
2
votes
1 answer

Encoding all subtypes with prepopulated field with Circe

I'd like to be able to add a field to certain case classes when they are encoded in JSON vis circe. e.g. trait OntologyType { val ontologyType: String = this.getClass.getSimpleName } case class Thing(i: Int) extends OntologyType val thing =…
James Gorrie
  • 351
  • 2
  • 6
2
votes
1 answer

ADT Json serialization/deserialization

Experimenting with Circe to serialize and deserialize Algebraic Data Types in Scala I tried out the following sample, following the doc and some samples on the web: sealed trait StringData case class Data2(f1: String, f2: String) extends…
Daniel
  • 1,522
  • 1
  • 12
  • 25
2
votes
1 answer

Using Circe optics to modify all fields of an object, or all items of an array

I am trying to modify a nested JSON structure using Circe's optics. However, all the examples are only modifying a single field within the object with the known name. What I need to do: Assuming foo key of my object contains an array of objects,…
Koterpillar
  • 7,883
  • 2
  • 25
  • 41
2
votes
0 answers

How to output numbers without type tag with circe-yaml?

Following code using circe-yaml produces the output below. While the output is correct, it is not very nice. In my case the output is expected to be read and further edited by humans, therefore I would like to have it as natural as possible. How can…
Suma
  • 33,181
  • 16
  • 123
  • 191
2
votes
1 answer

How to parse nested Json Arrays with Circe Optics

I read the example given by the Circe docs using Circe Optics. The example in the docs is pretty straight forward because the path to the node is pretty easy to find. In my case the json looks like import io.circe._, io.circe.parser._ val json =…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
2
votes
1 answer

Cannot created object walking though JSON string using Circe JSON parser

So i am trying to create a custom decoder for a JSON string to be converted into a domain object. I am using Scala/Circe to walk through the JSON and create the object. I am unable to get this to run. I am sure i am not clear on how to walk through…
Som Bhattacharyya
  • 3,972
  • 35
  • 54