Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 04:25
    codecov-commenter commented #1729
  • 04:23
    codecov-commenter commented #1729
  • 04:13
    scala-steward review_requested #1729
  • 04:13
    scala-steward opened #1729
  • Apr 21 12:29
    scala-steward review_requested #1728
  • Apr 21 12:29
    scala-steward opened #1728
  • Apr 21 07:39
    heksesang commented #1717
  • Apr 20 14:31
    scala-steward review_requested #1727
  • Apr 20 14:31
    scala-steward opened #1727
  • Apr 20 14:18

    travisbrown on master

    Update discipline-munit to 1.0.… (compare)

  • Apr 20 14:18
    travisbrown closed #1723
  • Apr 20 14:18

    travisbrown on master

    Update munit, munit-scalacheck … (compare)

  • Apr 20 14:18
    travisbrown closed #1722
  • Apr 20 14:18

    travisbrown on master

    Update cats-core, cats-laws to … (compare)

  • Apr 20 14:18
    travisbrown closed #1724
  • Apr 20 13:36

    travisbrown on master

    Fix Decoder[None.type] (#1726) (compare)

  • Apr 20 13:36
    travisbrown closed #1726
  • Apr 20 13:36
    travisbrown closed #1725
  • Apr 20 13:36
    travisbrown commented #1726
  • Apr 20 08:08
    nigredo-tori review_requested #1726
Philipp Dörfler
@phdoerfler
fwiw Any and Serializable aren't it.
yurique
@yurique:nowhere.chat
[m]
@phdoerfler: you can represent that as a Tuple2[Double, String]
import io.circe.syntax._

Tuple2(1234.123, "test").asJson
// [
//  1234.123,
//  "test"
// ]

import io.circe.parser._
decode[Tuple2[Double, String]](""" [ 1234.123, "test" ] """)
// Right((1234.123,test))
Philipp Dörfler
@phdoerfler
@yurique:nowhere.chat Thank you! I had no idea! I looked the documentation up and down and didn't find anything about arrays with mixed types
yurique
@yurique:nowhere.chat
[m]

@phdoerfler:

if you also want it in a case class, that's straightforward, too 🙂

    import io.circe._

    case class MyResponse(double: Double, string: String)

    object MyResponse {
      implicit val codec: Codec[MyResponse] = Codec.from(
        implicitly[Decoder[Tuple2[Double, String]]].map((MyResponse.apply _).tupled),
        implicitly[Encoder[Tuple2[Double, String]]].contramap(r => (r.double, r.string))
      )
    }

decode[MyResponse](""" [ 1234.123, "test" ] """)
// Right(MyResponse(1234.123,test))
Yeah, I didn't know about this, either. It just worked – lucky guess =)
Philipp Dörfler
@phdoerfler
@yurique:nowhere.chat Thanks! Much appreciated! I gotta say if it weren't for this chat room I'd long have given up on circe.
yurique
@yurique:nowhere.chat
[m]
Yeah, it does take time and patience to learn.
And it does pay off afterwards :)
(I still have some things to figure out...)
Philipp Dörfler
@phdoerfler
Yeah, I feel like I knew this already and have forgotten
I think at some point I discovered it by accident, too
Anyway, glad that decode[(Double, String)] does the trick
Philipp Dörfler
@phdoerfler
@yurique:nowhere.chat fwiw I discovered that you can get circe to tell you about decoding errors much nicer than the default reporting:
case Left(df: DecodingFailure) =>
  implicitly[cats.Show[DecodingFailure]].show(df) // <- this right here
yurique
@yurique:nowhere.chat
[m]
oh wow, that's really nice!
Philipp Dörfler
@phdoerfler
I even went as far and wrote some code around this to dump the offending json part to a temporary file and add its location to the exception
there's the whole thing. It's still WIP but I use it and it takes out a lot of frustration :) hope this helps!
yurique
@yurique:nowhere.chat
[m]
cool! bookmarking it :)
Philipp Dörfler
@phdoerfler
@yurique:nowhere.chat I just realised that in your code you have the implicit inside of the case class companion object and that automatically brings it into scope, saving you from having to import it explicitly :o I had no idea!
Philipp Dörfler
@phdoerfler
@yurique:nowhere.chat I turned your code into a snippet for VS Code: https://gist.github.com/phdoerfler/22623b64fbc11e556078641855cdea40 :)
jatcwang
@jatcwang:matrix.org
[m]
Don't forget you can use scastie for runnable snippets :)
Philipp Dörfler
@phdoerfler
Good point! I love Scastie and use it all the time! However, the snippet I made lets you specify the name of the case class, its two fields and the types of the two fields and it does all that right within VS Code for ultimate convenience. Scastie can't quite compete with that I'm afraid ;)
Vasco Figueira
@vlfig
So, I have fairly large ADT of case classes representing error conditions. They have different shapes but all of them have an errorCode val. The default derivation wasn't including that member so I customised it to include the errorCode. That is working fine in https://scastie.scala-lang.org/8Bjn1eDBSXCQPlZ3fKuk9A , but, what I really wanted was the name of the jsonObject to be the error code. I'm struggling a bit with the shapeless-foo. Help is much appreciated. Comments in the scastie snippet. Thanking you legends in advance.
jatcwang
@jatcwang:matrix.org
[m]

@vlfig: Does https://circe.github.io/circe/codecs/adt.html#the-future (circe-generic-extras) solve your problem?
You can set the discriminator as errorCode and it should give you something like:

{
  "errorCode": "MayOrMayNotExist",
  "address": "..."
}

You can update transformConstructorName of the Configuration object to change the constant each type maps to. (e.g. snake case may_or_may_not_exist)

Justin Reeves
@justinallenreeves
I’m trying to use Cats and Circe, but I need to deserialize a NonEmptyChain[HeaderValidationError] to json and I’m not sure how.
sealed trait HeaderValidationError {
  def errorMessage: String
}

final case class ExpectUUIDHeaderError(key: String, value: String) extends HeaderValidationError {
  def errorMessage: String = s"Missing Header $key, UUID required, got $value"
}

final case class MissingHeaderError(value: String) extends HeaderValidationError {
  def errorMessage: String = s"Missing Header $value"
}
import cats.data._
import io.circe.generic.semiauto.deriveEncoder
import io.circe.syntax._
import io.circe.Encoder
implicit val encodeHeaderValidationError: Encoder[NonEmptyChain[HeaderValidationError]] =
       deriveEncoder[NonEmptyChain[HeaderValidationError]]
could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder[cats.data.NonEmptyChain[HeaderValidationError]]
ybasket
@ybasket:matrix.org
[m]
@justinallenreeves: Check https://circe.github.io/circe/codecs/adt.html, it explains how you can handle ADTs like yours. And note that circe has a built-in Encoder for NonEmptyChain, so need to derive something there. Just make sure you have an Encoder[HeaderValidationError], then the NEC version comes "for free".
One comment on the first example on that page though: Rather don't use auto derivation, but define the instances in the companion objects using semi-automatic derivation.
Justin Reeves
@justinallenreeves
ah, that works.
Dmitriy Kostyuchenko
@thedmitriyk
Speaking of, does semi-automatic derivation work in Scala 3?
Dmitriy Kostyuchenko
@thedmitriyk
The silence is deafening. :D
jatcwang
@jatcwang:matrix.org
[m]
Dmitriy Kostyuchenko
@thedmitriyk
Yeah, I saw that. I was more thinking of this case.
import io.circe._, io.circe.generic.semiauto._

case class Huh(str: String)

implicit val huhEncoder: Encoder[Huh] = deriveEncoder[Huh]
I think I saw a Dotty issue filed that may address this, but I guess it hasn't been fixed yet.
The error, for reference, is this:
could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder[Huh].
I found:

    shapeless.Lazy.mkLazy[I]

But method mkLazy in object Lazy does not match type shapeless.Lazy[io.circe.generic.encoding.DerivedAsObjectEncoder[Huh]].

One of the following imports might make progress towards fixing the problem:

  import shapeless.~?>.idKeyWitness
  import shapeless.~?>.idValueWitness
  import shapeless.~?>.witness
Spoiler alert: the following imports made progress towards fixing the problem in that including an import removed it from the error message.
jatcwang
@jatcwang:matrix.org
[m]
hm you shouldn't be seeing anything related to shapeless. According to the release notes circe-generic does not depend on shapeless for scala 3
Dmitriy Kostyuchenko
@thedmitriyk
It's literally just this right here: https://scastie.scala-lang.org/F9AQcGYCSky7CBRupgIBsQ
My dependencyTree tells me this:
[info]   +-io.circe:circe-generic_2.13:0.14.0-M5 [S]
[info]   | +-com.chuusai:shapeless_2.13:2.3.3 [S]
Hmm... wait a second...
Dmitriy Kostyuchenko
@thedmitriyk
Okay. I must have initially tried it before circe was cross-published for Scala 3, and then Scastie and Metals both conspired to lie to me.
jatcwang
@jatcwang:matrix.org
[m]
They have never lied to me. I have a very trusting and fruitful friendship with them ;)
CodingPenguin
@TheCodingPenguin
Is there a better way to understand errors with Auto and SemiAuto encoding:
could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder
I understand that something has missing encoder within the object but I'm rather dealing with a complicated one and looking at all the different classes it should be ok :D
jatcwang
@jatcwang:matrix.org
[m]

@TheCodingPenguin: Not until Scala 3 unfortunately. Scala 2's implicit derivation error is not very helpful.

To debug it, I'd try to see if each field has an Encoder.

implicitly[Encoder[Field1]] etc

Alexandre DUVAL
@KannarFr
trait A
case object AA extends A

sealed abstract class AClass(a: A)
case class BClass(b: X) extends A(AA)
Hi, when serialize this I only got the x field, do you know any way to serialize BClass instance and get b and a fields?
jatcwang
@jatcwang:matrix.org
[m]
@KannarFr: you will have to write the encoder manually. derivation only look at the case class constructor fields so that's why you only see field b
octeep
@octeep:m.octeep.xyz
[m]
Hello, is there a way to compare if two Json have the same elements? Key order for objects should not matter. And value order in arrays should not matter either.
jatcwang
@jatcwang:matrix.org
[m]
octeep: Not for what you want unfortunately, since for json array comparison the order does matter (in the defaults equals implementation)
octeep
@octeep:m.octeep.xyz
[m]
Right, I've implemented my own function to do that. Is there a reason why JObject, JArray, JNumber and such are private? If they were public it would've made pattern matching much simpler.
jatcwang
@jatcwang:matrix.org
[m]
I believe it is to make it easier to change the AST under the hood, so hiding it makes it easier to maintain binary compatibility reasons. It might be a decision inherited from Argonaut I think?