Hi - is argonaut 6.2.2
backwards compatible with 6.2
, i.e. it's truly a "patch" version bump?
Nevermind, I see:
All minor releases (from 6.0 onwards) are binary compatible, i.e. 6.1.x stream are drop in replacements, and the same will be true for 6.2.x, but going from 6.1 to 6.2 may require changes or recompilation.
lift-json
to argonaut
so I can take advantage of Scala Native support and 2.13
support. The is the code I'm trying to convert. https://github.com/ekrich/sconfig/blob/master/sconfig/src/test/scala/org/ekrich/config/impl/JsonTest.scala#L60-L92
toJson
I'm having more luck with. I'm have difficulty with JNumber
pattern matching. I have somelike this.
private[this] def fromJson(json: Json): AbstractConfigValue = {
import scala.collection.JavaConverters._
json match {
case JObject(fields) =>
val m = new HashMap[String, AbstractConfigValue]()
fields.toMap.foreach({ field =>
m.put(field._1, fromJson(field._2))
})
new SimpleConfigObject(fakeOrigin(), m)
case JArray(values) =>
new SimpleConfigList(fakeOrigin(), values.map(fromJson(_)).asJava)
case JNumber(i) =>
i match {
case JsonLong(v) => longValue(v)
case JNumber(v) => i.
}
jNull
and or friends.
Option
of that type.
java.time._
types: implicit val codec: JsonValueCodec[Map[String, List[ZonedDateTime]]] = JsonCodecMaker.make(CodecMakerConfig())
Hi, I have the following string that I pass to a Parse.EncodeEither method like below:
val test ="""{"key":{"serial":0},"checkIn":true,"repositoryID":"rep_id","state":"disabled"} """
val re = Parse.decodeEither[A](test).left.map(MalformedContent(_))
I'am using argonaut 6.2.2, this code result in the below error:
Left(MalformedContent(String: CursorHistory(List(El(CursorOpDownField(serial),true), El(CursorOpDownField(key),true))),None))
Json
In project A i have
sealed trait ParameterValue
Case Class IntegerVal(v: int) extends ParameterValue
and in project B i have a companion object to ParameterValue
Object ParameterValue {
implicit val dec: DecodeJson[ParameterValue] = DecodeJson(c =>
c.as[Int].map[ParameterValue](IntegerVal(_)))
}
and also in project b:
Object MMC
{
implicit val dec: DecodeJson[MMC] = DecodeJson(r => for {
parameters <- (r --\ "parameters").as[Map[String, ParameterValue]]
} yield MMC(parameters));
}
yet it fails that it cannot find the decoder for ParameterValue.
Is it an issue to have the companion object in another project than the trait and case classes?
Any ideas?
@plokhotnyuk is there another way to achieve my goal of having the data in one project and the conversion in another? I do not mind it to be more explicit. But I if I try to do
parameters <- (r --\ "parameters").as[Map[String, ParameterValue]](ParameterValue.dec)
then I get that it needs to the type to be DecodeJson[Map[String, ParameterValue]]
but has the type DecodeJson[ParameterValue]
which indicates, that I need to combine a map decoder with a parametervalue decoder. I have found the operation &&&
to do this, but i cannot find the map decoder in argonaut
Okay solved it by:
Object MMC
{
implicit val dec: DecodeJson[ParameterValue] = DecodeJson(c =>
c.as[Int].map[ParameterValue](IntegerVal(_)))
implicit val dec: DecodeJson[MMC] = DecodeJson(r => for {
parameters <- (r --\ "parameters").as[Map[String, ParameterValue]]
} yield MMC(parameters));
}
yet I would still love to know how to combine an implicit with an explicit decoder
is the intent for OptionDecodeJson
to decode to Some
when the value decodes successfully and fail to decode when the value does not decode successfully?
scala> Json.jNumber(42).as(OptionDecodeJson(JBooleanDecodeJson))
res29: argonaut.DecodeResult[slamdata.Predef.Option[Boolean]] = DecodeResult(Left((java.lang.Boolean,CursorHistory(List()))))
it behaves this way because DecodeResult#option
behaves this way. DecodeResult#option
is used in the implementation for OptionDecodeJson
here: https://github.com/argonaut-io/argonaut/blob/v6.3.0/argonaut/shared/src/main/scala/argonaut/DecodeJson.scala#L345
scala> (Json.obj(("bar", Json.jNumber(42))).hcursor --\ "foo").as[Option[String]]
res37: argonaut.DecodeResult[slamdata.Predef.Option[slamdata.Predef.String]] = DecodeResult(Right(None))
scala> (Json.obj(("foo", Json.jNumber(42))).hcursor --\ "foo").as[Option[String]]
res39: argonaut.DecodeResult[slamdata.Predef.Option[slamdata.Predef.String]] = DecodeResult(Left((String,CursorHistory(List(El(CursorOpDownField(foo),true))))))
I would expect the later to return DecodeResult(Right(None))
as well. If I want to ensure that the decoder doesn't fail in the latter case, I can simply check the result, and return Right(None)
if it failed. I just hadn't realized it had this behavior til now :)