by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Sep 29 20:25
    scala-steward review_requested #1543
  • Sep 29 20:25
    scala-steward opened #1543
  • Sep 29 17:14
    Lasering edited #1542
  • Sep 29 17:10
    Lasering opened #1542
  • Sep 28 12:27
    codecov-commenter commented #1541
  • Sep 28 12:24
    codecov-commenter commented #1541
  • Sep 28 12:12
    scala-steward review_requested #1541
  • Sep 28 12:12
    scala-steward opened #1541
  • Sep 24 09:59
    travisbrown closed #1533
  • Sep 24 09:57
    travisbrown closed #1535
  • Sep 24 09:56

    travisbrown on master

    Update refined, refined-scalach… (compare)

  • Sep 24 09:56
    travisbrown closed #1539
  • Sep 21 14:33
    neko-kai opened #1540
  • Sep 20 10:33
    codecov-commenter commented #1539
  • Sep 20 10:30
    codecov-commenter commented #1539
  • Sep 20 10:18
    scala-steward review_requested #1539
  • Sep 20 10:18
    scala-steward opened #1539
  • Sep 18 16:34
    scala-steward opened #1538
  • Sep 18 16:34
    scala-steward review_requested #1538
  • Sep 16 21:28
    benhutchison commented #877
Travis Brown
@travisbrown
If you have a minimisation I'd be happy to take a look
Tharun Kumar Kapse
@tkapse
@travisbrown I tried one of online json validators and it was able to parse it..
Travis Brown
@travisbrown
:thumbsup:
Tharun Kumar Kapse
@tkapse
{
"first_name": "tharun",
"last_name": "kapse",
"gender": "male",
"address": {
"flat_no": "XXXX",
"street": "XXXX",
"address_line1": "XXXX",
"address_line2": "XXXX",
"city": "XXXX",
"Landmarks": [
],
"others": {},
"city": "XXXX",
"State": "XXXX"
},
"email": "XXXX"
}
each object would look something like this
in the array
Jente Hidskes
@Hjdskes
Given a Decoder, is it possible to retrieve the discriminator used in this decoder, if any?
Travis Brown
@travisbrown
@Hjdskes you could have a subtype of Decoder that carries around extra structure / info like that, but it's not currently provided.
Jente Hidskes
@Hjdskes
@travisbrown Thanks :)
Michael Sluyter
@msluyter
Hi. Pretty much of a newbie question. I was just attempting the Quick Start section of the docs and get the following:
[info] Starting scala interpreter...
Welcome to Scala 2.13.3 (Java HotSpot(TM) 64-Bit Server VM, Java 11.0.7).
Type in expressions for evaluation. Or try :help.

scala> import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._
     |
import io.circe._
import io.circe.generic.auto._
import io.circe.parser._
import io.circe.syntax._

scala> sealed trait Foo
     |
trait Foo

scala> case class Bar(xs: Vector[String]) extends Foo
     |
class Bar

scala> case class Qux(i: Int, d: Option[Double]) extends Foo
     |
class Qux

scala> val foo: Foo = Qux(13, Some(14.0))
     |
val foo: Foo = Qux(13,Some(14.0))

scala> val json = foo.asJson.noSpaces
     |
                      ^
       error: Internal error: unable to find the outer accessor symbol of class

basically just cut & pasted the code from the example. I did add

scalacOptions ++= Seq("-Ymacro-annotations")

to my build.sbt

