by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    jodersky
    @jodersky
    Oh wow, I didn't know circe already supports dotty!
    Damian Reeves
    @DamianReeves
    no it doesn't
    oh you meant dotty
    jodersky
    @jodersky
    yeah sorry
    Damian Reeves
    @DamianReeves
    Yeah it does
    Damian Reeves
    @DamianReeves
    @jodersky so from that gist am I to get that if I use upickle/ujson 2.13 I can apply that derivation to define my readers and writers?
    Is there anything special you needed to do with the compiler to get it not to invoke macros?
    jodersky
    @jodersky
    nothing special, just don't use macroRW
    Damian Reeves
    @DamianReeves
    Oh wow. That simple
    Thanks
    jodersky
    @jodersky
    The way I use it is through a custom pickler:
    trait ReadWriter[A] extends Reader[A] with Writer[A]
    
    object ReadWriter {
      given rw[A](using r: Reader[A], w: Writer[A]) as ReadWriter[A]{
        def read(value: ujson.Value): A = r.read(value)
        def write(value: A): ujson.Value = w.write(value)
      }
    }
    
    object pickler {
    
      def read[A](value: ujson.Value)(using reader: Reader[A]): A =
        reader.read(value)
      def write[A](value: A)(using writer: Writer[A]): ujson.Value =
        writer.write(value)
    
    }
    and then you'd add aderives clause to your case classes:
    case class Foo(x: Int) derives Reader, Writer
    case class Bar(foo: Foo) derives Reader, Writer
    I use it at our company in an R&D project, but keep in mind that what I showed you really is just a prototype
    I'd love to find the time to port it back to upickle proper
    Damian Reeves
    @DamianReeves
    That is very nice. Thanks
    The thing is the JSON I’m using is so custom that I need to override every pickler anyway, so I end up not using macroRW
    jodersky
    @jodersky
    haha, I know that situation :)
    Damian Reeves
    @DamianReeves
    I forget is there a way to use a reader in upickle that doesn’t throw but returns an Either or some Validation like structure?
    Li Haoyi
    @lihaoyi
    there isn’t, everything throws and I don’t even remember what they throw haha
    you’ll have to catch the exception and wrap it yourself
    acse-fk4517
    @acse-fk4517

    Hi all, I am looking at the ujson library and it looks good. Ive played around with Circe a bit and wanted to ask if it had a deep merge like function built in?

    For example:

    val x = Js.Obj("x" -> "xval")
     val y = Js.Obj("y" -> "yval")
    // merge x and y ....

    And after merging they should look like this (I will be working on a deeply nested structure but this example should be valid):

    {
        "x":"xval",
        "y": "yval",
      }

    Is this possible?

    acse-fk4517
    @acse-fk4517
    seems like I cant deep merge but can interop with circe; will try that
    Li Haoyi
    @lihaoyi
    you can write your own deep merge, shouldnt be too much code
    but there isnt one built in
    RFC 7386
    Damian Reeves
    @DamianReeves
    I can’t figure out for the life of me why the implicit cannot be found in this case: https://scastie.scala-lang.org/DamianReeves/tFnnvtraQueMwx6CR7j3sw/8
    Oleg Pyzhcov
    @oleg-py
      trait LiteralCodec {
        implicit def readWriter[A: ReadWriter]: ReadWriter[Literal] =
    Why is there a type parameter?
    Damian Reeves
    @DamianReeves
    OMG! You are a life saver. This excerpt is from code where everything else requires that type parameter
    I’ve been looking at this for hours and couldn’t see that
    Oleg Pyzhcov
    @oleg-py
    yeah, the context bound triggers an implicit resolution that becomes ambiguous (A is literally anything), which is why this definition is ignored in implicit search
    Damian Reeves
    @DamianReeves
    Thanks so much. And yeah that makes perfect sense
    Oleg Pyzhcov
    @oleg-py
    np ^^
    Ken Swanson
    @Swandog

    If I have some JSON like this:

    {"data": [0, 1, 2]}

    Is there a way to use the upickle read method to access the array in the data array without defining a class for the whole object to deserialize?

    Basically, I'm trying to avoid writing a case class like
    case class Thing(data: Seq[Int])
    Li Haoyi
    @lihaoyi
    upickle.default.read[Seq[Int]](ujson.read("""{"data": [0, 1, 2]}""")("data"))
    basically ujson.read to parse it, grab the relevant part of the JSON blob via ("data"), and then use upickle.default.read on that part
    Andrew Richards
    @ajrnz
    If you have a your own upickle.AttributeTagged object for some special type of serialization is there any way to adapt the reader/writer so it can be used from upickle.default? I need to customize the serialization of a class which is part of a hierarchy which has serializers defined using upickle.default but the reader/writers aren't compatible.
    My example is that I'd like to write the default values for some elements of the hierarchy but not others
    Li Haoyi
    @lihaoyi
    you can define custom serializer instances for the specific subclasses via upickle.default.bimap
    Andrew Richards
    @ajrnz
    yeah but I would have to write the entire serializer from scratch. what I'd like to be able to do is use the one generated by a different AttributeTagged. You can't use bimap to do this it quotes the values you return
    bmeesters
    @bmeesters
    Hello, does ujson have a way of minimizing the differences between JSON blobs. We have some large blocks that we use for unit testing and if they are not the same we want to easily find out what has changed exactly.
    Doug Roper
    @htmldoug
    ujson is pretty minimal. It doesn't have any diff stuff, but you could easily convert your ujson.Value it to a different ADT and use something like https://github.com/json4s/json4s#merging--diffing.
    Carlos Kassab
    @LaranIkal
    Hi, I have imported upickle: import upickle._ and I am still getting the error: Symbol 'type geny.Writable' is missing from the classpath.
    This symbol is required by 'trait ujson.Value'. How can I get geny.Writable?. I am using plain Scala not ammonite

    Hi, I have imported upickle: import upickle._ and I am still getting the error: Symbol 'type geny.Writable' is missing from the classpath.
    This symbol is required by 'trait ujson.Value'. How can I get geny.Writable?. I am using plain Scala not ammonite

    This is my code:

    !/bin/sh

    exec scala \
    -classpath "$VUUBASE/jdbc/postgresql-42.2.12.jar:$VUUBASE/jdbc/ojdbc7.jar" \
    -savecompiled "$0" "$@"
    !#

    // This program will import admin.schema and set new values for some keys

    import scala.util.matching.Regex

    import upickle._

    import scala.io.Source
    import java.io.{FileNotFoundException, IOException}
    import java.io.File
    import java.io.PrintWriter
    import scala.util.control.Breaks._

    val logWriter = new PrintWriter(new File("logs/jsonfileops.log"))

    // Just with ammonite - import $ivy.com.lihaoyi::upickle:1.1.0

    // json filename
    val langsFilename = "admin.schema"

    //Read json file
    val jsonFileSource = scala.io.Source.fromFile(langsFilename)

    // Convert file into a long string
    val jsonlinesFromFile = try jsonFileSource.mkString finally jsonFileSource.close()

    val data = ujson.read(jsonlinesFromFile)

    //println(jsonlinesFromFile)

    val tableName = "PRODUCT_LINES2"

    //println(data)
    try {
    if( data("TABLE_COLUMNS")(tableName) == "" ) {
    println(s"Table: $tableName DOES NOT exist in schema.")
    } else {
    println(s"Table: $tableName exists in schema.")
    }
    } catch {
    case e : Throwable => {
    val notFoundPattern: Regex = "key not found".r

                                  // Check if the exception has not found pattern value according the regex above
    
                                  notFoundPattern.findFirstMatchIn(e.toString()) match
                                  {
                                    case Some(_) => { 
                                                      println(s"Table: $tableName DOES NOT exist in json schema, insert it.")
                                                      data("TABLE_COLUMNS")(tableName) = "description_code"
                                                      println(data("TABLE_COLUMNS")(tableName))
                                                    }
                                    case None => println(s"Unknown exception: $e") 
                                  }
                                }
        }

    Hi, I have imported upickle: import upickle._ and I am still getting the error: Symbol 'type geny.Writable' is missing from the classpath.
    This symbol is required by 'trait ujson.Value'. How can I get geny.Writable?. I am using plain Scala not ammonite

    This is my code:

    !/bin/sh

    exec scala \
    -classpath "$VUUBASE/jdbc/postgresql-42.2.12.jar:$VUUBASE/jdbc/ojdbc7.jar" \
    -savecompiled "$0" "$@"
    !#

    // This program will import admin.schema and set new values for some keys

    import scala.util.matching.Regex

    import upickle._

    import scala.io.Source
    import java.io.{FileNotFoundException, IOException}
    import java.io.File
    import java.io.PrintWriter
    import scala.util.control.Breaks._

    val logWriter = new PrintWriter(new File("logs/jsonfileops.log"))

    // Just with ammonite - import $ivy.com.lihaoyi::upickle:1.1.0

    // json filename
    val langsFilename = "admin.schema"

    //Read json file
    val jsonFileSource = scala.io.Source.fromFile(langsFilename)

    // Convert file into a long string
    val jsonlinesFromFile = try jsonFileSource.mkString finally jsonFileSource.close()

    val data = ujson.read(jsonlinesFromFile)

    //println(jsonlinesFromFile)

    val tableName = "PRODUCT_LINES2"

    //println(data)
    try {
    if( data("TABLE_COLUMNS")(tableName) == "" ) {
    println(s"Table: $tableName DOES NOT exist in schema.")
    } else {
    println(s"Table: $tableName exists in schema.")
    }
    } catch {
    case e : Throwable => {
    val notFoundPattern: Regex = "key not found".r

                                  // Check if the exception has not found pattern value according the regex above
    
                                  notFoundPattern.findFirstMatchIn(e.toString()) match
                                  {
                                    case Some(_) => { 
                                                      println(s"Table: $tableName DOES NOT exist in json schema, insert it.")
                                                      data("TABLE_COLUMNS")(tableName) = "description_code"
                                                      println(data("TABLE_COLUMNS")(tableName))
                                                    }
                                    case None => println(s"Unknown exception: $e") 
                                  }
                                }
        }

    I am sorry, I am very new in Scala, any help will be appreciated.

    Carlos Kassab
    @LaranIkal
    I want to use Ammonite for my Scala scripting, but I need to add some jdbc jars to the classpath, I have tried many ways, I read and tried all the possible things listed here but, not working: https://ammonite.io/?source=post_page---------------------------#ScalaScripts

    I want to use Ammonite for my Scala scripting, but I need to add some jdbc jars to the classpath, I have tried many ways, I read and tried all the possible things listed here but, not working: https://ammonite.io/?source=post_page---------------------------#ScalaScripts

    Do you know how to do a scala script in ammonite in order for me to have my jdvc drivers in a directory and set my classpath?

    scalway
    @scalway

    @Laranlkal

    1) What's wrong with import $ivy?

    import $ivy.`org.postgresql:postgresql:42.2.14`

    you should do that on start of your script and rest should happen well.

    2) You should use tool to import ".jar" for you (sbt/ammonite/mill/coursier). Upickle depends on other libraries (written by Li) and those also need to be on a classpath. Ammonite solves that problem for you.

    scalway
    @scalway
    3) please respect others. Your code is not readable at all. Please:
    • remove stuff that is not relevant
    • wrap whole code in triple ` signs
    scalway
    @scalway
    4) In MultiStage part of ammonite there is also part about classpaht https://ammonite.io/?source=post_page---------------------------#Multi-stageScripts