Details
-
Type:
Improvement
-
Status:
Closed
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.3
-
Component/s: None
-
Labels:None
-
JDK version and platform:1.5+
Description
Serialization of Java 5 Enum to/from xml attribute doesn't work:
xstream.useAttributeFor(MyEnum.class)
unless SingleValueConverter is registered:
xstream.registerConverter(EnumSingleValueConverter.create(MyEnum.class));
below (and in attachment) is the converter code:
public class EnumSingleValueConverter<T extends Enum<T>>
extends EnumConverter implements SingleValueConverter {
private Class<T> enumType;
public static <V extends Enum<V>> SingleValueConverter create(Class<V> enumClass) {
return new EnumSingleValueConverter<V>(enumClass);
}
private EnumSingleValueConverter(Class<T> newEnumType) {
this.enumType = newEnumType;
}
public boolean canConvert(Class type) {
return type == enumType;
}
public Object fromString(String str) {
return Enum.valueOf(enumType, str);
}
public String toString(Object obj) {
return obj.toString();
}
}
it should be possible to add support for that in XStream facade so user wouldn't have to register anything.
Attachments
Issue Links
| This issue is depended upon by: | ||||
| XSTR-413 | @XStreamAsAttribute should support of enum types |
|
|
|
Enums can now be defined as attributes with the standard API. You may give the head revision a try.