uPickle does not handle BigDecimal?

219 views
Skip to first unread message

SemanticBeeng .

unread,
Feb 4, 2015, 12:13:33 PM2/4/15
to scal...@googlegroups.com
Hi,

Getting "uPickle does not know how to write " errors for some case classes using BigDecimal.

Is there a way around this by writing a BigDecimal format or must I write a format for every class containing BigDecimal?

Thanks
Nick

Haoyi Li

unread,
Feb 4, 2015, 12:15:59 PM2/4/15
to SemanticBeeng ., scal...@googlegroups.com
Yep, uPickle doesn't support BigDecimal.


You can easily write your own format for BigDecimal to make it work on the JVM, but for Scala.js you'll need to implement it.

No need to do it for every class, just BigDecimal itself is sufficient.

--
You received this message because you are subscribed to the Google Groups "Scala.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-js+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-js/CAJzU2RRp-yJ5epb1Khv3HU6Xzz%3DFcgco_jU5ZfsjcWj3OKhG6Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

SemanticBeeng

unread,
May 24, 2015, 11:16:11 AM5/24/15
to scal...@googlegroups.com, Sébastien Doeraene, scal...@googlegroups.com, semanticbeen...@gmail.com
Hi,

Now the BigDecimal is supported (thanks!) in SJS 0.6.3 it is possible to make uPickle work with BigDecimal

 object BigDecimalFormat {

    val nothing = ""

    implicit val bigDecimal2Writer = upickle.Writer[BigDecimal] {
      case t => Js.Str("" + t)
    }

    implicit val bigDecimal2Reader = upickle.Reader[BigDecimal] {
      case Js.Str(str) => BigDecimal(str)
    }
  }

It is just that one needs to explicitly import BigDecimalFormat._ by hand wherever needed.
Intellij thinks it is unused and auto optimizes it away (annoying).

Is there a better way?

Thanks
Nick

Benjamin Jackman

unread,
May 29, 2015, 2:37:39 PM5/29/15
to scal...@googlegroups.com, sjrdo...@gmail.com
You could put that into a trait you mix into your package objects. Then you won't have to import it.
You will have to deal with chained package clauses / base packages if the package object is up 
the hierarchy from your file.

In general for implicits I prefer a style where library makers write their code in such a way that I can mix their exports in via a trait to my package objects so that I don't have to say import somelib._ everywhere. Usually they just write an object that contains all their exports, however if they put them into a trait they could still just have the eponymous object mix it in, and clients would also have the option to simply mix it the trait into their package objects if they set their projects up that way, or
just use the import someObject._ style if they prefer (small projects).

Benjamin Jackman

unread,
May 29, 2015, 2:42:32 PM5/29/15
to scal...@googlegroups.com, sjrdo...@gmail.com
to clarify i'd have structured the code like this

 trait BigDecimalFormat {

    val nothing = ""

    implicit val bigDecimal2Writer = upickle.Writer[BigDecimal] {
      case t => Js.Str("" + t)
    }

    implicit val bigDecimal2Reader = upickle.Reader[BigDecimal] {
      case Js.Str(str) => BigDecimal(str)
    }
  }

object BigDecimalFormat extends BigDecimalFormat

//In the base package of your project
//file mycorp/foo/package.scala
package mycorp

package object foo extends BigDecimalFormat {
....
}

//File /mycorp/foo/bars/MyClass.scala
package mycorp.foo
package bars

class MyClass {
  //BigDecimalFormat will be in scope
Reply all
Reply to author
Forward
0 new messages