Details
-
Type: Bug
-
Status: Closed
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.1.3
-
Component/s: None
-
Labels:None
-
JDK version and platform:1.5.1-01 and fedora core 3
Description
It appears that XStream is not deserializing Enums correctly.
Here is a test for proof:
public enum MyEnum {
Test1(), Test2();
MyEnum() {
}
// public Object readResolve()
{ // return MyEnum.valueOf(this.name()); // }}
MyEnum test1 = MyEnum.Test1;
System.out.println(test1.equals(xStream.fromXML(xStream.toXML(test1))));
If you run the previous two lines, it will print out false signifying that test1 is not equal to itself when ran through XStream. Now if you uncomment the readResolve method it works just fine, however, XStream should return the correct Enum. The problem exists in the ObjectInputStream. If you read the java docs on Enums in the ObjectInputStream, the ois should call the static method Enum.valueOf(Class, String).
Here is a solution:
You could add an EnumConverter.
public class EnumConverter extends Converter {
{ return type.isEnum(); }public boolean canConvert(Class type)
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context)
{ writer.setValue(((Enum)source).name()); }public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
{ return Enum.valueOf(context.getRequiredType(), reader.getValue()); }}