Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Colt Frederickson
    @Fristi The best thing to do is to write an encode json and decode json instance separately which correctly addresses the partiality of your conversion.
    Mark de Jong

    This might be a useful contribution:

      implicit class RichCodecJson[A](val codecJson: CodecJson[A]) {
        def narrow[B](f: B => A, g: A => String \/ B): CodecJson[B] =
            x => codecJson.encode(f(x)),
            cur =>
              codecJson.decode(cur).flatMap(x => g(x).fold(err => DecodeResult.fail(err, cur.history), DecodeResult.ok))


          x => fromString(x).fold[String \/ MerchantStatus](-\/(s"Could not map '$x' to MerchantStatus"))(\/-.apply)
    hey folks
    I'm pretty new to scala and just discovered argonaut, which I love so far
    I have some json that looks like this:
    say that I model both of these as sublcasses of type Event
    is there an idiomatic way to parse this list .as[List[MarketEvent]]?
    Gary Pamparà
    @elsehow something along the lines of https://gist.github.com/markhibberd/8231912 should help you
    Not sure what you mean with that MarketEventbit, but you can discriminate on the type property to determine which decoder to use
    Yuval Itzchakov
    When migrating from argonaut 6.1 -> 6.2-scalaz, are there any changes in the imports that need to be done accordingly?
    I'm unable to find a decoder for Array[T] all of the sudden.
    Lehel Iszlai

    Hey guys I have a really basic question,
    for a basic webhook thing I'd like to parse json partially and keep a field as string

       val rawJson: String ="""
      "url": "http://bar",
      "method": "GET",
      "body": "{"tucsfoo":""}",
      "cronExpression": "* * * * *"
    case class CronTask(url: String, method: String, body: String, cronExpression: String)
    object CronTask {
      import argonaut._, Argonaut._
      implicit def PersonCodecJson: CodecJson[CronTask] =
        casecodec4(CronTask.apply, CronTask.unapply)("url", "method", "body", "cronExpression")

    I would love to get back the body as a string, how would i do that?

    Gary Pamparà
    Doesn't that work?
    Lehel Iszlai
    @gpampara nope :(
    Kevin Meredith
    Hello! Will CursorHistory#toString, i.e. on a Left of DecodeResult(result: Either[(String, CursorHistory), A) ever include JSON values? Or it will only include the traversed path, i.e. which keys were walked, and which failed
    u can get a List[CursorOp]
    Pyry-Samuli Lahti
    Hi guys! If I have a CodecJson[Example] (defined by casecodec20) but would like to change the decoder so that a particular missing field would default to some value, what would be the way to do it?
    The signature of def flatMapCursor(f: HCursor => DecodeResult[HCursor]): DecodeJson[A] looks promising, but I'm not quite sure if and how could it be utilized.
    Pyry-Samuli Lahti
    FYI, solved it by using withFocus:
    implicit def decodeJson = DecodeJson[Example] { c =>
        c.withFocus(js =>
          if (js.hasField("arrayOfStuff")) js
          else js.withObject(_ + ("arrayOfStuff", jEmptyArray))
    Anirudh Vyas
    i have a rather simple problem - I have a case class with name, someXField, paths : Seq[String] -- I try to do casecodec3 using argonaut but argonaut cannot recognize Seq[String] - what am I missing? any suggestions
    Pyry-Samuli Lahti
    IIRC there's no implicit codec for Seq. Create one youself, or use e.g. List instead
    Anirudh Vyas
    I ended up using List.
    thanks for the help
    Francisco Arámburo
    why is that this works:
          .as[Option[String]](DecodeJson[Option[String]] { hcursor =>
            (hcursor --\ "lol").as[Option[String]]
          }) shouldBe DecodeResult.ok(None)
    but this fails:
          .as[Option[OrderStatus]](DecodeJson[Option[OrderStatus]] { hcursor =>
            (hcursor --\ "lol").as[Option[OrderStatus]]
          }) shouldBe DecodeResult.ok(None)
    with: DecodeResult(Left((Attempt to decode value on failed cursor.,CursorHistory(List(El(CursorOpDownField(lol),false)))))) was not equal to DecodeResult(Right(None)) (OrderJsonSpec.scala:30)
    So, does "io.argonaut" %% "argonaut" % "6.2.2" at the moment, support automatic codec derivation for an ADT?
    pretty much that's what it does
    Pyry-Samuli Lahti
    Hmm.. The "argonaut-jawn" % "6.2.2" JAR includes only the manifest? Am I missing something? See e.g. http://central.maven.org/maven2/io/argonaut/argonaut-jawn_2.11/6.2.2/argonaut-jawn_2.11-6.2.2.jar
    Pyry-Samuli Lahti
    ^ Created Github issue argonaut-io/argonaut#315
    Raymond Barlow
    hey peeps
    how does one deal with ADT encode/decode in argonaut?
    Alex Cruise
    Hey folks, I know there's an IDSL for path navigation with history, cursors etc., has anyone built a JsonPath/jq style EDSL for it?
    I've built a prototype with https://github.com/json-path/JsonPath but it's not very scala-like
    Kevin Meredith

    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.

    on http://argonaut.io/

    Eric K Richardson
    Hi Guys, trying to convert some test code from 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
    The 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.
    Also, having some difficulty with matching jNull and or friends.
    Any help appreciated.
    Rohde Fischer
    is it correctly observed that I cannot parse a String to Json and get some strongly typed error?
    Rohde Fischer
    I guess the Scaladoc answers that, since I cannot find any error types :)
    D Cameron Mauch
    I have an ADT called Product (sealed trait with a few case objects). I created encoder and decoder for it. But I need to encode/decode for Option[Product]. How do I do that?
    I would have thought if the base type has an encoder/decoder, it would already know how to handle an Option of that type.