Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Jonas Michel
    @jonasrmichel
    Aha! I just realized this is actually a byproduct of the fixed re-implementation of DecodeResult.option().
    Jonas Michel
    @jonasrmichel

    Which is good. I'm glad that fix got in. The difference in behavior was just surprising upon upgrade.

    I would encourage those (myself included) who are "cheating" and decoding sum types by pattern matching against tuples of Options to properly decode your sum types with the DecodeJson and DecodeResult ||| combinator.

    Gary Pamparà
    @gpampara
    I’ve never seen that issue, and decoding to option etc just seems strnage to me. DecodeResult is a stucture that can handle failure, so the use of Option as the type of the decode does seem weird to me.
    With that I mean decoding/parseDecoding into Option[Foo]
    Colt Frederickson
    @coltfred
    What's the chance that we can get a new version of 6.2 published? argonaut-io/argonaut#254 isn't in 6.2-RC2
    toxicafunk
    @toxicafunk
    hi, i'm a bit confused about some behaviour on argonaut 6.1
    I have the following worksheet
     package argonaut
    
    import argonaut._
    import argonaut.Argonaut._
    
    import scalaz._
    import Scalaz._
    
    object parser {
        case class Course (
            id: Int
            , teachingCenter_id: Int
            , name: String
            , cycle: String
            , materials: Option[List[String]]
        )
    
        object Course {
          implicit def CourseCodecJson: CodecJson[Course ] =
            Argonaut.casecodec5(Course .apply, Course .unapply)(
                "id"
                  , "teachingCenter_id"
                ,"name"
                ,"cycle"
                ,"materials"
            )
        }
      val res = """[{"id":3,"name":"Math 101","cycle":"First","materials":[],"teachingCenterId":1}]"""
                                                      //> res  : String = [{"id":3,"name":"Math 101","cycle":"First","materials":[],"t
                                                      //| eachingCenterId":1}]
        res.decodeOption[List[Course]]            //> res0: Option[List[argonaut.parser.Course]] = None
        res.parse match {
            case \/-(j) => (+j).\\.flatMap(_.--\("materials")).map( (c:Cursor) => c.focus) == Some(jEmptyArray)
            case -\/(s) => s
        }                                         //> res1: Any = true
    
    }
    "materials":[] produces a None even though its declared as materials: Option[List[String]]
    i would expect this to be valid json
    what am I missing?
    toxicafunk
    @toxicafunk
    oh, its not the parsing, my case class is wrong (teachingCenter_id != teachingCenterId)
    David Ainslie
    @davidainslie
    How would I encode/decode a case class that extends a generic trait e.g.
    trait Blah[A] {
      def blah(a: A): A
    }
    
    case class StringBlah[String]() {
      def blah(a: String) = a.toUpperCase
    }
    Colt Frederickson
    @coltfred
    @davidainslie In general this room is dead, but in your example, blah has nothing to do with serialization...
    David Ainslie
    @davidainslie
    Many thanks for letting me know. I did eventually sort things out. Thanks again
    Colt Frederickson
    @coltfred
    @davidainslie If you have a data type that maps directly to some other type that already has a codec you can use .map, .contramap and .xmap to convert.
    Paul Snively
    @PaulAtBanno
    Hey, everyone! What do I need to import to get the .asJson syntax?
    Or rather, do I need to expose an implicit val in scope if I want that syntax to be available outside a module that does the right import?
    I'd like to have my codecs, imports, etc. in one module, then elsewhere just say myFoo.asJson.
    Gary Pamparà
    @gpampara
    @PaulAtBanno That’s a good question! I’ve never seen that syntax tbh. I just usually use import argonaut._, Argonaut._ and then use .jencode to get the json
    I might be using out of date syntax
    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