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
Serializing an enum value depends on members of the the enum object being initialized or not. Note that for most users the serialized bytes don't matter, because Enumeration class has a readResolve method that returns the correct value instance.
objectTestEnumextendsEnumeration {
valLeft, Centre, Right=Value
}
defserialize(x: Any) = {
importjava.io._valbaos=newByteArrayOutputStream(1024);
valoos=newObjectOutputStream(baos);
oos.writeObject(x);
oos.flush();
baos.toByteArray
}
importjava.util.Arraysvalt=TestEnum.Rightvalser1= serialize(t) // serialize tvalvalues=TestEnum.values // get the list of possible valuesvalser2= serialize(t) // serialize the same t again
println(Arrays.equals(ser1, ser2)) // outputs false
The abstract class Enumeration has member objects ValueOrdering and ValueSet, which may or may not be initialized when serializing. They should be marked @transient, but that doesn't work for member objects. Test for that:
classCextendsSerializable {
@transient // not respectedobjectOextendsSerializable { valx=0 } // needs to be serializable// replace the object definition by the following, then serialization is stable// class O { val x = 0 }// @transient// lazy val O = new O
}
objectSD {
importjava.io._defserialize(obj: AnyRef):Array[Byte] = {
valbuffer=newByteArrayOutputStreamvalout=newObjectOutputStream(buffer)
out.writeObject(obj)
buffer.toByteArray
}
defdeserialize(a: Array[Byte]):AnyRef= {
valin=newObjectInputStream(newByteArrayInputStream(a))
in.readObject
}
defserializeDeserialize[T<:AnyRef](obj: T) = deserialize(serialize(obj)).asInstanceOf[T]
}
importSD._importjava.util.Arrays.{equals=>arrComp}
objectTestextendsApp {
valc=newCvala= serialize(c)
c.Ovalb= serialize(c)
println(arrComp(a, b))
}
An easy fix may be to add empty readObject/writeObject methods to Enumeration (to investigate the consequences).
The text was updated successfully, but these errors were encountered:
Serializing an enum value depends on members of the the enum object being initialized or not. Note that for most users the serialized bytes don't matter, because Enumeration class has a
readResolve
method that returns the correct value instance.The abstract class Enumeration has member objects ValueOrdering and ValueSet, which may or may not be initialized when serializing. They should be marked
@transient
, but that doesn't work for member objects. Test for that:An easy fix may be to add empty readObject/writeObject methods to Enumeration (to investigate the consequences).
The text was updated successfully, but these errors were encountered: