Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Paul Snively
    @PaulAtBanno
    Hmmm
    Colt Frederickson
    @coltfred
    scala> import argonaut.JsonIdentity._
    import argonaut.JsonIdentity._
    
    scala> "1".asJson
    res0: argonaut.Json = "1"
    @PaulAtBanno JsonIdentity
    If you want to mix that in to another object you're importing you can use object Foo extends argonaut.JsonIdentitys
    Paul Snively
    @PaulAtBanno
    @coltfred: Heya, thanks!
    Colt Frederickson
    @coltfred
    @PaulAtBanno np.
    Paul Snively
    @PaulAtBanno
    OK, more of a dumb implicit/import question: if that same module imports ArgonautShapeless._, how do I expose those implicits outside the module?
    I reallllly want to just say import MyCodecs._ in client code, and have automatic case class codec derivation, etc. "just work."
    Colt Frederickson
    @coltfred
    Sadly, I don't know much about that. If the ArgonautShapeless provides a trait, you could mix that in to MyCodecs?
    SteveDraper
    @SteveDraper

    I assume it's a very deliberate design decision, but I was surprised to discover that Argonaut allows Json numbers to be quoted in the Json (which would technically make them Json strings). For example:

    scala> import argonaut._
    import argonaut._
    
    scala> import Argonaut._
    import Argonaut._
    
    scala> val noQuoteNum = "1"
    noQuoteNum: String = 1
    
    scala> val quotedNum = """"1""""
    quotedNum: String = "1"
    
    scala> noQuoteNum.decodeOption[Int]
    res0: Option[Int] = Some(1)
    
    scala> quotedNum.decodeOption[Int]
    res1: Option[Int] = Some(1)

    Can anyone shed any light on why this behavior was considered desirable for me...?

    Iván Portilla
    @Ivykp
    Hello everybody, i would like to ask you a question. Let's say i have case class Person(id: String, money: Double)i am using Argonaut to allow this entity to be serializable to json. However when i add the codec an evaluate the asJson, i get the money value as scientific notation. Is there any way to avoid this scientific notation?
    SteveDraper
    @SteveDraper
    @Ivykp If its a monetary value then Double is probably the wrong type. Better to normalize it to its minimal unit (cents?) and use an integer type most likely
    Iván Portilla
    @Ivykp
    @SteveDraper thanks for the answer. In my case is was just for example terms, in my real case i need a decimal type.
    Gary Pamparà
    @gpampara
    @Ivykp best to see what that would decode to, the decimal that is
    Ashlin Eldridge
    @ashlineldridge
    @SteveDraper Did you figure out a solution to your problem re: "converting a project from scalaz 7.1.x to 7.2.x and as a result of that from Argonaut 6.1 to 6.2..."? I'm having the same issue and would like to continue using Scalaz's \/ rather than Either but unsure how to proceed.
    Kjetil Mehl
    @aspic
    Hi! I'm trying to provide a client with a more meaningful decode error, and try to traverse the cursorhistory. However, when I manually handle a DecodeResult in a given codec I don't get the history of the parent path, even though I call c.history (it's just empty). Am I overlooking something in the cursor class? :-)
    If I let argonaut handle decodeResult itself (with a simple codec) the history gets appended just fine.
    Andreas Jim-Hartmann
    @devkat
    hi, quick question:
    Parse.decodeOption[Map[String, Seq[String]]](jsonString)
    doesn’t compile since it is missing the implicit EncodeJson for this type
    I tried adding a CodecJson for Seq[A] (based on List), but it didn’t help
    any hints? TIA!
    I’m using version 6.2 BTW
    Colt Frederickson
    @coltfred

    @devkat This in general is a bad place to get help. I've tried a few times and have failed. In this case I think I can help you. If you really wanted to deserialize the Seq version you just need an implicit for "Seq".

    
    scala> implicit def SeqDecodeJson[A](implicit e: DecodeJson[A]): DecodeJson[Seq[A]] = CanBuildFromDecodeJson[A, Seq]
    SeqDecodeJson: [A](implicit e: argonaut.DecodeJson[A])argonaut.DecodeJson[Seq[A]]
    
    scala> Parse.decodeOption[Map[String, Seq[String]]]("""{"foo":["bar","baz"]}""")
    res4: Option[Map[String,Seq[String]]] = Some(Map(foo -> Vector(bar, baz)))

    But remember that if you just deserialize as a Vector or List you can refer to it as a Seq.

    scala> val x: Option[Map[String, Seq[String]]] = Parse.decodeOption[Map[String, Vector[String]]]("""{"key": ["value1", "value2"]}""")
    x: Option[Map[String,Seq[String]]] = Some(Map(key -> Vector(value1, value2)))

    This is because Vector is a Seq.

    Andreas Jim-Hartmann
    @devkat
    @coltfred, thanks a lot for your reply!
    Mark de Jong
    @Fristi
    Is there a variant of xmap on CodecJson where decoding of failure is taken in to account ?
    Mark de Jong
    @Fristi
    This is not nice to write
    implicit val json: CodecJson[MerchantStatus] = codecJson[String]
        .xmap[MerchantStatus](x => fromString(x).getOrElse(sys.error(s"Could not map '$x' to a MerchantStatus")))(_.status)
    Colt Frederickson
    @coltfred
    @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
    @Fristi

    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] =
          CodecJson(
            x => codecJson.encode(f(x)),
            cur =>
              codecJson.decode(cur).flatMap(x => g(x).fold(err => DecodeResult.fail(err, cur.history), DecodeResult.ok))
          )
      }

    Resulting:

    codecJson[String]
        .narrow[MerchantStatus](
          _.status,
          x => fromString(x).fold[String \/ MerchantStatus](-\/(s"Could not map '$x' to MerchantStatus"))(\/-.apply)
        )
    elsehow
    @elsehow
    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:
    {
       "events":[
          {
             "type":"trade",
             "tid":1111597035,
             "price":"2559.98",
             "amount":"0.07365713",
             "makerSide":"ask"
          },
          {
             "type":"change",
             "side":"ask",
             "price":"2559.98",
             "remaining":"20.98651537",
             "delta":"-0.07365713",
             "reason":"trade"
          }
       ]
    }
    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à
    @gpampara
    @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
    @YuvalItzchakov
    Hi,
    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
    @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à
    @gpampara
    Doesn't that work?
    Lehel Iszlai
    @iszlai
    @gpampara nope :(
    Kevin Meredith
    @kevinmeredith
    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
    @Pyppe
    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
    @Pyppe
    FYI, solved it by using withFocus:
    implicit def decodeJson = DecodeJson[Example] { c =>
      originalCodec.Decoder.decode(
        c.withFocus(js =>
          if (js.hasField("arrayOfStuff")) js
          else js.withObject(_ + ("arrayOfStuff", jEmptyArray))
        )
      )
    }
    Anirudh Vyas
    @AnirudhVyas
    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
    @Pyppe
    IIRC there's no implicit codec for Seq. Create one youself, or use e.g. List instead
    Anirudh Vyas
    @AnirudhVyas
    I ended up using List.
    thanks for the help
    Francisco Arámburo
    @FrancoAra
    hello