How can I unpickle an object with a field that might miss (and use an null) in that case?
scala> case class Person(name:String=null,age:Int)
defined class Person
scala> import scala.pickling.Defaults._, scala.pickling.json._
scala> val p2 = JSONPickle("""{"age":2}""").unpickle[Person]
scala.pickling.PicklingException: No field 'name' when unpickling, tag Person, fields were Map(age -> 2.0)
I need this in order to unpickle a string came from browser that is also missing the type field, so I will not be able to use an Option/Some/None type.
Just declare it having type
Option[String]
, like this:Option[T]
is the idiomatic Scala way of handling values which otherwise (e.g. in Java) could benull