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

How to use Circe Optics and Decode together

I am using circe optics like this import io.circe.parser._ import io.circe.optics._ import io.circe.optics.JsonPath._ val json = parse("""{"response": {"person": {"firstname": "foo", "lastname":"bar"}}}""").right.get Now I want to extract the…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
3
votes
1 answer

Json-circe can't derive encoder for subtype of sealed trait

Why do I get error could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[A$A6.this.Bar] in the following code: import io.circe.{Decoder, DecodingFailure, Encoder, HCursor, Json, ObjectEncoder} import…
Nikita
  • 4,435
  • 3
  • 24
  • 44
3
votes
2 answers

How do I override default codec in circe?

I'd like to encode Array[Byte] fields of my case classes as Base64 strings. For some reason Circe doesn't pick up my codec using default one instead that converts byte array into json array of ints. What should I do to fix it ? Here is my minimized…
expert
  • 29,290
  • 30
  • 110
  • 214
3
votes
1 answer

Map null values to None in Scala using Circe

In continuation of this question, I have the following code but would like to map null JSON values to Scala None. Current behavior I am getting is that there's no difference between including a key with null ("key": null) and not including it. I…
FrozenHG
  • 45
  • 4
3
votes
1 answer

diverging implicit expansion for type io.circe.Encoder[scala.collection.immutable.Map[Int,Any]]

I wrote this code import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._ Map(1 -> 1, 2 -> "a").asJson.toString but I get the following error cmd35.sc:1: diverging implicit expansion for type…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
3
votes
2 answers

Parse json list to two list types by field value in Scala circe

I got given (example) json: { "version": 1.1, "author": "XYZ", "elements": [{ "type": "nodeX", "id": 1, "a": 1, "b": 2 }, { "type": "nodeX", "id":…
BoyFarmer
  • 101
  • 4
3
votes
0 answers

How to create encoder/decoder that depends on fields in message for circe

The use case Im trying to get working with using Circle is as follows. Given a stream of JSON messages I want to match on the op and convert the message from and to the appropriate type. The code below shows all the details. However the code des not…
user3139545
  • 6,882
  • 13
  • 44
  • 87
3
votes
2 answers

Use Circe to decode Normal Class (not case class)

I have written this code to read and write josn using circe import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._ case class Foo(i: Int) val f = Foo(10) val json = f.asJson.toString val t1 = decode[Foo](json) this works…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
3
votes
2 answers

How to modify value type of a json via Circe

I need capture int values of a json and change it to a string value by a certain mapping table. I use circe, i had known how to modify a value without changing value type, for example: package com.accenture.soh.driver import io.circe._,…
Laurence Geng
  • 424
  • 3
  • 9
3
votes
0 answers

Failure(java.lang.IllegalArgumentException: Illegal base64 when decoding jwt token with jwtCirce

I am currently trying to build an oAuth 2 authentication inside some akka-http service. I want to use jwt-scala with jwtCirce for decoding a web token. When trying it out in the console (as well as inside my tests) I get the error…
Stoecki
  • 585
  • 1
  • 3
  • 16
3
votes
1 answer

Avoid serialize to null using Circe json serializer

How to avoid serializing None to null using Circe json serializer? I am not able to force this library to skip serializing fields which are None. Is it possible to achieve?
PawelN
  • 848
  • 7
  • 17
3
votes
2 answers

How to encode Json when HList parameter is HNil?

Having this example, import io.circe.generic.auto._ import io.circe.shapes._ import io.circe.parser._ import io.circe.syntax._ import shapeless._ case class A[T <: HList](name: String, params: T) when I instance this case class with a non-empty…
3
votes
1 answer

Migrating for Json4S to Circe

I have the following code written in json4s which compiles and works fine import org.json4s._ def jsonRead[T <: AnyRef](input: String)(implicit m: Manifest[T]): T = { Try(read[T](input)).get } def jsonWrite[T <: AnyRef](input: T)(implicit m:…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
3
votes
1 answer

Parse a case class containing an HList into a JSON string, using Circe

I'm doing a thing in Scala. I have the following case class: import shapeless._ case class Foo(param1: String, param2: HList) I'd like to obtain a JSON representation of this type, using Circe. I'd also like to map the resulting JSON string back to…
willena
  • 63
  • 7
3
votes
3 answers

Creating a `Decoder` for arbitrary JSON

I am building a GraphQL endpoint for an API using Finch, Circe and Sangria. The variables that come through in a GraphQL query are basically an arbitrary JSON object (let's assume there's no nesting). So for example, in my test code as Strings, here…
Tom Adams
  • 143
  • 1
  • 10