travisbrown on master
Update discipline-munit to 1.0.… (compare)
travisbrown on master
Update munit, munit-scalacheck … (compare)
travisbrown on master
Update cats-core, cats-laws to … (compare)
travisbrown on master
Fix Decoder[None.type] (#1726) (compare)
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))
@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))
decode[(Double, String)]
does the trick
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.
@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
)
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]]
Encoder
for NonEmptyChain
, so need to derive something there. Just make sure you have an Encoder[HeaderValidationError]
, then the NEC version comes "for free".
import io.circe._, io.circe.generic.semiauto._
case class Huh(str: String)
implicit val huhEncoder: Encoder[Huh] = deriveEncoder[Huh]
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
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]
could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder
@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
b