Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Lorenzo Gabriele
    @lolgab

    Hi! :wave:
    Does anyone want to review this PR I opened to fix the CI (it wasn't running the tests): com-lihaoyi/upickle#359 ?

    Thanks!

    Then we can trigger a release @htmldoug @objektwerks

    Objektwerks
    @objektwerks
    @lolgab Thanks for the update, Lorenzo. Looking forward to the new release.:)
    Lorenzo Gabriele
    @lolgab
    @objektwerks @htmldoug UPickle 1.4.1 is now going to Maven Central: https://github.com/com-lihaoyi/upickle/runs/3514651632
    Objektwerks
    @objektwerks
    @lolgab Awesome!
    Objektwerks
    @objektwerks
    Just upgraded to upickle 1.4.1 - and no more @nowarn annotation required! Thanks, team uPickle!
    Lorenzo Gabriele
    @lolgab
    Thank you to @htmldoug who made the fix! :)
    Happy having fixed the CI so now we can finally merge PRs confidently!
    Siddhant Sanyam
    @siddhant3s
    I'm trying to use upickle to extract my case classs schema. The visitor that uPickle provide is great. I would like to have a visitor on the type if you know what I mean:
    1. I have user defined case class Foo(..)
    2. I want a JSON deserializer for that class. That's easy, I can just use macroRW
    3. I also want to codegen Foo into a Python class. For that, it would be nice if I could re-use any of uPickle's machinery to have a Visitor on the schema of Foo. That way I'll be able to generate the Python code string easily
    Li Haoyi
    @lihaoyi
    i dont thinkvthe visitor is helpful for codegen. Visitor is called on the runtime data structure, not on the definition structure. Only macroRW has that; you could try forking it to generate the code directly or to generate metadata you can use for the codegen
    Siddhant Sanyam
    @siddhant3s
    Do you think it's possible to use the Reader[T] generated by macroRW[T] to something like that? I mean I can write my own macro, but I was hoping if I can re-use something from your macro so that I can be assured that 2. and 3. are in sync.
    Li Haoyi
    @lihaoyi
    no, the macro generated read/writers explicitly avoid generating inspectable data structures to avoid performance overhead. It makes them fast, but it means you cannot really repurpose them for anything else
    i could imagine coming up with some way to store a lazily instantiated type description on each readwriter, so others can use it, but that's not what happens now
    Siddhant Sanyam
    @siddhant3s
    Thanks for confirming. I'll handwrite it in that case!
    Siddhant Sanyam
    @siddhant3s

    @lihaoyi I've been going through your wonderful tutorial on Visitor pattern yet again. I want to modify it slightly to include a cache based early stop for recursive Data structure. Think of an ADT which is self referencing. With the pattern in the tutorial, there is no way for visitor to communicate back to dispatcher to stop going further.
    So I came up with this approach, could you please comment on what you think about it:

        trait Visitor[T] {
            def visitPrimitive(tpe: Type): T
            def visitOption(innerVisited: T): T
            def visitArrayType(innerVisited: T): T
            def visitMapType(keyVisited: T, valueVisited: T): T
            def visitStructType(tpe: Type): StructVisitor[T]
            def stopValue: Option[T] = None // If defined, we stop visiting and simply return this value
        }
        trait StructVisitor[T] {
            def visitFieldKey(name: String): Visitor[T]
            def visitFiledType(name: String, visitedValue: T): Unit
            def done: T
            def stopValue: Option[T] = None  // If defined, we stop visiting and simply return this value
        }

    This is a visitor to visit Type from scala.reflect

    Not 100% happy with it, so was wondering if you can share some thought on how to improve it.
    Li Haoyi
    @lihaoyi
    I don't have any real insight here, depends on how well it fits your use case
    I always find visitors pretty finnicky to think about, so I always end up with a lot of "shake it around until it compiles, shake it some more until tests pass" kind of workflows
    Yeitijem
    @Yeitijem
    Hi is there some documentation about using upickle in conjunction with scala 3? And if yes, where can I find it?
    Simon Parten
    @Quafadas
    WOuld anyone have a hint on this error when using a library with a upickle dependancy from ammonite?
    java.lang.IncompatibleClassChangeError: class ilsop.models.RiskDecompositionSearchResult$$anon$10$$anon$11 has interface upickle.core.Types$CaseR$CaseObjectContext as super class
    Lorenzo Gabriele
    @lolgab
    @Quafadas It can be that you're using a library that depends on a different version of upickle that is not binary compatible. I think you can use ammonite without the com-lihaoyi libraries, so you can use the version of upickle you want.
    Simon Parten
    @Quafadas
    Ah, are you saying the com-lihayoi libraries are imported by default... and I should disable that default import so I can use my own version? That sounds plausible... brb... thanks!
    Alexey Novakov
    @novakov-alexey
    Hi. Does upickle work with Scala 3? I need to parse JSON text into a list of case classes.
    Alexey Novakov
    @novakov-alexey
    ok..... it works fine in Scala 3 project, for example:
    object TaskInstance:
      given rw: SnakePickle.ReadWriter[TaskInstance] = SnakePickle.macroRW
    Lorenzo Gabriele
    @lolgab
    Yes, it does @novakov-alexey
    Some Scala 3 specific features are not supported well yet, like converting a Scala 3 enum to JSON, but the shared Scala 2 / Scala 3 works almost identically.
    Almost because I remember there were some bugs open, maybe they now are all solved.
    Alexey Novakov
    @novakov-alexey
    @lolgab thanks for reply. Is there some workaround for reading JSON into Scala 3 enums?
    I am wondering which JSON library supports Scala 3 enums today :-)
    Simon Parten
    @Quafadas
    Does upickle support validation of a json document vs a schema? I couldn't see anything... is anyone aware of a good solution to this in scala / java. I think I'm googling the wrong questions somehow...
    1 reply
    Stephen Link
    @BoopBoopBeepBoop

    I was looking at one of the open bugs for com-lihaoyi/upickle#356 and think I'm struggling a bit to figure out exactly where my understanding is breaking down. I've never used macros before, so that might be part of it.

    The way it appears to me that things are happening right now is that the Sum types for the enumeration are being derived correctly when we go to write the values, but the implicit classtag that is carried through is the classtag for the enumeration supertype.

    package upickle
    
    import scala.language.implicitConversions
    import utest._
    
    import upickle.default._
    
    object EnumTests extends TestSuite {
    
      enum SimpleEnum derives ReadWriter:
        case A, B
    
      val tests = Tests {
        test("simple") {
          test("enum write") {
            val parsed = write(SimpleEnum.B)
            val expected = """{"$type":"upickle.EnumTests.SimpleEnum.B"}"""
            assert(parsed == expected)
          }
        }
      }
    }

    I added some printlns to the code temporarily, but see the line beggining with Instance Check

    ----------------------- Running Tests upickle.EnumTests -----------------------
    >>>>> SUM READER TRAVERSING [upickle.EnumTests$SimpleEnum$@1b73be9f]
    >>>>> PRODUCT READER []
    >>>>> PRODUCT READER []
    >>>>> SUM WRITER TRAVERSING [upickle.EnumTests$SimpleEnum$@1b73be9f]
    CASE CLASS WRITER defaults[]
    >>>>> PRODUCT WRITER FULL CLASS NAME: upickle.EnumTests.SimpleEnum.A
    CASE CLASS WRITER defaults[]
    >>>>> PRODUCT WRITER FULL CLASS NAME: upickle.EnumTests.SimpleEnum.B
    Instance Check upickle.EnumTests.SimpleEnum.A [B <: upickle.EnumTests$SimpleEnum]: true
    RETURNING (upickle.EnumTests.SimpleEnum.A,upickle.implicits.CaseClassWriterPiece$CaseClassWriter@42721fe) for input B
    >>>>> PRODUCT WRITER []
    >>>>> PRODUCT WRITER []
    X upickle.EnumTests.simple.enum write 30ms 
      utest.AssertionError: parsed == expected
      parsed: String = {"$type":"upickle.EnumTests.SimpleEnum.A"}
      expected: String = {"$type":"upickle.EnumTests.SimpleEnum.B"}
        utest.asserts.Asserts$.assertImpl(Asserts.scala:30)
        upickle.EnumTests$.$init$$$anonfun$3$$anonfun$2$$anonfun$2(EnumTests.scala:13)
    1 targets failed
    upickle.jvm[3.0.2].test.test 1 tests failed: 
      upickle.EnumTests upickle.EnumTests.simple.enum write
    That Instance Check line comes from TaggedReader.Leaf.findWriter, which I've modified like this (just enough to get some debugging information):
    class Leaf[T](c: ClassTag[_], tag: String, r: CaseW[T]) extends TaggedWriter[T]{
      def findWriter(v: Any) = {
        val isInstance = c.runtimeClass.isInstance(v)
        println(s"Instance Check $tag [$v <: ${c.runtimeClass.getName}]: ${isInstance}")
        if (isInstance) (tag -> r)
        else null
      }
    }
    Stephen Link
    @BoopBoopBeepBoop

    so, the way that this appears to me, although I'm very much a novice here, is that val parsed = write(SimpleEnum.B) is resulting in the usage of a classtag for SimpleEnum itself? And not SimpleEnum.B.

    Thoughts?

    Stephen Link
    @BoopBoopBeepBoop

    Ah, a simpler answer. The macros are more specific than the classtag. So even though the macro is giving the correct type, the classtag that is implicitly carried is not specific to the enum instance

    CLASS NAME: upickle.EnumTests.SimpleEnum.B Classtag: upickle.EnumTests$SimpleEnum

    Ryan Tomczik
    @Tomczik76
    Hey everyone I'm new to upickle, I'm looking for a way to generate a set of all the paths in a JSON object without using an intermediate AST. It looks like upickle might be a good option using a Visitor. The TraceVisitor looks similar to what I need. Is there a way to use it out of the box to generate all the paths in a JSON object?
    Ryan Tomczik
    @Tomczik76
    Nevermind, I implemented a custom one