amnaredo / test

0 stars 0 forks source link

Can't convert case class with option field to JSON string #114

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago
case class User(name: String, stars: Option[Int] = None)

val u = new User("Freewind", Some(7))
println("### u: " + u)
val s = upickle.write(u)
println("### s: " + s)

Will produce error:

### u: User(Freewind,Some(7))
scalajsenv.js:95 Uncaught scala.scalajs.runtime.UndefinedBehaviorError: 
An undefined behavior was detected: Some(7) is not an instance of scala.Tuple2

ID: 71 Original Author: freewind

amnaredo commented 3 years ago

You forgot to import the implicits (import upickle._) in https://github.com/freewind/wind-links-scala-js-react/blob/master/src/main/scala/in/freewind/windlinks/DataConverter.scala. uPickle should print a better error message to avoid the confusion.

Edit: You may want to use org.scalajs.dom.extensions.KeyCode instead of rolling out your own keycode table.

Original Author: tindzk

amnaredo commented 3 years ago

Thanks very much @tindzk, I will try it tomorrow. And thanks for the suggestion about the KeyCode!

PS: I just renamed the package name, so the url of DataConverter.scala now is: https://github.com/freewind/fast-links/blob/master/src/main/scala/in/freewind/fastlinks/DataConverter.scala

Original Author: freewind

amnaredo commented 3 years ago

Thanks, forgot to close this issue

Original Author: freewind