You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
The createdAt field does not appear in the JSON, and hence upon unpickling is set to a new (different) value. By contrast, the millis field is represented and hence retains the originally assigned value. If createdAt is defined as a var instead of a val then it will appear in the JSON and the correct value is preserved.
Is this a bug or a feature? (In our actual application, createdAt is really an internal uniqueID field, so really do want it declared as a val so that it can never be accidentally modified.)
This was run using 0.11.0-M2, Scala 2.11.8, java 1.8.0_121, the IDE is IntelliJ
The complete test routine and console output is pasted below.
BTW: 0.10.0 throws an exception trying to unpickle, claiming that the var finishedAt is not represented as a Java variable in the class file. Have the same problem in 0.11.0-M2 in our full application, but have not been able to reproduce within an isolated test file.
package pickletests
import scala.pickling._
import scala.pickling.Defaults._
import scala.pickling.json._
/**
* Created by JCrowley on 6/19/2017.
*/
trait TestTrait1 {
val createdAt = System.currentTimeMillis
var finishedAt:Long = 0
def setAsFinished():Unit = finishedAt = System.currentTimeMillis
override def toString = s"TestTrait[createdAt=$createdAt, finishedAt=$finishedAt]"
}
case class TestCaseClass(int:Int, dbl:Double, str:String, millis:Long = System.currentTimeMillis) extends TestTrait1 {
override def toString = s"TestCaseClass[int=$int, dbl=$dbl, str='$str', millis=$millis ${super.toString}]"
}
object PickleVarInTraitTests extends App {
val test = TestCaseClass(1, 2.0, "String3")
Thread.sleep(1000)
test.setAsFinished()
Console.println(s"\nEnvironment - Scala: ${scala.util.Properties.versionString} Java: ${System.getProperty("java.version")}\n")
Console.println("\nOriginal TestCaseClass: " + test.toString + "\n")
val testPickled = test.pickle
val testPickledJSON = testPickled.value
Console.println("JSON pickled: " + testPickledJSON + "\n")
val testUnpickle = testPickledJSON.unpickle[TestCaseClass]
Console.println("Success for [TestCaseClass] = " + testUnpickle + "\n")
val testUnpickle2 = testPickledJSON.unpickle[AnyRef]
Console.println("Unpickle1 == Unpickle2: " + (testUnpickle == testUnpickle2))
}
Have this trait:
and this case class:
instantiating the case class by:
TestCaseClass(1, 2.0, "String3")
and pickling gives this JSON:
The createdAt field does not appear in the JSON, and hence upon unpickling is set to a new (different) value. By contrast, the millis field is represented and hence retains the originally assigned value. If createdAt is defined as a var instead of a val then it will appear in the JSON and the correct value is preserved.
Is this a bug or a feature? (In our actual application, createdAt is really an internal uniqueID field, so really do want it declared as a val so that it can never be accidentally modified.)
This was run using 0.11.0-M2, Scala 2.11.8, java 1.8.0_121, the IDE is IntelliJ
The complete test routine and console output is pasted below.
BTW: 0.10.0 throws an exception trying to unpickle, claiming that the var finishedAt is not represented as a Java variable in the class file. Have the same problem in 0.11.0-M2 in our full application, but have not been able to reproduce within an isolated test file.
----------------------------------- Console Output -------------------------------------
The text was updated successfully, but these errors were encountered: