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

json Generic decoder with default values using scala with circe

I have encountered a weird situation. I m trying to build a method that takes a type and a JSON and build it into a case class instance and if needed auto-complete missing key values. So far I managed to do everything separately but not…
soosita
  • 187
  • 13
3
votes
1 answer

Decode json inside json string

I'm dealing with an API which expects a JSON object where one of the values (blob) is a JSON object stringified: { "credential": { "blob": "{\"access\":\"181920\",\"secret\":\"secretKey\"}", "project_id":…
Simão Martins
  • 1,210
  • 11
  • 22
3
votes
0 answers

How to Use Circe for Decoding JSON Lists with Different Data Types in Scala

There is a complex json object. The "value" field in decode can have different types. I am new to scala so I will be grateful for any idea to do so. {"price":0, "price_currency":"USD", "decode":[ {"label":"Make","value":"BMW"}, …
George
  • 53
  • 5
3
votes
1 answer

Retrieving null values from json using circe-optics

The json I have looks like this: { "cards": [ { "card_id":"1234567890", "card_status":"active", "card_expiration":{ "formatted":"01/20" }, "debit":{ …
Marek M.
  • 3,799
  • 9
  • 43
  • 93
3
votes
2 answers

Automatic derivation of Enumeration can't find Encoder

I have a case class like this: case class Admin(name: String, role: Role) Role is an Enumeration object Role extends Enumeration { type Role = Value val Manager = Value } When I try to: import io.circe.generic.auto._, io.circe.syntax._ val…
Chih
  • 105
  • 7
3
votes
2 answers

Using Circe custom codec to decode json to a list of a case class

I am trying to use Circe's custom codecs to decode json to a List of a trait Feature which is broken down into two case classes: trait Feature { def name: String def index: Int def frequency: Int } case class NumericFeature(name, index,…
3
votes
2 answers

How to decode missing json array as empty List with circe

For example, we have some case class case class Foo(a: Int, b: List[String]) And we want to deserialize instance of Foo from json {"a": 1} replacing missing b array with Nil We can create custom decoder for such behavior implicit val fooDecoder:…
3
votes
1 answer

How to create custom encoding of Option types with Circe?

It's possible to have a class that looks like so: case class Amount(value: Int) case class Data(insurance: Option[Amount], itemPrice: Amount) If insurance = None it should get a default value of waived: true E.g: Data(Some(123),100).asJson //…
PhD
  • 11,202
  • 14
  • 64
  • 112
3
votes
1 answer

How do I get circe to have an either/or output scenario for the generated Json?

Here's what I intend - let's say I have a field called medical_payments - it can "either" be a limit if one elects or waived { "medical_payments": { "limit_value":"one_hundred" } } If it's elected as a waiver then it should be: { …
PhD
  • 11,202
  • 14
  • 64
  • 112
3
votes
1 answer

Automatic derivation of codecs for structural types

Given the following JSON structure val rawJson = """ |{ | "users": [ | { | "name": "Mario", | "age": 10, | "address": { | "street": "13 Blvd", | "postcode": "ABC…
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
3
votes
2 answers

Transform JSON tree to other format (XML, CSV etc.) recursively with circe

In order to transform JSON nodes to an other format than JSON (like XML, CSV etc.) with circe I came up with a solution where I had to access internal data structures of circe. This is my working sample that transforms JSON to a XML String (not…
Urs Peter
  • 53
  • 6
3
votes
2 answers

Scala - Circe - Case Class Serialization without Class Name

I have used Circe previously for case class serialization / deserialization, and love how it can be used without the boilerplate code required by other Scala JSON libraries, but I'm running into an issue now I'm not sure how to resolve. I have an…
Timothy Perrigo
  • 723
  • 4
  • 18
3
votes
2 answers

Streamed JSON decoding using preferably Circe and Akka Streams

My use case is similar to this entry, in wanting to read an inner, huge array (multiple gigabytes as text) from within a JSON object such as: { "a": "...", // root level fields to be read, separately ... "bs": [ // the huge array, most…
akauppi
  • 17,018
  • 15
  • 95
  • 120
3
votes
1 answer

Blending Magnolia with Circe's trick for automatic derivation

I've got a typeclass and want to provide semi-automatic and automatic derivation to users. I have a working implementation based on Magnolia and it works really well. There's a trait providing definitions for Typeclass[A], combine[A] and…
3
votes
0 answers

"Dynamic" JSON Decoding with Circe

Consider this JSON: { "myDocument": { "static_key": "value", "dynamic_key": "value", "static_key2": "value2", "dynamic_key2": { "dynamic_key3": "value3" } } } The JSON documents that I'm going to process have some…
Alejandro Echeverri
  • 1,328
  • 2
  • 19
  • 32