Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumeration not serializable in 2.8 (was in 2.7) #2211

Closed
scabug opened this issue Jul 29, 2009 · 3 comments
Closed

Enumeration not serializable in 2.8 (was in 2.7) #2211

scabug opened this issue Jul 29, 2009 · 3 comments
Labels

Comments

@scabug
Copy link

scabug commented Jul 29, 2009

Welcome to Scala version 2.8.0.r18383-b20090724020219 (Java HotSpot(TM) Client VM, Java 1.6.0_13).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object MyEnum extends Enumeration {
| val ONE = Value("1")
| val TWO = Value("2")
| }
defined module MyEnum

scala> val one = MyEnum.ONE
one: MyEnum.Value = 1

scala> val out = new java.io.ObjectOutputStream(new java.io.ByteArrayOutputStream)
out: java.io.ObjectOutputStream = java.io.ObjectOutputStream@37dde9

scala> out.writeObject(one)
java.io.NotSerializableException: scala.collection.generic.VectorTemplate$$Elements
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.jav...

@scabug
Copy link
Author

scabug commented Jul 29, 2009

Imported From: https://issues.scala-lang.org/browse/SI-2211?orig=1
Reporter: Kieron Wilkinson (kieron)
See #5211

@scabug
Copy link
Author

scabug commented Jul 29, 2009

Kieron Wilkinson (kieron) said:
Here it is again corrected using WikiFormatting:

Welcome to Scala version 2.8.0.r18383-b20090724020219 (Java HotSpot(TM) Client VM, Java 1.6.0_13).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object MyEnum extends Enumeration {
     |   val ONE = Value("1")
     |   val TWO = Value("2")
     | }
defined module MyEnum

scala> val one = MyEnum.ONE
one: MyEnum.Value = 1

scala> val out = new java.io.ObjectOutputStream(new java.io.ByteArrayOutputStream)
out: java.io.ObjectOutputStream = java.io.ObjectOutputStream@37dde9

scala> out.writeObject(one)
java.io.NotSerializableException: scala.collection.generic.VectorTemplate$$Elements
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.jav...

@scabug
Copy link
Author

scabug commented Jul 29, 2009

@michelou said:
Fixed in r18404. For example:

object Serialize {
  import java.io._
  def write[A](o: A): Array[Byte] = {
    val ba = new ByteArrayOutputStream(512)
    val out = new ObjectOutputStream(ba)
    out.writeObject(o)
    out.close()
    ba.toByteArray()
  }
  def read[A](buffer: Array[Byte]): A = {
    val in =
      new ObjectInputStream(new ByteArrayInputStream(buffer))
    in.readObject().asInstanceOf[A]
  }
}
object Enu extends Enumeration {
  val ONE = Value("1")
  val TWO = Value("2")
}
println(Enu.ONE)
val one: Enu.Value = Serialize.read(Serialize.write(Enu.ONE))
println(one)

prints

1
1

@scabug scabug closed this as completed May 18, 2011
@scabug scabug added the enum label Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant