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

Generic json decoder trait with Circe implementation

I have a trait used to inject json decoder as dependency to components of my project: trait JsonDecoder { def apply[T](s: String): Option[T] } When I try to implement it with Circe: import io.circe.generic.auto._ import…
mixel
  • 25,177
  • 13
  • 126
  • 165
2
votes
1 answer

How to parse a JSON Scala without case classes

I have a JSON that can change through time and using case Class might be unconvenient because I need to change the structure of it everytime the JSON change. for example, if I have a JSON like this: val json= """{ "accounts": [ { "emailAccount":…
salvob
  • 1,300
  • 3
  • 21
  • 41
1
vote
1 answer

Can shapeless derive a "shallow" generic for ADT coproduct?

I'm trying to come up with a parser framework for ADT hierarchy. I want it to automatically derive a parser defined at either "leaf" (case class) level or "node" (intermediate sealed trait) level. Usage would look like: sealed trait Chrum case…
xko
  • 111
  • 2
1
vote
2 answers

Parse \n separated json with circe

do you guys know how to parse an array of json objects but without square brackets { "jsonValue1": "val1", "jsonValue2": [1, 1, 2, 2]} { "jsonValue1": "val1", "jsonValue2": [1, 1, 2, 2]} { "jsonValue1": "val1", "jsonValue2": [1, 1, 2, 2]} to a…
1
vote
0 answers

Scala, ZIO, Sttp, Circe - how to map error response to custom trait in sttp client using circe?

I have a simple sttp client implementation: basicRequest.auth .get(...) .response(asJsonEither[GeneralTrait, MyResponse]) .send(backend) .map(_.body.left.map { case HttpError(body, statusCode) =>…
Developus
  • 1,400
  • 2
  • 14
  • 50
1
vote
1 answer

Redoc documentation for tapir endpoint with sealed heirarchy not rendering as expected

I'm trying to define a tapir endpoint, which will accept two potential different payloads (in the snippet below, two different ways of defining a Thing). I'm broadly following the instructions here: https://circe.github.io/circe/codecs/adt.html, and…
user3468054
  • 610
  • 4
  • 11
1
vote
1 answer

Remove Case Class Definition in asJson output from Circe

Consider: import io.circe.generic.auto._, io.circe.syntax._ sealed trait Data case class Failed(foo: String, bar: String) extends Data case class Success(foo1:String, bar1:String) extends Data case class Task(Foo:String, Data: Data) val something =…
1
vote
1 answer

Encoding Decoding a field with any datatype assigned with value None in Scala

I am writing to write encoder/decoder for Any. So suppose my class is case class Demo( field1: Any ) and then I try to encoder it via val myDemo=Demo(field1=None) print(myDemo.asJson+"\n") I have definded encoder decoder as implicit val…
Freez
  • 53
  • 9
1
vote
1 answer

Circe decoder only array size

I have an Api that I need to do multiple concurrent call with cursor, but I'm interested in only knowing the size of an array in the Json only, I wonder if is it possible to make a custom decoder with Circe or Jsoniter for it? The Json look simple…
iron_bat
  • 21
  • 2
1
vote
1 answer

Missing implicits when attempting to derive encoder/decoder for generic sealed trait

I am having problems with the following: sealed trait Expression[T] { def doSomething: Either[String, T] } case class Literal(s: String) extends Expression[String] { def soSomething = Right(s) } object Expression{ implicit def encoder[T:…
Terry Dactyl
  • 1,839
  • 12
  • 21
1
vote
0 answers

Circe: decoding Json field with alias, how to do it?

Assuming that I have a Scala case class that contains a field "someRandomName" that has an alias "someArbitraryName": case class NN( name1: Option[String], @alias("someArbitraryName") someRandomName: String // ... 100 more optional fields ) I…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
1
vote
1 answer

Issue converting decimal (Double) value from JSON to case class

I am using Scala 2.12 with circe version 0.14.1. I am converting a JSON into corresponding case class as below: case class Document(curr: String, value: Double) val json = s"""{"Document":{"curr":"USD","value":40000000.01}}""" import…
NKM
  • 602
  • 1
  • 6
  • 18
1
vote
2 answers

How to use circe with generic case class that extends a sealed trait

I have this minimal example, I want to create encoders/decoders with circe semi-automatic derivation for the generic case class A[T] import io.circe.{Decoder, Encoder} import io.circe.generic.semiauto._ import io.circe.syntax._ sealed trait…
emkay64
  • 121
  • 1
  • 6
1
vote
1 answer

How to Decode a Generic Case Class with semiautomatic in Circe in Scala 3

The following code works with Scala 2.13 (see https://stackoverflow.com/a/59996748/2750966): import io.circe.generic.semiauto._ case class Name(name: String) case class QueryResult[T: Decoder](data: T) implicit val nameDer =…
pme
  • 14,156
  • 3
  • 52
  • 95
1
vote
0 answers

circe-json and scala neted objects processing

I have to create a json in the below format using scala and circe-json encoder classes { "actions":{ "action1":{ "read":[ { "parq":[ { "str":"" }, { …
narman12
  • 43
  • 8