Anyone seen this before and/or have any ideas?
Travis Brown
@travisbrown
@msluyter ugh, I hadn't seen that particular one before, but the 2.13 patch releases have broken a ton of stuff in the REPL in general…
this one looks like it broke in 2.13.2.
it should be fine if you use :paste mode for the sealed trait and case classes
(i.e. enter :paste, copy and paste those lines, and then ctrl-D)
Michael Sluyter
@msluyter
using :paste worked, thanks @travisbrown!
Marcin Pucilowski
@Pucilowski
I'm using configured codecs from the extras package to serialize ADT hierarchies with a "type" property. Is there a way to supply a custom class<->string mapping for the value of this property? e.g. type StringDef maps to "string" , not "StringDef". Would it be easier to just type out my encoders/decoders manually?
Travis Brown
@travisbrown
@Pucilowski extras provides configuration options for transforming constructor names that should work
(Sorry, not at a computer at the moment, but if you look for “transform + constructor names” it should come up)
Ibrahim Moufti
@Mouftizo

Hello everyone : ) appologies in advance if this comes as a noob question as I am relatively new to Circe.

I have a JSON that looks like:

{
  "data": [
    {
      "id": "1",
      "email": "hello@world.com",
      "name": "Mr foo",
      "roles": [
        "Chief Bar Officer"
      ],
      "avatar_url": null,
      "phone_number": null
    },
    {
      "id": "2",
      "email": "bye@world.com",
      "name": "Mr baz",
      "roles": [
        "Chief Baz Officer"
      ],
      "avatar_url": null,
      "phone_number": null
    }
  ]
}

I am mainly interested in parsing/deserializing the data list, and I would like to do that manually (I prefer the manual way for some mysterious reason).

In case this is relevant, I am using sttp's circe library sttp.client.circe._ with the intention of parsing incoming data from get requests directly into Json using asJson.

A get sttp request looks something like:

val r1 = basicRequest
    .get(uri"https://woooo.woo.wo/v1/users")
    .header("accept", "application/json")
    .header("Authorization", "topsecret"
    .response(asJson[SomeClass])

This is what I have tried so far:

// Define the case class
case class User(
    id: String,
    email: String,
    name: String,
    roles: List[String],
    avatar_url: Option[String],
    phone_number: Option[String]
)

// Define the manual deserializer

case object User {

  implicit val userDecoder: Decoder[User] = (hCursor: HCursor) => {
    val data = hCursor.downField("data").downArray
    for {
      id <- data.get[String]("id")
      email <- data.get[String]("email")
      name <- data.get[String]("name")
      roles <- data.get[List[String]]("roles")
      avatarUrl <- data.get[Option[String]]("avatarUrl")
      phoneNumber <- data.get[Option[String]]("phoneNumber")
    } yield User(id, email, name, roles, avatarUrl, phoneNumber)
  }
}

The problem with my approach (I think) is that .downArray makes me only serialize the first User in the array of Users.

My objective is to be able to have some sequence of users (something like List[User] maybe), but at the moment I only end up deserializing one user in the array.

It is worth mentioning that the "data" array, does not contain a fixed-number of users, and every api call could result in a different number of users.

Travis Brown
@travisbrown
@Mouftizo it would be better to build up the instance you need to parse the top-level JSON object compositionally…
i.e. have a Decoder[User] that only decodes a single user JSON object, and then use Decoder[List[User]].at("data") or something similar to decode the top-level JSON object containing the data field with the JSON array.
Ibrahim Moufti
@Mouftizo
@travisbrown I thought of doing something like: val decodingResult = parser.decode[List[User]](incomingString) but I am using the sttp asJson , so not sure how to make that work with that (as I don't access the raw string). Not sure if that is 100% in line with what you suggested
Travis Brown
@travisbrown
@Mouftizo in that case you could do something like this:
case class Users(value: List[User])

object Users {
  implicit val decodeUsers: Decoder[Users] = Decoder[List[User]].at("data").map(Users(_))
}
6 replies
and then asJson[Users]
felher
@felher
Is there a reason that there is no emap on KeyDecoder, just map? Should errors not be possible on key decoders?
Matthew de Detrich
@mdedetrich
So I am using Circe 0.13.0 on Scala.js with scala-java-time and I am getting the following error locally when compiling
[error] Referring to non-existent method java.time.format.DateTimeFormatter$.ofPattern(java.lang.String)java.time.format.DateTimeFormatter
[error]   called from de.zalando.logistics.common.itemtrace.model.DeployedCirceImplicits.$$init$()scala.Unit
[error]   called from de.zalando.logistics.common.DeployedGlobalCirceImplicits$.<init>()
[error]   called from states.States$.<init>()
[error]   called from components.Header$.$$anonfun$component$2(japgolly.scalajs.react.CompScope$DuringCallbackU,components.HeaderProps,scala.runtime.BoxedUnit)japgolly.scalajs.react.ReactElement
[error]   called from components.Header$.<init>()
[error]   called from components.Content$.$$anonfun$component$2(japgolly.scalajs.react.CompScope$DuringCallbackU,components.ContentProps,scala.runtime.BoxedUnit)japgolly.scalajs.react.ReactElement
[error]   called from components.Content$.<init>()
[error]   called from components.backend.Backend.render(components.MainProps,states.States$MainObject)japgolly.scalajs.react.ReactElement
[error]   called from components.MainComponent$.$$anonfun$App$3(japgolly.scalajs.react.CompScope$DuringCallbackU)japgolly.scalajs.react.ReactElement
[error]   called from components.MainComponent$.<init>()
[error]   called from components.KeyCapture$Backend.render(components.KCInput)japgolly.scalajs.react.ReactComponentU
[error]   called from components.KeyCapture$.$$anonfun$component$3(japgolly.scalajs.react.CompScope$DuringCallbackU)japgolly.scalajs.react.ReactComponentU
[error]   called from components.KeyCapture$.<init>()
[error]   called from Main$.main([java.lang.String)scala.Unit
[error]   called from core module module initializers
Does anyone have any ideas here?
Matthew de Detrich
@mdedetrich
I am explicitly including scala-java-time 2.0.0 in my library dependencies
Scala-java-time is referring to https://github.com/cquiroz/scala-java-time btw
Matthew de Detrich
@mdedetrich
@cquiroz From Scala-java-time says that the scala.js linker gets very confused if there are two packages providing the same set of classes
Matthew de Detrich
@mdedetrich
Any ideas on how to resolve this?
Travis Brown
@travisbrown
@felher key decoders can only either fail or not, there's no error value (the idea is that they're only ever used in the context of value decoding, which will generally have enough context to provide a useful error).
I could imagine an emap-like def omap[B](f: A => Option[B]): KeyDecoder[B] method, but so far nobody's proposed it.
@mdedetrich you're sure you don't have multiple scala-java-time versions on the classpath, or something like that?
Marcin Pucilowski
@Pucilowski
Is there a library that given a Json instance I can pluck out every object containing a property x?
This is easily done in most JsonPath implementations but what circe-optics refer to as JsonPath doesn't feature recursive traversals
Travis Brown
@travisbrown
@Pucilowski you can do that kind of thing with circe-droste: https://github.com/circe/circe-droste
like everything related to recursion schemes it's a mix of incredibly elegant and completely impenetrable nonsense though
Marcin Pucilowski
@Pucilowski
oh that looks like a fun can of worms to be opening
thanks
Travis Brown
@travisbrown
I also have a recursive ("internal") visitor implementation, but it's not released yet. see e.g.: https://meta.plasm.us/posts/2019/09/23/scala-and-the-visitor-pattern/
I'm using something similar in this Dhall implementation and it works very nicely: https://github.com/travisbrown/dhallj
Marcin Pucilowski
@Pucilowski
Hm, I also discovered that circe-optics features a monocle Plated implementation which could be useful
Is there much of a learning curve to circe-droste or is most of the recursion scheme complexity tucked away?
Matthew de Detrich
@mdedetrich
@travisbrown Don't think so but I will double check this
Travis Brown
@travisbrown
@Pucilowski the circe-droste API itself is tiny, but Droste is pretty huge
Matthew de Detrich
@mdedetrich
@travisbrown So I can confirm we are using the same version of scala-java-time everywhere