Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Case class with List/Array/Seq does not unpickle to its parrent type #27

Open
@mraulim

Description

@mraulim

steps

Hello, I have some issue with unpickling derived case classes.
example (additional import Defaults._ is needed for 0.10.x):

scala> import scala.pickling._, Defaults._
import scala.pickling._

scala> import json._
import json._

scala> trait A
defined trait A

scala> case class B(a: List[Int]) extends A
defined class B

scala> B(List(1,2,3,4)).pickle.unpickle[B]
res1: B = B(List(1, 2, 3, 4))

scala> B(List(1,2,3,4)).pickle.unpickle[A]

problem

scala> B(List(1,2,3,4)).pickle.unpickle[A]
java.util.NoSuchElementException: key not found: hd
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:58)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:58)
    at scala.pickling.json.JSONPickleReader.readField(JSONPickleFormat.scala:234)
    at scala.pickling.json.JSONPickleReader.readField(JSONPickleFormat.scala:158)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2$$anonfun$fieldVals$1$1.apply(Runtime.scala:172)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2$$anonfun$fieldVals$1$1.apply(Runtime.scala:171)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
    at scala.collection.AbstractTraversable.map(Traversable.scala:105)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2.fieldVals$1(Runtime.scala:171)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2.unpickle(Runtime.scala:202)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2$$anonfun$fieldVals$1$1.apply(Runtime.scala:188)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2$$anonfun$fieldVals$1$1.apply(Runtime.scala:171)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
    at scala.collection.AbstractTraversable.map(Traversable.scala:105)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2.fieldVals$1(Runtime.scala:171)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2.unpickle(Runtime.scala:202)
    at .<init>(<console>:17)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
    at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
    at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:889)
    at xsbt.ConsoleInterface.run(ConsoleInterface.scala:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:73)
    at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:64)
    at sbt.Console.console0$1(Console.scala:23)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
    at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
    at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)

note

this works fine:

scala> trait A
defined trait A

scala> case class B(a: Int) extends A
defined class B

scala> B(2).pickle.unpickle[A]
res3: A = B(2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions