pickling icon indicating copy to clipboard operation
pickling copied to clipboard

Unpickle case object to AnyRef does not work as expected

Open tobnee opened this issue 10 years ago • 2 comments

It seems that the reflection that is needed for unpickling to AnyRef will create a new instance of the case object, which gives us two singletons of the same type:


sealed trait Drink
case object Beer extends Drink
case object Wine extends Drink
case class OrderDrinkV1(drink: Drink)
case class DrinkOrderedV1(drink: Drink)

object TestApp extends App {
  import scala.pickling.Defaults._
  import scala.pickling.json._

  val order = OrderDrinkV1(Beer)
  val json = order.pickle.value
  val anyRefValue = JSONPickle(json).unpickle[AnyRef]
  val drink = anyRefValue.asInstanceOf[OrderDrinkV1].drink

  // ok 
  assert(drink.getClass == Beer.getClass)

  // fails and would work if the unpickle would target OrderDrinkV1 and not AnyRef
  assert(drink == Beer)
}

tobnee avatar Aug 13 '15 16:08 tobnee

@tobnee Looks like you're hitting the runtime picklers. I've fixed this in 0.11.x with the new macros. Can you try against master and see if the issue persists? If not, I'll mark this as closed (we're trying to get a 0.11.x out soon).

jsuereth avatar Aug 13 '15 20:08 jsuereth

Oh, also as a workaround, you can provide a custom pickler/unpickler for the case objects.

jsuereth avatar Aug 13 '15 20:08 jsuereth